IF-Else Statement and Ternary Operator Dart

Mercy Jemosop
2 min readJun 24, 2021

--

IF Statement

If-Else is a conditional statement used to execute boolean expressions(true, false). This conditional statement is divided into two parts, If condition- which is executed when the condition evaluates to true and Else- which is executed when the condition is false.

If-Else syntax:

if (Condition) {action to be executed if condition is true}

else {action to be executed if condition is false}

Example:

In the example below, the condition checks if a person is eligible to vote based on age, if age is equal or greater than 18, the condition will be true and user will be notified that they are eligible to vote. if the condition evaluates to false: user has not attained the minimum age, they are notified that they are underage hence not eligible to vote.

Dart Conditional Operators ( ? : )

Dart Ternary Operators are conditional operators used to shorten an if-else statement. The advantage of using a ternary operator is that it allows you to replace a simple if-else statements with a single line expression which is easy to read.

Conditional Operator Syntax:

condition ? action to be executed if condition is true : action to be executed if condition is false

Example:

ternary operator example

--

--

Mercy Jemosop
Mercy Jemosop

Written by Mercy Jemosop

Software Developer. I am open to job referrals. connect with me on twitter @kipyegon_mercy

No responses yet