WHAT IS A LOOP?
You may find yourself in a position where you need to run a block of code multiple times. Statements are typically executed in order: the first statement in a function is executed first, then the second, and so on.
Different control structures are available in programming languages, allowing for more sophisticated execution routes.
A loop statement allows us to repeat the execution of a statement or a set of statements, and the typical structure of a loop statement in most programming languages is as follows:
for loop | The code that maintains the loop variable is abbreviated by executing a sequence of instructions many times. |
while loop | While a given condition is true, it repeats a statement or a series of statements. Before performing the loop body, it checks the condition. |
do while loop | It’s similar to a while statement, but it checks the condition at the end of the loop body. |
for loop
The syntax of for loop has three parts:
Initialization: When the loop begins, the initial condition is executed once. We can either initialize the variable or use one that has already been initialized. It’s a condition that can be turned off.
Condition: This is the second condition, which is run every time the loop is tested. It keeps running as long as the condition is true. It must return either true or false as a boolean value. It’s a condition that can be turned off.
Increment/Decrement: It increases or decreases the value of the variable. It’s a condition that can be turned off.
Statement: The loop’s statement is executed each time the second condition is false.
for(initialization; condition; increment/decrement) { //statement or code to be executed }
A simple program to demonstrate for loop:
A program to print first twenty natural numbers.
class SimpleProgram { public static void main(String[] args) { for(int i=1;i<=20;i++) { System.out.println(i); } } }
TYPES OF for LOOP:
Nested for loop:
The term “nested for loop” refers to a for loop that is contained within another loop. When the outer loop executes, the inner loop completes.
Program example:
WAP to print the pattern given below:
* ** *** **** *****
class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i<=term;i++) { for(int j=term;j>=i;j--) { System.out.print("* "); } System.out.println();//new line } } }
Labeled for loop:
Each Java for loop can be given a name. We do this by placing label before the for loop. It comes in handy when utilizing the nested for loop because it allows us to break/continue individual for loops.
Syntax:
labelname: for(initialization; condition; increment/decrement) { }
class Example { public static void main(String[] args) { aa: for(int i=1;i<=5;i++){ bb: for(int j=1;j<=5;j++) { if(i==3&&j==3){ break aa; } System.out.println(i+" "+j); } } } }
Infinitive for loop
no initialization,increment/decrement,condition,just two semicolons in the body of the loop makes it a infinitive loop.Use ctrl+c to exit from the program.
Syntax
for(;;) { }
Program example:
class Example { public static void main(String[] args) { for(;;) { System.out.println("Hello Loops"); } } }
for each loop
In Java, the for-each loop is used to iterate over an array or collection. Because we don’t have to increment the value or use subscript notation, it’s easier to use than a standard for loop.
It is based on elements rather than the index. It returns each element in the defined variable one by one.
Syntax:
for(data_type variable : array_name) { }
Program Example:
class Example { public static void main(String[] args) { int arr[]={2,4,6,8,10}; for(int i:arr) { System.out.println(i); } } }
while loop
The while loop in Java is used to execute a section of code until the provided Boolean condition is true. The loop comes to an end as soon as the Boolean condition is false.
The while loop is a type of if statement that repeats itself. The while loop is recommended if the number of iterations is not fixed.
It also has the same four parts as a for loop.
Syntax:
initialization while (condition) { Increment / decrement statement }
A simple program to demonstrate while loop:
A program to print first twenty natural numbers.
class SimpleProgram { public static void main(String[] args) { int i=1; while(i<=20) { System.out.println(i); i++; } } }
Infinitive while loop
If one pass true or 1 in the while loop, it will be infinitive while loop.Use ctrl+c to exit from the program.
Syntax:
while(1) { }
Program Example:
class Example { public static void main(String[] args) { while(true) { System.out.println("Hello loops"); } } }
do while loop
The Java do-while loop is used to repeatedly iterate a section of a program until the specified condition is met. A do-while loop is recommended if the number of iterations is not fixed and the loop must be executed at least once.
An exit control loop is a Java do-while loop. In contrast to while and for loops, the do-while loop checks the condition at the end of the loop body. Because the condition is tested after the loop body, the Java do-while loop is executed at least once.
Syntax
initialization do { //code to be executed / loop body increment/decrement }while (condition);
A simple program to demonstrate while loop:
A program to print first twenty natural numbers.
class DoWhileExample { public static void main(String[] args) { int i=1; do { System.out.println(i); i++; }while(i<=10); } }
Infinitive do while loop
If one pass true or 1 in the do while loop, it will be infinitive do while loop.Use ctrl+c to exit from the program.
Syntax:
do { }while(1);
Program Example:
class Example { public static void main(String[] args) { do { System.out.println("Hello loop"); }while(true); } }
COMPARISON OF LOOPS
Comparison | for loop | while loop | do while loop |
Introduction | The Java for loop is a control flow statement that repeats a section of a program several times. | The Java while loop is a control flow statement that repeats the execution of a portion of a program based on a boolean condition. | The Java do while loop is a control flow statement that runs a portion of a program at least once before continuing to run it based on a boolean condition. |
When to use | If the number of iterations is fixed, the for loop is suggested. | If the number of iterations is not fixed, the while loop is preferred. | The do-while loop is recommended if the number of iterations is not fixed and the loop must be executed at least once. |
Syntax | for(initialization;condition;increment/decrement) { // code to be executed } | while(condition) { //code to be executed } | do{ //code to be executed }while(condition); |
Example | for(int i=1;i<=20;i++) { System.out.println(i); } | int i=1; while(i<=20) { System.out.println(i); i++; } | int i=1; do { System.out.println(i); i++; }while(i<=20); |
Syntax for infinitive loop | for(;;) { //code to be executed } | while(true) { //code to be executed } | do { //code to be executed }while(true); |
loops in java|for loop|while loops|do while loops|their types|comparison between different loops|