loading

if-else Statement

An if-block in Dart is executed if the specified condition is met. Otherwise-block is run if the supplied condition is untrue. The if-block is connected to the else block.

Dart if…else Statement Flow Diagram

If-Else Statement -

Syntax

				
					if(condition) {  
      // statement(s);  
} else {  
    // statement(s);  
}  
				
			

The if-else phrase is used in this case to indicate whether the outcome is TRUE or False. Body is executed if the given condition evaluates true; otherwise, body is executed if the supplied condition evaluates false.

Example

				
					void main() {  
     var x = 20;  
     var y = 30;  
    print("if-else statement example");  
  
    if(x > y){  
         print("x is greater than y");     
} else {  
         print("y is greater than x");  
  
};  
  
}  
				
			

Output

				
					if-else statement example
y is greater than x
				
			

Explanation

There are two variables in the code above that store integer values. The else-block was printed after the supplied condition was evaluated as false.

Example

				
					void main() {  
     var num = 20;  
       
    print("if-else statement example");  
  
    if(num%2 == 0){  
         print("The given number is even");     
} else {  
         print("The given number is odd");  
};  
} 
				
			

Output

				
					If-else statement example
The given number is even
				
			

Explanation

In the example above, we used an if-else statement to determine whether a given number is even or odd. The integer variable num had the value 20. Since the modulus of 20 equals 0, the supplied condition was assessed as true, and the given value was displayed on the screen.

Share this Doc

if-else Statement

Or copy link

Explore Topic