What are decision-making statements in Dart programming?
Decision-making statements allow programmers to choose which statement should be executed under different conditions. At runtime, the decision-making statements determine which statement to execute based on the test expression.
There are four types:
ifstatementif-elsestatementif else ifstatementswitch casestatement
if statement
The if statement checks the given condition and if it returns true, the block of code is executed. If not, it disregards the block of code.
Syntax:
if ( condition ){
// statement(s) to be executed
}
void main(){int a = 8;// Condition is trueif (a > 5) {print(" $a is greater than 5");}}
if-else statement
The if-else statement tests the condition and, if it is true, it executes the if-block. But if it is not, the else block is executed.
Syntax:
if ( condition ){
// body of if
}
else {
// body of else
}
The if-else flow diagram
void main(){int a= 15;if (a > 10) {// This will be printedprint("The condition is true");}else {// This will not be printedprint("The condition is false");}}
if else if statement
The if else if statement examines the condition and, if it is true, executes the statements within it. If it is not, other if conditions are examined, and if they are true, they are executed.
It’s useful when we have to choose between more than two options.
Syntax:
if ( condition1 ){
// body of if
}
else if ( condition2 ){
// body of if
}
else if ( conditionN ){
// body of if
}
.
.
else {
// statement
}
The if else if flow diagram
void main(){int a = 15;int b = 5;if (a < b) {print("Condition 1 is true");}else if (a > b) {print("Condition 2 is true");}else if (a == b) {print("Condition 3 is true");}else {print("All the conditions are false");}}
switch case statement
The switch case statement is a simplified form of a nested if-else statement. The switch case statement is used to eliminate the if-else statement’s long chain. The variable’s value is compared to the multiple cases, and if a match is found, a block of statements associated with that case is executed.
Syntax:
switch( expression )
{
case value1:{
// statement(s)
}
break;
case value2:{
// statement(s)
}
break;
case valueN:{
// statement(s)
}
break;
default: {
//statement(s);
}
}