Dart If Else Statement – Multiple Conditions and Exercises

2
25248
if else statement in dart
if else conditions in dart

The if else statement/condition holds the basic flow of a program. Only a particular part of the program is executed when the if statement turns out to be true. Dart if else statement checks a condition in the if part and executes the program based on the result of the condition. If condition holds true then the program executes the if part otherwise programs execute the else part.

The syntax of if else statement in Dart looks like this.

  
 if(condition){
       //Execute this part of the code if the condition is true.
 }else{
      //Execute this part of the code if the condition is not true.
 }
   

Considering the Dart syntax for if else code let’s take a real example.

void main() {
if (100 > 50) {
  print("100 is greater than 50");
 }
}

As we knew 100 is great than 50 means this given condition is true and we will get the following output as an answer.

100 is greater than 50

Speaking of the condition we have used greater (>) sign condition. Dart supports all the typical logical conditions from mathematics. Let’s say we have two int variables a and b. Here are some of the very common mathematical conditions.

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Eqdual to: a != b

We can use these conditional statements to perform different action.

Dart offers various condition statements.

  • if Given condition holds true then the defined if code block is executed.
  • else code block to be executed if the same condition is false
  • else if when there are more conditions and anyone of them can be true. Let’s say the first condition is false and we want to test the next condition.
  • Nested if and else statement. Based on the first condition. If the first condition is true then to test the next condition after the first condition is true if that could also be true or false.

Time to show every conditional statement in action.

Dart If else statement

void main() {
  
  var a=100;
  var b=50;
if (a > b) {
  print('a is greater than b');
 }
 else{
   print('a is smaller than b');
 }
}
a is greater than b

Lets swap the values of a and b by keeping the same condition.

void main() {
  
  var a=50;
  var b=100;
if (a > b) {
  print('a is greater than b');
 }
 else{
   print('a is smaller than b');
 }
}
a is smaller than b

Dart else if statement

Considering the score a relevant grade will be printed on console.

void main() {
  
 var score = 82;
        var grade;

        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else if (score >= 70) {
            grade = 'C';
        } else if (score >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        print ('Your grade is $grade');
}

The grade is

Your grade is B

Nested if else statement

This time we have to check if a course is offered in summer or winter. It is also possible the course get offered in summer and winter.

void main() {
  bool offeredWinter = true;
  bool offeredSummer = true;
  if (offeredWinter) {
    if (offeredSummer) {
      print('The course is offered in winter and summer');
    }
  }
}

Output on the console.

The course is offered in winter and summer

Single line if statement (Ternary Operator)

If the time of the day is below 11 AM. Code will print Good Morning as a result otherwise Good Day.

void main() {
  var time = 8;
  String result = time < 11 ? "Good Morning." : "Good Day.";
  print(result);
}

Here is the actual output

Good Morning

Switch in Dart is also conditional code which can execute many alternative blocks of the code. It is advised to go through switch tutorial as well.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here