While loop:

While loop:

Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.


This content originally appeared on DEV Community and was authored by Kaviya.k

While loop:

Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.

Syntax of while loop in Java:

while (test_expression) {   

 // statements        

update_expression; 


}


condition is evaluated before each iteration of the loop
If condition is true, the code block inside the while loop will execute
The process repeats until condition is false

Execution of While Loop in Java:

Image description

  1. Control enters the while loop.
  2. The condition is tested.
  3. If true, execute the body of the loop.
  4. If false, exit the loop.
  5. After executing the body, update the loop variable.
  6. Repeat from step-2 until the condition is false.
Example 1: Display Numbers from 1 to 5

// Program to display numbers from 1 to 5

class Main {
  public static void main(String[] args) {

    // declare variables
    int i = 1, n = 5;

    // while loop from 1 to 5
    while(i <= n) {
      System.out.println(i);
      i++;
    }
  }
}

output:
1
2
3
4
5

Task1:

Image description

Program:
public class Count {

    public static void main(String[] args) {
    int count=1;
        while (count<=5){
        count=count+1;
        System.out.println("count: "+count);
        }
        }
        }

output:
count: 2
count: 3
count: 4
count: 5
count: 6

Task2:

Image description

Program:

public class Count1 {

    public static void main(String[] args) {
        int count=5;
        while (count>=1) {
        count=count-1;
        {
        System.out.println("count:" +count);
        }
        }
    }
}

output:
count:4
count:3
count:2
count:1
count:0

Task3:

Image description

Program:
public class Count2 {
    public static void main (String args[])
    {
    int count=0;
    while(count<10) {
    count=count+2;
    {
    System.out.println("count:" +count);
    }
    }
    }
}
output:
count:2
count:4
count:6
count:8
count:10

Task4:

Image description

program:
public class Count3 {
    public static void main (String args[])
    {
    int count=1;
    while(count<=10) {
    count=count+2;
    {
    System.out.println("count:" +count);
    }
    }
    }

}
output:

count:3
count:5
count:7
count:9
count:11

References:

https://www.geeksforgeeks.org/java-while-loop-with-examples/
https://www.programiz.com/java-programming/do-while-loop


This content originally appeared on DEV Community and was authored by Kaviya.k


Print Share Comment Cite Upload Translate Updates
APA

Kaviya.k | Sciencx (2025-02-03T02:00:29+00:00) While loop:. Retrieved from https://www.scien.cx/2025/02/03/while-loop-3/

MLA
" » While loop:." Kaviya.k | Sciencx - Monday February 3, 2025, https://www.scien.cx/2025/02/03/while-loop-3/
HARVARD
Kaviya.k | Sciencx Monday February 3, 2025 » While loop:., viewed ,<https://www.scien.cx/2025/02/03/while-loop-3/>
VANCOUVER
Kaviya.k | Sciencx - » While loop:. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/03/while-loop-3/
CHICAGO
" » While loop:." Kaviya.k | Sciencx - Accessed . https://www.scien.cx/2025/02/03/while-loop-3/
IEEE
" » While loop:." Kaviya.k | Sciencx [Online]. Available: https://www.scien.cx/2025/02/03/while-loop-3/. [Accessed: ]
rf:citation
» While loop: | Kaviya.k | Sciencx | https://www.scien.cx/2025/02/03/while-loop-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.