Java Loop Control Statements: Comprehensive Guide

Rumman Ansari   Software Engineer   2024-10-17 10:32:00   11263  Share
Subject Syllabus DetailsSubject Details 1 Questions
☰ TContent
☰Fullscreen

Table of Content:

Loop in Java

Java’s iteration statements are for, while, and do-while. These statements create what we commonly call loops.

A loop can be used to tell a program to execute statements repeatedly. Or we can say that, a loop repeatedly executes the same set of instructions until a termination condition is met.


Why Loop is Important ?

Suppose that you need to display a string (e.g., Welcome to Ansari Academy! ) a hundred times. It would be tedious to have to write the following statement a hundred times:

System.out.println("Welcome to Ansari Academy!");
System.out.println("Welcome to Ansari Academy!");
...
...
...
System.out.println("Welcome to Ansari Academy!");

So, how do you solve this problem?

Java hava a powerful feature called a loop that controls how many times an operation or a sequence of operations is performed in succession. Using a loop control statement, you simply tell the computer to display a string a hundred times without having to code the print statement a hundred times, as follows:

// Demonstrate the while loop.
class A {
public static void main(String args[]) {
   int count = 0;
   while (count < 100) {
   System.out.println("Welcome to Ansari Academy!");
   count++;
   }
 }
}

Output

Welcome to Ansari Academy!
Welcome to Ansari Academy!
....
....
....
Welcome to Ansari Academy!
Welcome to Ansari Academy!
Press any key to continue . . .

Explanation

The variable count is initially 0 . The loop checks whether count < 100 is true . If so, it executes the loop body to display the message Welcome to Ansari Academy! and increments count by 1 . It repeatedly executes the loop body until count < 100 becomes false . When count < 100 is false (i.e., when count reaches 100 ), the loop terminates and the next statement after the loop statement is executed


Another important Example ?

// Demonstrate the while loop.
class A {
public static void main(String args[]) {
  int n = 10;
  while(n > 0) {
  System.out.println(n);
  n--;
  }
 }
}

Output

10
9
8
7
6
5
4
3
2
1
Press any key to continue . . .

Types of Loop

Java provides mainly three types of loop statements: while loops, do - while loops, and for loops.

Types of Loops in Java Programming Language
Sr.No. Loop & Description
1   while loop

In while loop first checks the condition if the condition is true then control goes inside the loop body otherwise goes outside of the body.

2  do...while loop

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

3  for loop

For Loop is used to execute set of statements repeatedly until the condition is true.

4  For-each or Enhanced For Loop

The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.

5  Labeled For Loop

We can have name of each for loop.It is useful if we have nested for loop so that we can break/continue specific for loop


MCQ Available

There are 2 MCQs available for this topic.

2 MCQ