Decision Making Statements in Dart
By: Yogita Kumar
Tags: Flutter Widget, Flutter Ui, Flutter, Dart, Flutter App Development

In situations, where instructions needs to be executed based on certain conditions, these conditional statements are known as decision making statement.

There are 4 types of decision making statements available in dart:

if Statement if….else Statement else if Ladder switch…case Statement

Let’s see in detail:

if Statement

it evaluates the condition and execute block of code, if condition is true

Syntax:

if(condition){ //block of code } //Next instruction

condition in if statement always return boolean value true/false.

If condition result is true then block of instruction is executed and then next instruction after if block.

And if condition result is false then immediate next instruction executed. This time control skips the instructions inside if block.

Flowchart for If statement

Example for if statement

2. if…else Statement

If statement followed by optional else block. When if condition return false result, statements in else blocks executed.

Syntax:

if(condition){ //1st Block of instructions }

else{

//2nd Block of instructions

}

//Next instruction

As mentioned earlier if statement always returns boolean value true/false.

If statement returns true, 1st Block of instructions will be executed.

If statement returns false, else statement, 2nd Block of instructions will be executed.

Flowchart for if…..else statement

Example for if…else statement

3. else….if ladder

else….if ladder comes into picture when there is situation to check multiple condition.

Syntax:

if(condition_1) { //1st block of instructions } else if(condition_2) { //2nd block of instructions } ………. else if(condition_n) { //nth block of instructions } else { //if all above conditions false then execute this block }

if condition_1 returns false, then second else if condition get evaluated.

If condition_2 returns false, then third else if condition get evaluated.

If any of else if condition returns true, then that respective blocks of instructions get executed.

and if condition or all else if condition returns false, then only else block is executed.

Always remember, if statement must be first statement, no else if statement after else, else is optional, any of else if returns true then that respective block executed others are discarded.

Flow chart for else if ladder

Example for else if ladder

4. Switch…case statement

switch…case statement evaluate the condition and result of that condition matches with case values and that respective block of instruction gets executed.

Syntax:

switch(condition) { case(value1): { //1st block of instructions }break; case(value2) { //2nd block of instructions }break; default: { //Default statements for execution }break; }

Here you can write n number of case statements.

Missing of break statement at the end of each case statement gives an error message Switch case may fall through to the next case. Fall through means control executes all consecutive case statements until it finds next break statement or end of switch statement.

Flowchart for switch…case statement

Example for switch…case statement

here opr string value matches with case value, and that respective block is executed.

Thank you!