If Else Statement Example Program in Dart


The code starts with an import statement import 'dart:io'; which allows the program to use the input-output functions in the dart:io library. The next line declares two variables age and name without initializing them. The following line prints a message on the console asking the user to enter their name and age.

The stdin.readLineSync() method is used to read user input from the console. The first call to stdin.readLineSync() reads a string value for name, while the second call reads an integer value for age. The int.parse() method is used to convert the string value of age into an integer.

The next line begins an if statement. The condition age >= 18 is checked, which evaluates to true if the user is 18 years or older. If the condition is true, the block of code inside the curly braces is executed. The code inside the block prints a message on the console saying that the user is eligible. The $name and $age are string interpolations that insert the values of name and age into the message.

If the condition is false, the code inside the else block is executed. The code inside the else block prints a message on the console saying that the user is not eligible. The $name and $age are string interpolations that insert the values of name and age into the message.

Overall, this code demonstrates how to use user input and conditional statements in Dart to make decisions based on certain conditions. Specifically, this code checks whether the user is eligible based on their age.

Source Code

import 'dart:io';
void main() {
   var age,name;
   print('Enter Your Name and Age : ');
   name=stdin.readLineSync();
   age=int.parse(stdin.readLineSync());
   if (age >= 18) 
   {
      print('$name  age is $age You are eligible');
   }
   else
   {
      print('$name age is $age You are Not eligible');
   }
}
To download raw file Click Here

Output

Enter Your Name and Age : 
Ram
20
Ram  age is 20 You are eligible