Dart If statement
If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true. If it returns false then the statements following the end of the if statement is executed.
Syntax :
if ( condition )
{
Statements;
}
This condition is a test expression that evaluated to Boolean True or False and the execution of statements is based on these Boolean values.
Program
Consider the following code in dart that contains if statement,
import 'dart:io';
void main( )
{
print( " Enter your age : " ) ;
int? age = int.parse( stdin.readLineSync( ) ! ) ;
if ( age >= 18 )
{
print( " You are eligible to vote ! \n " ) ;
}
}
Output:
Enter your age :
25
You are eligible to vote !
Program
Consider the following code in dart that contains multiple if statements,
import 'dart:io';
void main( )
{
print( " Enter the value of x : " ) ;
int? x = int.parse( stdin.readLineSync( ) ! ) ;
print( " Enter the value of y : " ) ;
int? y = int.parse( stdin.readLineSync( ) ! ) ;
if ( x > y )
{
print( " x is greater than y \n " ) ;
}
if ( x < y )
{
print( " x is less than y \n " ) ;
}
if ( x == y )
{
print( " x is equal to y \n " ) ;
}
}
Output
Enter the value of x :
25
Enter the value of y :
50
x is less than y
Enter the value of x :
50
Enter the value of y :
25
x is greater than y
Enter the value of x :
50
Enter the value of y :
50
x is equal to y