Java Journeys : From for to while and beyond!

Ritu Sitlani
Javarevisited
Published in
3 min readMar 14, 2024
source : Unsplash

Java Loops:

In simple terms, Java loops are tools in programming that allow you to repeat a set of instructions over and over again, but only if a particular condition is true.
In Java, there are three main types of loops:

while Loop :

A while loop in Java is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition evaluates to true.

package Java;
public class Statements{
public static void main (String args[]) {
int i = 0; //variable initialization
while(i<=5) { //condition
System.out.println(i); //code to be executed repeatedly
i++;
}}}

In above simple Java code using while loop, we initialized the variable i to 0. The while loop checks the condition, The loop continues executing as long as this condition evaluates to true. If the condition evaluates to false initially, the code inside the loop will never execute.

The loop keeps going until i is not smaller than 5. When i reaches 5, the loop stops because the condition is no longer true.

For loop :

Used when you know how many times you want to execute a block of code. It has an initialization, condition, and an update statement.

package Java;
public class Statements{
public static void main (String args[]) {
for(int i = 0; i<=5;i++) {
System.out.println(i);
}
}}

Output:

0
1
2
3
4
5

In above simple Java code using for loop,
Initialization step. Here, we initialize a variable i to 0 before the loop starts.
Condition check step. This is the condition that determines whether the loop should continue executing. As long as i is less than or equal to 5, the loop will continue.
++: Update step. After each iteration of the loop, this statement increments the value of i by 1.

Basically, for loops are ideal when the number of iterations is predetermined or known in advance, whereas while loops offer greater flexibility, especially when the number of iterations may vary or when looping until a specific condition is satisfied.

do — while Loop :

Similar to a while loop, but it guarantees that the block of code inside the loop is executed at least once before checking the condition.

package Java;
public class Statements{
public static void main (String args[]) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);

}
}

Output:

1
2
3
4
5

In above simple Java code using do — while loop,
Initialization step. Here, we initialize a variable i to 1 before the loop starts.
then, System. out.println statement prints the value of i during each iteration of the loop.
i++ : After each iteration of the loop, this statement increments the value of i by 1.
The loop will continue to execute as long as the value of i is less than or equal to 5.

Basically, The do-while loop makes sure that the code inside it runs at least once, regardless of the condition being true or false initially. It runs the code block first and then checks the condition. This loop is useful when you need to ensure that certain actions happen before checking the condition for further repetitions.

Break and Continue :

In Java, the “break” statement serves to halt a loop prematurely, terminating its execution, whereas the “continue” statement skips the ongoing iteration of the loop and advances to the subsequent one. “break” is commonly employed to exit a loop under specific conditions, while “continue” enables the omission of particular iterations within the loop, depending upon certain conditions.

Summary :

In summary, each loop construct serves specific purposes: the for loop for predetermined iterations, the while loop for dynamic conditions, and the do-while loop for ensuring at least one execution before condition evaluation. Choosing the appropriate loop construct depends on the specific requirements and conditions of the program.

Points worth noting :

  • Be mindful of loop conditions, ensuring they result in true for loop continuation and false for termination. Avoid infinite loops.
  • We can utilize print statements or a debugger to track the flow of execution and pinpoint any problems related to loop logic or variables.

Explore More :

  1. java-basics-exploring-overflow-and-underflow-concepts
  2. java-basics-primitive-data-types-and-wrapper-classes-made-simple
  3. introduction-to-java-variables
  4. java-operators-making-math-harder
  5. java-journeys-exploring-decision

Thankyou!

linkedin , twitter

--

--

Ritu Sitlani
Javarevisited

Hi, I'm an Engineer sharing insights on software testing and Java - Learning along the way!