While programming we're supposed to do the same action multiple times. For handling the repetitive action we adopt loops. Technically we can term this action as Iteration.
Loops are broadly classified into two categories. they are definite and indefinite loops.
for
for..In
forEach
while
do-while
Let's discuss more regarding the loops with the below examples.
for loop is a definite loop. The execution or flow of a for loop in dart is similar to Java for loop execution. Initially, we initialize the loop, we provide the condition that we want to execute based on that we perform incremental or decremental iteration
Syntax:
for(initialization; condition; increment or decrement) {
// Code Logic
}
Eg:
void main(){
for(int i =0; i<5; i++){
print('Repetition Count = ${i}');
}
}
// Output: Repetition Count = 0
// Output: Repetition Count = 1
// Output: Repetition Count = 2
// Output: Repetition Count = 3
// Output: Repetition Count = 4
// Let's consider another example with the decremental operation
void main(){
// Simple for loop decremental
for(int i =5; i>0; i--){
print('Repetition decremental Count = ${i}');
}
}
// Output: Repetition decremental Count = 5
// Output: Repetition decremental Count = 4
// Output: Repetition decremental Count = 3
// Output: Repetition decremental Count = 2
// Output: Repetition decremental Count = 1
Now let's try another example, multiplication of a 19th table from 1 to 10 using for loop
Eg:
void main(){
// Multiplication of a 19th table
for(int i =1; i<=10; i--){
print("19 X ${i} = ${19*i} ");
}
}
Output
19 X 1 = 19
19 X 2 = 38
19 X 3 = 57
19 X 4 = 76
19 X 5 = 95
19 X 6 = 114
19 X 7 = 133
19 X 8 = 152
19 X 9 = 171
19 X 10 = 190
It is a simplified version of a traditional for loop
Let's take an example of finding an odd number from the given list
Eg:
void main(){
List<int> findPrimeNo = [2,12,31,43,56,23,82,88,96, 117];
for(int i in findPrimeNo){
if(i%2 != 0)
print('$i is an odd number');
}
}
// Output :
31 is an odd number
43 is an odd number
23 is an odd number
117 is an odd number
This is a straightforward action of for loop. with this kind of looping, we can execute a program with few lines of code
to understand clearly please go through with below examples
Eg:
void main(){
final numbers = <int>[1, 2, 3, 4, 5, 6,7, 8 ,9];
numbers.forEach(print);
}
Output :
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
Let's take a look at another example with Key-value pairs.
Eg:
void main(){
var usrMap = {"name": "Aditya", 'Email': 'test@aditya.in'};
usrMap.forEach((k,v) => print('${k}: ${v}'));
}
// Output
name: Aditya
Email: test@aditya.in
This is all about a definite loop, I hope you've learned something from the above examples. Let's go through the in-definite loop with a few more examples
While loop executes each statement till the specified conditions get true. Simply we can say, In a while loop, It evaluates the condition each time before it gets eecuted.
Eg:
void main(){
// printing numbers from 1 to 5
int i =0;
while( i<5 ){
i++;
print(i);
}
}
// Output
1
2
3
4
5
do-While this is contemporary loop to while loop. The major difference between While loop & do-while is... in do-while action will be executed first, if the action satisfies the condition in while the loop will be executed.
Let's take the same example that we implemented while loop printing numbers from 1 to 5
Eg:
void main(){
int i =0;
do{
i++;
print(i);
}while(i<5);
}
// Output
1
2
3
4
5