Dart If-else statement
In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then the else – block is executed.
This else-block is always in association with the if block.
Syntax:
if ( condition ) { statements ; } else { statements ; }
Here, condition is a test expression that evaluates to Boolean True or False.
Program
Consider the following code in Dart that explains the concept of if-else statement,
import 'dart:io'; void main( ) { print( " Enter an integer : " ) ; int? a = int.parse( stdin.readLineSync( ) ! ) ; print( " Enter an integer : " ) ; int? b = int.parse( stdin.readLineSync( ) ! ) ; if ( a > b ) { print( " A is greater than B " ) ; } else { print( " B is greater than A " ) ; } }
Output
Enter an integer : 5 Enter an integer : 10 B is greater than A
Program
Consider the following code that accepts the integer value from the user and checks whether it is even or odd.
import 'dart:io'; void main( ) { print( " Enter an integer : " ) ; int? a = int.parse( stdin.readLineSync( ) ! ) ; if (a % 2 == 0) { print( " A is an even integer " ) ; } else { print( " A is an odd integer " ) ; } }
Output
Enter an integer : 5 A is an odd integer Enter an integer : 10 A is an even number