Conditionals are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined boolean condition evaluates to true or false. Conditions must be expressed in such way that its result can only be true or false, like formulating a Yes or No question. This can be done using comparison operators, like equals (=), greater than (>), less than (<)..

If the specified condition returns true as a result, then the program would execute some group of instructions, but in the opposite case, when it returns false the program would execute another instruction.

To make it more clear, let’s give you an example; Imagine you have a little boy and you want to teach him how cross a street with traffic lights. You must tell him about the conditions that are necessary to be met in order to cross the street, those conditions would be :

  • If the green light is on, you must stop, don’t cross, only cars can pass now.
  • If the yellow light is on, continue waiting until the red one becomes on.
  • If the red light is on, you can walk now.

There are four conditional constructs in Dart : If, if-else, else-if and switch-case.

If statement

The if construct evaluates a condition before a block of code is executed.

If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed. If Boolean expression evaluates to be false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

The following illustration shows how the if statement works.

Example

The output

If else statement

If statement is used to check the specified condition and if it is correct if block statements is executed and if false execute the else block statements.

If the Boolean expression evaluates to be true, then the if block of code will be executed, otherwise else block of code will be executed.

The following illustration shows how the if-else statement works.

Example

The output

Else if statement

The else-if statement is useful to test multiple conditions.

Below, the if statement is used to check the specified condition and if it is correct if block statements is executed, and if false it checks the else-if condition if it is correct it execute the else-if block statements, if there is an else-if statement again the process goes on and if no conditions are satisfied the else block will be executed.

When using else-if statements, there are a few points to keep in mind.

  • An if can have zero or one else’s and it must come after any else-if’s.
  • An if can have zero to many else-if’s and they must come before the else.
  • Once an else-if succeeds, none of the remaining else-if’s or else’s will be executed or tested.

The following illustration shows how the else-if statement works.

Example

The output

Switch statement

The switch statement is a multi-directional statement. It works almost exactly like the if-else statement. The difference is that the switch statement produces a more readable code in comparison to if-else statement. Also, sometimes it executes faster than the if-else.

The value of the variable expression is tested against all cases in the switch. If the variable matches one of the cases, the related code block is executed. If no case expression matches the value of the variable expression, the code within the default block is executed.

Here are the rules that apply to switch statement :

  • There can be any number of case statements inside a switch.
  • The case statements can include only constants. It cannot be a variable or an expression.
  • The data type of the variable expression and the constant expression must match.
  • Unless you put a break after each block of code, the execution flows into the next block.
  • The default block is optional.

The following illustration shows how the switch statement works.

Example

The output

Thank you for reading.

Follow me on Instagram and Twitter.

Photographer | story teller | CS student

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