loading

if else-if Statement

The ability to verify a group of test expressions and run the various statements is provided by the dart if else-if statement. It is applied when there are more than two options from which to choose.

Dart if else if Statement Flow Diagram

If Else-If Statement -

Syntax

				
					if (condition1) {  
   // statement(s)  
}  
else if(condition2) {  
   // statement(s)  
}  
else if (conditionN) {  
   // statement(s)  
}  
.  
.  
else {  
   // statement(s)  
} 
				
			

The else..if ladder is another name for this kind of structure in this context. A top-to-bottom evaluation of the state is made. The statement linked to that condition is carried out whenever the genuine condition is discovered. The else block is invoked if every condition in the provided statement evaluates to false.

Example

				
					void main() {  
var marks = 74;     
if(marks > 85)  
{  
       print("Excellent");  
}  
 else if(marks>75)  
{  
      print("Very Good");  
}   
else if(marks>65)  
{  
      print("Good");  
}  
else  
 {  
      print("Average");  
}  
}  
				
			

Output

				
					Average
				
			

Explanation

Based on the exam scores, the application mentioned above prints the outcome. If else if has been used to print the outcome. The integer value 74 has been initialized in the marks variable. We have examined all of the program’s conditions.

Since the first condition is false, the marks will be checked with it before moving on to the second condition.

After verifying that the second condition was true, it printed the result on the screen.

Until all expressions have been evaluated, this procedure will continue; if not, control will exit the else if the ladder and default statement are written.

You should change the value mentioned above and see the outcome.

Nested If else Statement

An if-else inside another is known as a dart nested if else statement. It helps when we have to make a lot of decisions. Let’s examine the next illustration.

Example

				
					void main() {  
  var a = 10;  
  var b = 20;  
  var c = 30;  
   
  if (a>b){  
       if(a>c){  
            print("a is greater");  
        }  
       else{  
            print("c is greater");  
               }         
   }  
else if (b>c){  
     print("b is greater");  
}  
else {  
     print("c is greater");  
}  
}  
				
			

Output

				
					C is greater
				
			

Three variables, a, b, and c, with the values 10, 20, and 30 have been declared in the program above. We included a condition in the outer if-else that determines whether an is greater than b. The inner block will be executed if the condition is true; else, the outer block will be executed.

Another condition in the inner block determines whether or not variable an is greater than variable c. The inner block will be executed if the condition is determined to be true.

After our program bypassed the inner block check of the second condition, it returned false in the first condition. If the requirement was met, the output was displayed on the screen.

Share this Doc

if else-if Statement

Or copy link

Explore Topic