Decision Making Statements in Dart

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:

  1. if Statement
  2. if….else Statement
  3. else if Ladder
  4. switch…case Statement

Let’s see in detail:

  1. if Statement

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

Syntax:

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:

else{

}

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 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:

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!

.NET Developer and Flutter Enthusiast | Coach and Trainer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store