Loop Control Statements in Java
Loop Control Statements:
We have already had an overview, in the previous articles, of how the loop control statements in Java work. They are also called repetition control structures. If you haven’t gone through it, please refer to this article.
Parts of a loop statement
- Initialization
- Condition
- Body
- Update
Types of Loop Control Structure:
There are 4 types of loop control statements in Java:
for Loop:
- It is used for a finite number of times to repeat a portion of a program, like when we know that the loop has to run for n times.
- In a for loop statement, the condition, initialization, and the update statement are defined in a single line.
- When the result of loop condition becomes false, it executes the next statements present after the for loop block.
- The for loop is an “entry-controlled” loop, meaning, the entry to the loop block is decided based on the condition provided.
- Syntax:
for (initialization ; condition ; increment/decrement) { // Statements if condition is true; // Statements if condition is true ; …. } // Statements if condition is false or when loop is completed.
Here are few working examples of for loop.
1. Objective: Write a program to print “Hello” 5 times using for loop.
package test_2; public class HelloMultiple { public static void main(String[] args) { int no; for (no = 1; no <= 5; no++) { System.out.println("Hello \n"); } } }
2. Objective: Program to display all the numbers from 1 to 10 using for loop.
package test_2; // Display all the numbers from 1 to 10 public class Numbers { public static void main(String[] args) { int no; System.out.println("Numbers from 1 to 10 are"); for (no = 1; no <= 10; no++) System.out.println(no); } }
3. Objective: Program to display the multiplication table of a number using for loop.
package test_2; import java.util.Scanner; public class MultiplicationTable { public static void main(String[] args) { int number, term, i; System.out.println("Enter a number to print multiplication table"); Scanner sc = new Scanner(System.in); number= sc.nextInt(); System.out.println("Enter number of terms in table"); term = sc.nextInt(); // Prints multiplication table System.out.println("Multiplication table-------------------\n"); for (i = 1; i <= term; ++i) { System.out.println(number + " X " + i + " = " + number*i); } } }
While loop:
- It is used for an unknown number of repetitions when we don’t know how many times the loop will continue.
- In a while statement, you can only specify the looping condition. And the loop block is executed only if the looping condition is true.
- When the result of the loop condition becomes false, it exits the loop and executes the next statements present after the block.
- It is an “entry-controlled” loop, same as for loop.
Syntax:
while( Condition ) { // Statements if condition is true; // Statements if condition is true …. } // Statements if consition is false or when loop is completed.
Here is a working example of while loop.
Objective: Write a program to print “Hello !” 5 times using while loop.
package test_2; public class HelloWhile { public static void main(String[] args) { int i = 0; while ( i < 5 ) { System.out.println( "Hello !\n"); i++; } } }
do-while loops:
- This loop is suitable when you want to execute the loop body at least once.
- This loop has a syntax similar to while loop except that the word “while,” along with the condition is at the end and the term “do” is at the beginning of the loop.
- The working of the do-while statement is different from while.
- it first executes the statement blocks and then checks the condition. And thus the loop is executed at least once, irrespective the condition is true or false.
- It is an “exit-controlled” loop, meaning, the control exits the loop body if the condition doesn’t match.
Syntax:
do { // Statements if condition is true; // Statements if condition is true …. } while( condition ) ; // Statements if condition is false or when loop is completed.
Here is a working example of the do-while loop.
Objective: Write a program to print Hello, World 5 times using do while loop.
package test_2; public class HelloDo { public static void main(String[] args) { int i = 0; do { System.out.println("Hello, World!\n"); i++; } while ( i < 5 ); } }
Comparison between Loops:
For Loop | While Loop | Do-While Loop |
---|---|---|
Initialisation, looping condition and update expression is part of for loop syntax | It doesn't contain initialisation and update expression in it's syntax | It doesn't contain initialisation and update expression in it's syntax |
Tests the condition at the beginning of the loop body | Tests the condition at the beginning of the loop body | Tests the condition at the end of the loop body |
entry-controlled loop | entry-controlled loop | exit-controlled loop |
Executes the block of code until a given condition is true | Executes the block of code until a given condition is true | The block of code inside do while loop will execute at least once irrespective of the condition |
for-each loop (Advanced or enhanced for loop):
- The for each loop is introduced in Java 5. It is mainly to traverse array or collection elements.
- The significance of for each loop is that
- it eliminates the possibility of bugs
- makes the code more readable.
Syntax:
for(data_type variable : array_name|collection_name) { // Statements to be executed } // Statements if condition is false or when loop is completed.
Here is a program to traverse each element of an array, using for-each loop.
package test_2; public class ArrayLoop { public static void main(String[] args) { int arr[]= {12, 1, 16, 56, 99}; for (int i: arr) { System.out.println("Element f array is: " + i); } } }
In the next article, we will learn about the Jump control statements, break & continue and the selection control statement, switch-case.