Dart Boolean data type is used to check whether a given statement true or false. The true and false are the two values of the Boolean type, which are both compile-time constants. In Dart, The numeric value 1 or 0 cannot be used to specify the true or false. The bool keyword is used to represent the Boolean value. The syntax of declaring the Boolean variable is given below.
We have declared the bool variable check that will use to verify the given expression. The expression 20>12 returned the true and we printed the result.
Dart do while loop executes a block of the statement first and then checks the condition. If the condition returns true, then the loop continues its iteration. It is similar to Dart while loop but the only difference is, in the do-while loop a block of statements inside the body of loop will execute at least once.
Dart do-while loop Flowchart
The syntax is given below.
Syntax:
do {
// loop body
}while(condition);
Here, the block of statement which is inside the do while body will executes first then evaluates the given condition.
A condition is evaluated either Boolean true or false. If it returns true, then the statements are executed again, and the condition is rechecked. If it returns false, the loop is ended, and control transfers to out of the loop.
Let’s understand the following example.
Example:
void main() {
int i = 10;
print("Dart do-while loop example");
do{
print(i);
i++;
}while(i<=20);
print("The loop is terminated");
}
Output:
Dart do-while loop example
The value of i: 10
The value of i: 11
The value of i: 12
The value of i: 13
The value of i: 14
The value of i: 15
The value of i: 16
The value of i: 17
The value of i: 18
The value of i: 19
The value of i: 20
The loop is terminated
Explanation –
In the above code, we have initialized the variable i with value 10. In do-while loop body, we defined two statements.
In the first iteration, the statement printed the initial value of i and increased by 1. Now the value of i becomes 11 and then we checked the condition.
The condition is, the value of i must be less than or greater than the 20. It matched with the condition and loop moved to the next iteration. It printed the series of numbers 10 to 20 until the condition returned false.
The while loop is used when the number of execution of a block of code is not known. It will execute as long as the condition is true. It initially checks the given condition then executes the statements that are inside the while loop. The while loop is mostly used to create an infinite loop.
Dart While Loop Flowchart
The syntax is given below.
Syntax:
while(condition){
//statement(s);
// Increment (++) or Decrement (--) Operation;
}
Here, if the condition returns the true, then the loop body is executed and condition is evaluated again. If the condition returns false then the loop is terminated and the control transfer to out of the loop.
Let’s understand the following example.
Example – 1
void main()
{
int i = 1;
while (i <= 5)
{
print( i);
++i;
}
}
Output:
1
2
3
4
5
Explanation:
Above example, we initialized the integer variable i with value 1 respectively, in the next statement we have defined while loop, that check the condition that, the value of i is smaller or greater than 5 in each iteration.
If the condition returns true then while loop body is executed and the condition is rechecked. It will be continued until the condition is false.
After that, the value of i is 6 that violated the condition; then, the loop is terminated. It printed the sequence of 1 to 5 on the console.
Infinite While Loop
When the while loop executes an endless time is called infinite while loop. Let’s have a look at the infinite loop example.
Example –
void main()
{
int i = 1;
while (i <= 5)
{
print( i);
--i;
}
}
We have made only one change in above code. We reduced the value of i for each iteration of while loop. So it will never match with the specified condition and became an infinite loop.
Example – 2
void main()
{
while (true)
{
print("Welcome to JavaTpoint");
}
}
It will print the given statement for infinite time. When we declare Boolean true in while loop, then it automatically became an infinite loop.
Logical Operator while loop
Sometimes we need to check the multiple conditions in a while loop. We can do this by using logical operators such as (||, &&, and !). Let’s see the following concepts.
while (n1<5 && n2>10) – It will execute if both conditions are true.
while (n1<5 || n2>10) – It will execute if one of the condition is true.
while(!n1 = 10) – It will execute if n1 is not equal to 10.
Consider the following example.
Example –
void main() {
int n1=1;
int n2=1;
// We are checking multiple condition by using logical operators.
while (n1 <= 4 && n2 <= 3)
{
print("n1 : ${n1}, n2: ${n2}");
n1++;
n2++;
}
}
Output:
n1 : 1, n2: 1
n1 : 2, n2: 2
n1 : 3, n2: 3
Explanation:
In the above code, we have assigned two variables n1 and n2 with the value 1 in both. Now we checked multiple conditions in the while loop where n1 is less than or equal to 4 and n2 is less than or equal to 3.
In the first iteration, it checked both values and printed the result. At one point, when the value of n1 and n2 is equal to 4. The n1 satisfied the condition one, but n2 did not meet the second condition, so the loop is terminated and printed the result to the screen.
The for..in loop is similar to for loop but different in its syntax. It iterates through an object’s properties. The Dart for..in loop accepts an expression as iterator and iterates through the elements one at a time in sequence. The variable var holds the values of the iteration. The for…in will execute until elements remain in iterators.
Dart For In Loop Flow Diagram
The syntax is given below.
Syntax:
for (var in expression) {
//statement(s)
}
Let’s understand the following example.
Example –
void main() {
var list1 = [10,20,30,40,50];
print("Dart for..in loop Example");
for(var i in list1) {
print(i);
}
}
Output:
Dart for..in loop Example
10
20
30
40
50
Explanation:
In the above program, we have iterator list1 and variable i. In the first iteration of the loop, the first element of the list assigned to the variable i. This process happened again, and the second element of the list assigned to i. It will be continued until there is no element left in the list. It printed the all element of the list to the console.
The for..in loop is suitable to iterate over the Dart object such as list, map, and set, where for loop is more effective to iterate along with the specified condition.
Let’s understand another example.
Example –
void main() {
var list1 = [10,20,30,40,50];
// create an integer variable
int sum = 0;
print("Dart for..in loop Example");
for(var i in list1) {
// Each element of iterator and added to sum variable.
sum = i+ sum;
}
print("The sum is : ${sum}");
}
Output:
Dart for..in loop Example
The sum is : 150
Explanation:
In the above example, we have declared a variable sum with value 0. We have a for..in loop with the iterator list, and each element of list added to the sum variable after each iteration.
In the first iteration, the value of the sum is equal to 10. In the next iteration, the value of sum became 30 after added 20 to it. After complete the iteration, it returned the sum of all elements of the list.
Dart for loop is used when we familiar with the number of execution of a block of code. It is similar to the C, C++, and Java for loop. It takes an initial variable to start the loop execution. It executes a block of code until it matches the specified condition. When the loop is executed, the value of iterator is updated each iteration, and then the test-expression is evaluated. This process will continue until the given test-expression is true. Once the test-expression is false, the for loop is terminated.
Dart for Loop Flowchart
Syntax:
for(initialization, condition, incr/decr)
The initialization is used as an initial value in a loop, and it executes only once.
A condition or test-expression returns Boolean values – True or False. The for loop will execute until the condition is true.
Once the condition evaluates false, the loop is ended.
The incr/decr is counter to increase or decrease the variable.
Let’s understand the following example.
Example 1:
void main() {
//for loop iteration
for(int i = 1; i < =10;i++)
{
print(i);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Explanation:
In the above example, we have initialized an integer variable i as initial value. We have assigned 1 to the variable and in the conditional part, we have defined the loop executed until value of i is smaller or equal to 10. Each time the loop will be iterated it will be increased value by 1.
In the first iteration of a loop, the value of i is incremented by 1 and it will become 2. Now the condition is rechecked if condition is true, then the loop will be moved on next iteration. The iteration of the loop will continued until the value becomes 10.
We can skip the initial value from the for loop. Consider the following example.
void main() {
var i = 1;
//for loop iteration skipping the initial value from for loop
for(; i < =10;i++)
{
print(i);
}
}
It will give the same output as previous code.
Also, we can skip the condition, increment, or decrement by using a semicolon.
Nested for Loop
The nested for loop means, “the for loop inside another for loop”. A for inside another loop is called an inner loop and outside loop is called the outer loop. In each iteration of the outer loop, the inner loop will iterate to entire its cycle. Let’s understand the following example of nested for loop.
Example –
void main()
{
int i, j;
int table_no = 2;
int max_no = 10;
for (i = 1; i <= table_no; i++) { // outer loop
for (j = 0; j <= max_no; j++) { // inner loop
print("${i} * ${j} = ${i*j}");
//print("\n"); /* blank line between tables */
}}
}
Dart Loop is used to run a block of code repetitively for a given number of times or until matches the specified condition. Loops are essential tools for any programming language. It is used to iterate the Dart iterable such as list, map, etc. and perform operations for multiple times. A loop can have two parts – a body of the loop and control statements. The main objective of the loop is to run the code multiple times. Dart supports the following type of loops.
Dart for loop
Dart for…in loop
Dart while loop
Dart do-while loop
We describe a brief introduction to the dart loops as follows.
Dart for loop
The for loop is used when we know how many times a block of code will execute. It is quite same as the C for loop. The syntax is given below.
Syntax –
for(Initialization; condition; incr/decr) {
// loop body
}
The loop iteration starts from the initial value. It executes only once.
The condition is a test-expression and it is checked after each iteration. The for loop will execute until false returned by the given condition.
The incr/decr is the counter to increase or decrease the value.
Let’s understand the following example.
Example –
void main()
{
int num = 1;
for(num; num<=10; num++) //for loop to print 1-10 numbers
{
print(num); //to print the number
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Dart for… in Loop
The for…in loop is slightly different from the for loop. It only takes dart object or expression as an iterator and iterates the element one at a time. The value of the element is bound to var, which is and valid and available for the loop body. The loop will execute until no element left in the iterator. The syntax is given below.
Syntax –
for (var in expression) {
//statement(s)
}
Example :
void main()
{
var list1 = [10,20,30,40,50];
for(var i in list1) //for..in loop to print list element
{
print(i); //to print the number
}
}
Output:
10
20
30
40
50
We need to declare the iterator variable to get the element from the iterator.
Dart while loop
The while loop executes a block of code until the given expression is false. It is more beneficial when we don’t know the number of execution. The syntax is given below.
Syntax:
while(condition) {
// loop body
}
Let’s understand the following example.
Example –
void main()
{
var a = 1;
var maxnum = 10;
while(a<maxnum){ // it will print until the expression return false
print(a);
a = a+1; // increase value 1 after each iteration
}
}
Output:
1
2
3
4
5
6
7
8
9
Dart do…while Loop
The do…while loop is similar to the while loop but only difference is that, it executes the loop statement and then check the given condition. The syntax is given below.
Syntax –
do {
// loop body
} while(condition);
Example –
void main()
{
var a = 1;
var maxnum = 10;
do
{
print("The value is: ${a}");
a = a+1;
}while(a<maxnum);
}
Output:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
The value is: 6
The value is: 7
The value is: 8
The value is: 9
Selection of the loop
The selection of a loop is a little bit difficult task to the programmer. It is hard to decide which loop will be more suitable to perform a specific task. We can determine the loop based on the following points.
Analyze the problem and observe that whether you need a pre-test loop or post-test loop. A pre-test loop is that, the condition is tested before entering the loop. In the post-test loop, the condition is tested after entering the loop.
If we require a pre-test loop, then select the while or for loop.
If we require a post-test loop, then select the do-while loop.
Dart Switch case statement is used to avoid the long chain of the if-else statement. It is the simplified form of nested if-else statement. The value of the variable compares with the multiple cases, and if a match is found, then it executes a block of statement associated with that particular case.
The assigned value is compared with each case until the match is found. Once the match found, it identifies the block of code to be executed.
Dart Switch Case Statement Flowchart
The syntax is given below.
Syntax:
switch( expression )
{
case value-1:{
//statement(s)
Block-1;
}
break;
case value-2:{
//statement(s)
Block-2;
}
break;
case value-N:{
//statement(s)
Block-N;
}
break;
default: {
//statement(s);
}
}
Here, the expression can be integer expression or character expression. The value 1, 2, n represents the case labels and they are used to identify each case particularly. Each label must be ended with the colon(:).
The labels must be unique because same name label will create the problem while running the program.
A block is associated with the case label. Block is nothing but a group of multiple statements for a particular case.
Once the switch expression is evaluated, the expression value is compared with all cases which we have defined inside the switch case. Suppose the value of the expression is 2, then compared with each case until it found the label 2 in the program.
The break statement is essential to use at the end of each case. If we do not put the break statement, then even the specific case is found, it will execute all the cases until the program end is reached. The break keyword is used to declare the break statement.
Sometimes the value of the expression is not matched with any of the cases; then the default case will be executed. It is optional to write in the program.
Let’s understand the following example.
void main() {
int n = 3;
switch (n) {
case 1:
print("Value is 1");
break;
case 2:
print("Value is 2");
break;
case 3:
print("Value is 3");
break;
case 4:
print("Value is 4");
break;
default:
print("Out of range");
break;
}
}
Output:
Value is 3
Explanation –
In the above program, we have initialized the variable n with value 3. We constructed the switch case with the expression, which is used to compare the each case with the variable n. Since the value is 3 then it will execute the case-label 3. If found successfully case-label 3, and printed the result on the screen.
Let’s have a look at another scenario.
Example –
void main()
{
// declaring a interger variable
int Roll_num = 90014;
// Evalaute the test-expression to find the match
switch (Roll_num) {
case 90009:
print("My name is Joseph");
break;
case 90010:
print("My name is Peter");
break;
case 090011:
print("My name is Devansh");
break;
// default block
default:
print("Roll number is not found");
}
}
Output:
Roll number is not found
Explanation –
In the above program, we have initialized the variable Roll_num with the value of 90014. The switch test-expression checked all cases which are declared inside the switch statement. The test-expression did not found the match in cases; then it printed the default case statement.
Benefit of Switch case
As we discussed above, the switch case is a simplified form of if nested if-else statement. The problem with the nested if-else, it creates complexity in the program when the multiple paths increase. The switch case reduces the complexity of the program. It enhances the readability of the program.
Dart if else-if statement provides the facility to check a set of test expressions and execute the different statements. It is used when we have to make a decision from more than two possibilities.
Here, this type of structure is also known as the else….if ladder. The condition is evaluated from top to bottom. Whenever it found the true condition, statement associated with that condition is executed. When all the given condition evaluates false, then the else block is executed.
Let’s understand the following example.
Example – Write a program to print the result based on the student’s marks.
The above program prints the result based on the marks scored in the test. We have used if else if to print the result. We have initialized the marks variable with the integer value 74. We have checked the multiple conditions in the program.
The marks will be checked with the first condition since it is false, and then it moved to check the second condition.
It compared with the second condition and found true, then it printed the output on the screen.
This process will continue until all expression is evaluated; otherwise the control will transfer out of the else if ladder and default statement is printed.
You should modify the above value and notice the result.
Nested If else Statement
Dart nested if else statement means one if-else inside another one. It is beneficial when we need a series of decisions. Let’s understand the following example.
Example – Write a program to find the greatest number.
void main() {
var a = 10;
var b = 20;
var c = 30;
if (a>b){
if(a>c){
print("a is greater");
}
else{
print("c is greater");
}
}
else if (b>c){
print("b is greater");
}
else {
print("c is greater");
}
}
Output:
C is greater
In the above program, we have declared three variables a, b, and c with the values 10, 20, and 30. In the outer if-else we provided the condition it checks if a is greater than b. If the condition is true then it will execute the inner block otherwise the outer block will be executed.
In the inner block we have another condition that checks if variable a is greater than c. If the condition is evaluated true then the inner block will be executed.
Our program returned the false in first condition, and then it skipped the inner block check another condition. If satisfied the condition and printed the output on the screen.
If statement allows us to a block of code execute when the given condition returns true. In Dart programming, we have a scenario where we want to execute a block of code when it satisfies the given condition. The condition evaluates Boolean values TRUE or FALSE and the decision is made based on these Boolean values.
Dart If Statement Flow Diagram
The syntax of if statement is given below.
Syntax –
If (condition) {
//statement(s)
}
The given condition is if statement will evaluate either TRUE or FALSE, if it evaluates true then statement inside if body is executed, if it evaluates false then statement outside if block is executed.
Let’s understand the following example.
Example – 1
void main () {
// define a variable which hold numeric value
var n = 35;
// if statement check the given condition
if (n<40){
print("The number is smaller than 40")
};
}
Output:
The number is smaller than 40
Explanation –
In the above program, we declared an integer variable n. We specified the condition in if statement. Is the given number is smaller than 40 or not? The if statement evaluated the true, it executed the if body and printed the result.
Example – 2
void main () {
// define a variable which holds a numeric value
var age = 16;
// if statement check the given condition
if (age>18){
print("You are eligible for voting");
};
print("You are not eligible for voting");
}
Output:
You are not eligible for voting
In the above program, we can see that the if condition evaluated the false then execution skipped the if body and executed the outside statement of if block.
The control statements or flow of control statements are used to control the flow of Dart program. These statements are very important in any programming languages to decide whether other statement will be executed or not. The code statement generally runs in the sequential manner. We may require executing or skipping some group of statements based on the given condition, jumps to another statement, or repeat the execution of the statements.
In Dart, control statement allows to smooth flow of the program. By using the control flow statements, a Dart program can be altered, redirected, or repeated based on the application logic.
Categories of Flow Statement
In Dart, Control flow statement can be categorized mainly in three following ways.
Decision-making statements
Looping statements
Jump statements
Dart Decision-Making Statements
The Decision-making statements allow us to determine which statement to execute based on the test expression at runtime. Decision-making statements are also known as the Selection statements. In Dart program, single or multiple test expression (or condition) can be existed, which evaluates Boolean TRUE and FALSE. These results of the expression/condition helps to decide which block of statement (s) will execute if the given condition is TRUE or FALSE.
Dart provides following types of Decision-making statement.
If Statement
If-else Statements
If else if Statement
Switch Case Statement
Dart Looping Statements
Dart looping statements are used to execute the block of code multiple-times for the given number of time until it matches the given condition. These statements are also called Iteration statement.
Dart provides following types of the looping statements.
Dart for loop
Dart for….in loop
Dart while loop
Dart do while loop
Dart Jump Statements
Jump statements are used to jump from another statement, or we can say that it transfers the execution to another statement from the current statement.
Dart provides following types of jump statements –