Dart Programming – If Else Statement (if , if..else, Nested if, if-else-if)

Decision-making statements are those statements which allow the programmers to decide which statement should run in different conditions. There are four ways to achieve this: 

if Statement:

This type of statements simply checks the condition and if it is true the statements within it is executed but if it in is not then the statements are simply ignored in the code.

Syntax: 
if ( condition ){
  // body of if
}

 

Example: 
 



Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int gfg = 10;
  
    // Condition is true
    if (gfg > 3) { 
      // This will be printed
        print("Condition is true"); 
    }
}

chevron_right


Output: 

Condition is true

 if…else Statement:

This type of statement simply checks the condition and if it is true, the statements within is executed but if not then else statements are executed.
 

Syntax: 
if ( condition ){
  // body of if
}
else {
  // body of else
}

 

Example: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int gfg = 10;
  
    // Condition is false
    if (gfg > 30) { 
      // This will not be printed
        print("Condition is true"); 
    }
    else {
      // This will be printed
        print("Condition id false"); 
    }
}

chevron_right


Output: 



Condition is false

else…if Ladder:

This type of statement simply checks the condition and if it is true the statements within it is executed but if it in is not then other if conditions are checked, if they are true then they are executed and if not then the other if conditions are checked. This process is continued until the ladder is completed.

Syntax: 
if ( condition1 ){
  // body of if
}
else if ( condition2 ){
  // body of if
}
.
.
.
else {
  // statement
}

Example: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int gfg = 10;
    if (gfg < 9) {
        print("Condition 1 is true");
        gfg++;
    }
    else if (gfg < 10) {
        print("Condition 2 is true");
    }
    else if (gfg >= 10) {
        print("Condition 3 is true");
    }
    else if (++gfg > 11) {
        print("Condition 4 is true");
    }
    else {
        print("All the conditions are false");
    }
}

chevron_right


Output: 

Condition 3 is true

Nested if Statement:

This type of statements checks the condition and if it is true then the if statement inside it checks its condition and if it is true then the statements are executed otherwise else statement is executed.

Syntax: 
if ( condition1 ){
  if ( condition2 ){
     // Body of if
  }
  else {
    // Body of else
  }
}

 

Example: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int gfg = 10;
    if (gfg > 9) {
        gfg++;
        if (gfg < 10) {
            print("Condition 2 is true");
        }
        else {
            print("All the conditions are false");
        }
    }
}

chevron_right


Output: 

All the conditions are false

 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.