Add Your Heading Text Here

Looping is the statements written in sequence which are performed continuously until the desired condition is satisfied.

Looping is the concept used for code reuse and improve the efficiency by not writing the same code many times.

In short, looping is generally used to perform some operation or specific block of code repeatedly until specific condition satisfies.

Dart supports the following looping structures.

  1. do-while loop
  2. while loop
  3. for loop
  4. for each loop

All the loops are used for performing operations iteratively. But, they all differ from each other with syntax, condition checking as well as performance.

The conditional expression for any looping must have boolean values i.e., either true or false.

Do-while loop

Do while loop is a loop which executes at least once. After that it evaluates the boolean conditional expression for further iteration of the loop.

Do while loop is generally used when you want to iterate your loop at lease once. Do-while loop will execute at lease once even if it doesn’t satisfies the boolean expression.

In do-while loop, boolean expression comes at the end of the loop. That’s why the loop or block of code gets executed before the boolean expression evaluation.

You must have to pass a condition expression in a do-while loop.

Do and while are the keywords required to use do-while loop.

Syntax :

do{

// block of code or statements

}while(conditional_expression); // conditional_expression must have value of type boolean

For ex :

void main() {
int i=1;
do{
print(“I am the best”);
i++;
}while(i<=2);
}

Output :

I am the best

I am the best

To make a do-while loop infinite pass an expression whose value is always true and you don’t change it any way.

For ex:

void main(){
	do{
	    print(“Infinite loop”);
	}while(2>1); // 2>1 is always true. So, it will iterate for infinite time.So,it will hang your system and your UI will not respond.
}

while loop

While loop is generally used when you don’t know or unsure about the iterations required for the looping.

While loop is executed only if it satisfies the conditional expression.

You must have to pass a condition expression in a while loop.

While is the keyword required to use while loop.

Syntax :

// conditional expression must have value of type boolean.

while(conditional_expression){

// block of code

}

Similarly, to make a while loop infinite pass an expression whose value is always true and you don’t change it any way.

For ex :

void main(){
	// the value of “true” will always be true. Hence, this while loop will iterate infinitely.
		while(true){
		print(“Infinite loop”);
	}
}

for loop

It is the most used loop out of all.

It is generally used when you are sure about the iterations required for the looping.

For is the keyword required to use for loop.

A conventional for loop consists of four parts.

Initialisation

Initialisation is executed only once. it is the set of statements used for initialising different variables.

You can write multiple initialisations for different variables.

Conditional Expression

Conditional expression is tested each time and used to check whether particular loop will iterate further or not. In short, if conditional expression evaluates to true then loop will executed else the loop will break and jump to the next statement after the for loop.

Block of code

It is the main body of the for loop. If Conditional expression evaluates to true then control flow will move inside the for loop where statements(block of codes) are written will be executed.

Increment/Decrement

It is executed every time after the block of code or statements executed.

It is used to increment or decrement any loop control variables.

You can have multiple increment/decrements in one for loop.

Syntax :

// the value of conditional expression must be true to enter inside for loop.

for(Initialisation;Conditional_Expression;Increment/Decrement){

// block of code
}

For ex :

void main(){
  for(int i=0,j=2;i<5 && j<5 ; i++,j++){
    print(i);
  }
}

Output :

0123

If you don’t pass any values for initialisation, conditional expression and increment/decrement it will turn in to infinite for loop and your UI will be stuck.

For ex :

// Also passing a true value in conditional expression which never changes will also turn your loop in to infinite loop same as while and do-while loop.
for(;;){
	print(“Hello friendsssss !!”);
} 

for-in loop

For-in loop is the enhanced form of the for loop.

It just have different syntax structure.

For and in are the keywords required to use for-in loop.

It is used to traverse or iterate different collections(like, array-list) easily.

Syntax :

// dataType is the type of the collection values stored.

for(dataType variableName in collection){

// block of code

}

For ex :

void main(){
// List is the type of the collection
  List<String> companies=["Google","Amazon","Apple","Microsoft"];

	// here String is the dataType of values stored inside collection companies.
  for(String companyName in companies){
    print(companyName);
  }
}

Output :

Google
Amazon
Apple
Microsoft

In this way, we can easily achieve our goal of not rewriting the same code many time and improve the efficiency and performance of the developer in easiest way.

It is better if you have a command over looping concept as it is almost used in each and every programming languages and programs.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons