Control Flow Statements in Dart
Control Flow Statements are used to control the flow of execution of the program. Control Flow Statements are used to alter the flow of the program based on certain conditions.
Types of Control Flow Statements
There are three types of Control Flow Statements in Dart:
- Decision Making Statements
- Looping Statements
- Jump Statements
Decision Making Statements
Decision Making Statements are used to make decisions based on certain conditions. There are three types of Decision Making Statements in Dart:
- if Statement
- if-else Statement
- if-else-if Ladder
if Statement
if Statement is used to execute a block of code if a certain condition is true. The syntax of if Statement is:
if(condition){
//statements to be executed if the condition is true
}
if-else Statement
if-else Statement is used to execute a block of code if a certain condition is true and another block of code if the condition is false. The syntax of if-else Statement is:
if(condition){
//statements to be executed if the condition is true
}
else{
//statements to be executed if the condition is false
}
if-else-if Ladder
if-else-if Ladder is used to execute a block of code if a certain condition is true and another block of code if the condition is false. There can be multiple else-if blocks in an if-else-if Ladder. The syntax of if-else-if Ladder is:
if(condition1){
//statements to be executed if the condition1 is true
}
else if(condition2){
//statements to be executed if the condition2 is true
}
else if(condition3){
//statements to be executed if the condition3 is true
}
else{
//statements to be executed if all the conditions are false
}
Looping Statements
Looping Statements are used to execute a block of code repeatedly. There are three types of Looping Statements in Dart:
- for Loop
- while Loop
- do-while Loop
for Loop
for Loop is used to execute a block of code repeatedly for a fixed number of times. The syntax of for Loop is:
for(initialization; condition; updation){
//statements to be executed repeatedly
}
while Loop
while Loop is used to execute a block of code repeatedly for an unknown number of times. The syntax of while Loop is:
while(condition){
//statements to be executed repeatedly
}
do-while Loop
do-while Loop is used to execute a block of code repeatedly for an unknown number of times. The syntax of do-while Loop is:
do{
//statements to be executed repeatedly
}while(condition);
Jump Statements
Jump Statements are used to alter the flow of the program. There are three types of Jump Statements in Dart:
- break Statement
- continue Statement
- return Statement
break Statement
break Statement is used to terminate the loop or switch statement. The syntax of break Statement is:
break;
continue Statement
continue Statement is used to skip the current iteration of the loop. The syntax of continue Statement is:
continue;
return Statement
return Statement is used to return a value from a function. The syntax of return Statement is:
return value;
Example
void main(){
//if Statement
int a = 10;
if(a > 5){
print("a is greater than 5");
}
//if-else Statement
int b = 10;
if(b > 5){
print("b is greater than 5");
}
else{
print("b is less than or equal to 5");
}
//if-else-if Ladder
int c = 10;
if(c > 5){
print("c is greater than 5");
}
else if(c > 3){
print("c is greater than 3");
}
else{
print("c is less than or equal to 3");
}
//for Loop
for(int i = 0; i < 5; i++){
print(i);
}
//while Loop
int j = 0;
while(j < 5){
print(j);
j++;
}
//do-while Loop
int k = 0;
do{
print(k);
k++;
}while(k < 5);
//break Statement
for(int i = 0; i < 5; i++){
if(i == 3){
break;
}
print(i);
}
//continue Statement
for(int i = 0; i < 5; i++){
if(i == 3){
continue;
}
print(i);
}
//return Statement
int sum = add(5, 10);
print(sum);
}
int add(int a, int b){
return a + b;
}
Output
a is greater than 5
b is greater than 5
c is greater than 5
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
0
1
2
4
15
Conclusion
In this tutorial, we learned about Control Flow Statements in Dart. In the next tutorial, we will learn about Functions in Dart.
Quiz
-
Which of the following is not a Decision Making Statement in Dart?
a) if Statement
b) if-else Statement
c) if-else-if Ladder
d) for Loop
-
Which of the following is not a Looping Statement in Dart?
a) for Loop
b) while Loop
c) do-while Loop
d) if-else-if Ladder
-
Which of the following is not a Jump Statement in Dart?
a) break Statement
b) continue Statement
c) return Statement
d) if-else-if Ladder
-
What will be the output of the following Dart code?
void main(){ int a = 10; if(a > 5){ print("a is greater than 5"); } else{ print("a is less than or equal to 5"); } }
a) a is greater than 5
b) a is less than or equal to 5
c) a is greater than 5 a is less than or equal to 5
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ int a = 10; if(a > 5){ print("a is greater than 5"); } else if(a > 3){ print("a is greater than 3"); } else{ print("a is less than or equal to 3"); } }
a) a is greater than 5
b) a is greater than 3
c) a is less than or equal to 3
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ for(int i = 0; i < 5; i++){ print(i); } }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ int i = 0; while(i < 5){ print(i); i++; } }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ int i = 0; do{ print(i); i++; }while(i < 5); }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ for(int i = 0; i < 5; i++){ if(i == 3){ break; } print(i); } }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compilation Error
-
What will be the output of the following Dart code?
void main(){ for(int i = 0; i < 5; i++){ if(i == 3){ continue; } print(i); } }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compilation Error