for loop, while loop, do-while loop java

for loop, while loop, do-while loop java

In the Java programming language, `for`, `while`, and `do-while` loops allow Java programmers to repeat or control a specific block of source code multiple times, based on a set of user-defined conditions. Fundamentally, there are three types of program loops in Java programming, the `for` loop, the `while` loop, and the `do-while` loop. Each of these loops is utilized to manage or control either single or multiple programming conditions; depending on the specific requirements of the current program, they can be employed for general-purpose tasks or for handling specific, individual conditions.

for loop, while loop, do-while loop java

So, let’s take a closer look at the for, while, and do-while loops in Java programming.

The for Loop in Java Programming.

The for loop is the easiest to use in Java programming. It is typically employed when a Java programmer already knows—in advance—exactly how many times a specific block of source code needs to be repeated within a program, from a defined starting point to an ending point. The `for` loop offers a comprehensive mechanism to handle program condition expressions within a single line: it allows for the initialization of a user-defined variable, the testing of a specified user-defined condition, and the updating of the variable (using increment or decrement operators) to execute a series of statements.

for Loop Syntax.

for (initialization; condition; update) {

// Program source code to be executed in each iteration of the for loop

}

Elements of the for Loop.

  • Initialization — This element initializes the loop variable. Here, a user-defined variable (previously declared in the program) is assigned an initial value. This step executes exactly once, before the loop begins its iterations.
  • Condition — This element defines the custom condition for the `for` loop. This condition is tested prior to each iteration (before the loop statements are executed). If the user-defined condition evaluates to `true`, the `for` loop continues to run; otherwise, it automatically terminates.
  • Update — This expression is executed after every iteration of the `for` loop, provided the loop condition remains true. Typically, it updates the loop variable’s value—either by incrementing or decrementing it—as long as the loop condition holds true.

Java for Loop Example.

public class Main

{

public static void main(String[] args) {

// Here, the variable ‘p’ prints integer values ​​ranging from 1 to 10.

for (int p = 1; p <= 10; p++) {

System.out.println(p);

}

}

}

Explanation of the for Loop.

  • Here, the variable `p` is initialized with a starting value of `p = 1`.
  • Within the `for` loop, `p <= 10` serves as a user-defined condition. In this specific program, it ensures that the `for` loop continues to execute as long as the value of the variable `p` remains less than or equal to 10.
  • After each iteration of the loop, the expression `p++` updates the variable `p` by incrementing its value by 1.

The while Loop in Java Programming.

In Java programming, the `while` loop is used when a programmer needs to repeatedly execute a specific block of source code for as long as a user-defined custom condition remains true. Unlike the `for` loop, the `while` loop evaluates the program condition *before* each iteration of the loop. Consequently, if the user-defined condition evaluates to `false` right at the beginning, the source code contained within the `while` loop will not be executed even once.

while Loop Syntax.

while (condition) {

// The program source code within this block will be executed as long as the condition remains true.

}

Elements of the while Loop.

  • Condition — This refers to the user-defined condition that is evaluated *before* the `while` loop begins its execution. If the user-defined condition here evaluates to *true*, the program source code block will be executed; conversely, if the user-defined condition evaluates to *false*, the `while` loop will terminate automatically.

Java while Loop Example.

public class Main

{

public static void main(String[] args) {

int q = 1;

// Here, the variable ‘q’ prints numbers from 1 to 10 as long as the condition remains true.

while (q <= 10) {

System.out.println(q);

q++;

}

}

}

Explanation of the while Loop.

  • Here, the variable `q` is initialized to 1.
  • Before every iteration of the loop, the specified condition (`q <= 10`) is evaluated. The loop continues to repeat as long as the value of the variable `q` remains less than or equal to 10.
  • In each iteration of the loop, the statement `q++` is executed, which increments the value of `q` by 1.

do-while Loop in Java Programming.

In Java programming, the nature of the `do-while` loop is similar to that of the `while` loop; the only difference is that the user-defined condition is evaluated *after* the main body of the loop has executed. This implies that the program code written inside a `do-while` loop is guaranteed to run at least once, regardless of the specific user-defined condition defined therein.

Syntax of the do-while Loop.

do {

// Program source code to be executed

} while (condition);

Elements of the do-while Loop.

  • Condition – Here, the user-defined condition is tested *after* the source code block within the do-while loop has executed. If the user-defined condition evaluates to `true`, the do-while loop continues to run; otherwise, the loop terminates.

Example of a do-while Loop.

public class Main

{

public static void main(String[] args) {

int p = 1;

// Here, it prints numbers from 1 to 10 until the do-while loop terminates.

do {

System.out.println(p);

p++;

} while (p <= 10);

}

}

Do-while Loop Explanation.

  • Here, the variable `p` is initialized with a value of 1.
  • The program code inside the `do` block executes first, and subsequently, the user-defined condition `p <= 10` is tested.
  • The do-while loop continues to execute as long as the value of the variable `p` remains less than or equal to 10.

Main Differences among for, while, and do-while java Loops

Loop Aspectfor Loopwhile Loopdo-while Loop
Test ConditionBefore the first program variable iteration.It tests condition Before the first value iteration.It tests program condition After the first iteration.
Execution GuaranteeFor loop Executes only, when the define condition is true.While loop Executes only, when the given condition is true.Only Do while Executes at least once time, regardless of condition test.
Where to Use CaseUse for loop When you know the number of series number iterations in advance.Use while loop When the number of iterations is unknown and you don’t know and depends on a user condition.When you want to run the loop should run at least once time.
Loop SyntaxFor loop syntax is Compact initialization, condition, and update in one line nature.While loop syntax is Separate initialization and update value outside the loop body.Do while loop Condition is checked after the loop’s body executes in program.

Infinite Loops in Java.

In Java programming, an infinite loop occurs when a user-defined loop condition is *always* evaluated as `true`, causing the loop’s logic or expression to run continuously without end. This can happen in a program if the programmer fails to update the loop control variable (either intentionally or accidentally), or if the loop condition is inherently defined to be always true.

Java Infinite for Loop Example.

public class Main

{

public static void main(String[] args) {

// Infinite for loop explanation

for (int p = 1; p <= 10; ) {

System.out.println(p); // Here, the variable ‘p’ prints the value 1 indefinitely.

}

}

}

Infinite for Loop Explanation.

  • In the program example provided above, no update statement (such as `p++`) is defined within the infinite loop; consequently, the value of the variable `p` remains constant at 1, and the user-defined condition `p <= 10` remains perpetually true.

Example of an Infinite while Loop.

public class Main

{

public static void main(String[] args) {

// Here, we define an infinite while loop

int p = 1;

while (p <= 10) {

System.out.println(p); // The while loop will print ‘1’ indefinitely because the variable is not updated.

}

}

}

Explanation of the Infinite while Loop.

  • In this program, the `while` loop condition `p <= 10` remains true because the value of the variable `p` is never updated. As a result, the loop never terminates.
  • To avoid infinite loops in Java programming, always ensure that the loop control variable or the loop condition is correctly updated within the loop body.

Detail summary about for, while, and do-while Loops

Loop TypeWhere to useCheck ConditionLoop Example
For loopUsed in program, when you know the number of iterations series in advance.It checks loop condition Before the loop starts.for (int p = 0; p < 10; p++) { … }
While loopUsed while loop in program, when you want to loop while a condition is true, and the number of iterations is unknown by programmer.It checks loop condition Before each iteration.while (q < 10) { … }
do-while loopUsed do while loop in java program, when you need to execute the loop at least once time.It checks loop condition After each iteration.do { … } while (r < 10);

Leave a Reply