Else If 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 four variables pre, cur, amt, and unit without initializing them. The following line prints a message on the console asking the user to enter the previous unit consumed.

The stdin.readLineSync() method is used to read user input from the console. The call to stdin.readLineSync() reads a string value for pre, which is then converted into an integer using the int.parse() method. The next line prints a message on the console asking the user to enter the current unit consumed.

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

The next line calculates the unit consumed by subtracting the previous unit from the current unit. The following lines begin an if-else statement to determine the amount the user needs to pay based on the number of units consumed. If the unit consumed is 0, the block of code inside the first if statement is executed, which prints a message on the console saying that the user needs to pay Rs 50.

If the unit consumed is between 1 and 100 (inclusive), the block of code inside the first else if statement is executed, which calculates the amount to be paid by multiplying the unit by 0.50 and prints a message on the console indicating the amount to be paid.

If the unit consumed is between 101 and 200 (inclusive), the block of code inside the second else if statement is executed, which calculates the amount to be paid by multiplying the unit by 1.50 and prints a message on the console indicating the amount to be paid.

If the unit consumed is greater than 200, the block of code inside the else statement is executed, which calculates the amount to be paid by multiplying the unit by 3.00 and prints a message on the console indicating the amount to be paid.

Overall, this code demonstrates how to use conditional statements in Dart to calculate and display the electricity bill amount based on the units consumed.

Source Code

import 'dart:io';
void main() {
   var pre,cur,amt,unit;
   print('Enter Pervious Unit : ');
   pre=int.parse(stdin.readLineSync());
   print('Enter Current Unit : ');
   cur=int.parse(stdin.readLineSync());
   unit=cur-pre;
   if(unit==0)
    {
       print('You Need to pay Rs: 50 ');
    }
	else if(unit>=1 && unit<=100)
	{
       amt=unit*0.50;
       print('You Need to pay Rs: $amt ');
   }
   else if(unit>=101 && unit<=200)
   {
       amt=unit*1.50;
       print('You Need to pay Rs: $amt ');
   }
   else
   {
       amt=unit*3.00;
       print('You Need to pay Rs: $amt ');
   }

}
To download raw file Click Here

Output

Enter Pervious Unit : 
20
Enter Current Unit : 
30
You Need to pay Rs: 5.0