Dart if Statements

 

Dart if Statements

If statement allows us to a block of code execute when the given condition returns true. In Dart programming, we have a scenario where we want to execute a block of code when it satisfies the given condition. The condition evaluates Boolean values TRUE or FALSE and the decision is made based on these Boolean values.

Dart If Statement Flow Diagram

The syntax of if statement is given below.

Syntax -

  1. If (condition) {  
  2.      //statement(s)  
  3. }  

The given condition is if statement will evaluate either TRUE or FALSE, if it evaluates true then statement inside if body is executed, if it evaluates false then statement outside if block is executed.

Let's understand the following example.

Example - 1

  1. void main () {  
  2.   // define a variable which hold numeric value  
  3.  var n = 35;  
  4.    
  5.  // if statement check the given condition  
  6.  if (n<40){  
  7.     print("The number is smaller than 40")  
  8.  };  
  9. }  

Output:

The number is smaller than 40

Explanation -

In the above program, we declared an integer variable n. We specified the condition in if statement. Is the given number is smaller than 40 or not? The if statement evaluated the true, it executed the if body and printed the result.

Example - 2

  1. void main () {  
  2.   // define a variable which holds a numeric value  
  3.  var age = 16;  
  4.   
  5.  // if statement check the given condition  
  6.  if (age>18){  
  7.     print("You are eligible for voting");  
  8.  };  
  9. print("You are not eligible for voting");   
  10. }  

Output:

You are not eligible for voting

In the above program, we can see that the if condition evaluated the false then execution skipped the if body and executed the outside statement of if block

Comments