Nested If Statement Example Program in Dart


The program starts by importing the dart:io library to allow the user to input data from the console. The next line declares two variables age and gender without initializing them. The program then prints a message on the console asking the user to enter their age.

The stdin.readLineSync() method is used to read user input from the console. The call to stdin.readLineSync() reads a string value for age, which is then converted into an integer using the int.parse() method.

The program then prints a message on the console asking the user to enter their gender. The stdin.readLineSync() method is used again to read user input from the console. The call to stdin.readLineSync() reads a string value for gender.

The program then starts a nested if-else statement to determine which room the user should go to based on their age and gender. If the user's age is greater than or equal to 18, the block of code inside the first if statement is executed.

If the user's gender is "M" or "m", the block of code inside the nested if statement is executed, which prints a message on the console saying "Goto Room-5".

If the user's gender is not "M" or "m", the block of code inside the nested else statement is executed, which prints a message on the console saying "Goto Room-6".

If the user's age is less than 18, the block of code inside the else statement is executed, which prints a message on the console saying "You are not Eligible".

Overall, this program demonstrates how to use nested if-else statements in Dart to perform more complex conditional logic based on multiple criteria. In this case, the program directs users to different rooms based on their age and gender.

Source Code

import 'dart:io';
 /*
 *   Age>=18
 *   Gender='M'
 *   Room-5
 *   Room-6
 *   Not Eligible
 * 
 */
void main() {
  var age,gender;
  print('Enter Your Age : ');
  age=int.parse(stdin.readLineSync());
  print('Enter Your Gender : ');
  gender=stdin.readLineSync();
  if(age>=18)
  {
    if(gender=='M'||gender=='m')
	{
      print('Goto Room-5');
    }
    else
    {
      print('Goto Room-6');
    }
  }
  else
  {
    print('You are not Eligible');
  }
}
To download raw file Click Here

Output

Enter Your Age : 
20
Enter Your Gender : 
Male
Goto Room-6