5/18/22, 2:36 PM Java while loop - Javatpoint
Home Java Programs OOPs String Exception Multithreading
https://www.javatpoint.com/java-while-loop 1/10
5/18/22, 2:36 PM Java while loop - Javatpoint
Java While Loop
The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean
condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is
recommended to use the while loop.
Syntax:
while (condition){
//code to be executed
I ncrement / decrement statement
}
The different parts of do-while loop:
1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed
and control goes to update expression. When the condition becomes false, we exit the while loop.
Example:
i <=100
2. Update expression: Every time the loop body is executed, this expression increments or
decrements loop variable.
https://www.javatpoint.com/java-while-loop 2/10
5/18/22, 2:36 PM Java while loop - Javatpoint
Example:
i++;
Flowchart of Java While Loop
Here, the important thing about while loop is that, sometimes it may not even execute. If the
condition to be tested results into false, the loop body is skipped and first statement after the while
loop will be executed.
Example:
https://www.javatpoint.com/java-while-loop 3/10
5/18/22, 2:36 PM Java while loop - Javatpoint
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need
to initialize and increment the variable used in the condition (here, i). Otherwise, the loop will
execute infinitely.
WhileExample.java
public class WhileExample {
public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
}
}
Test it Now
Output:
10
Java Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
https://www.javatpoint.com/java-while-loop 4/10
5/18/22, 2:36 PM Java while loop - Javatpoint
Syntax:
while(true){
//code to be executed
}
Example:
WhileExample2.java
public class WhileExample2 {
public static void main(String[] args) {
// setting the infinite while loop by passing true to the condition
while(true){
System.out.println("infinitive while loop");
}
}
}
Output:
infinitive while loop
infinitive while loop
infinitive while loop
https://www.javatpoint.com/java-while-loop 5/10
5/18/22, 2:36 PM Java while loop - Javatpoint
infinitive while loop
infinitive while loop
ctrl+c
In the above code, we need to enter Ctrl + C command to terminate the infinite loop.
← Prev
Next →
Youtube
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
https://www.javatpoint.com/java-while-loop 6/10
5/18/22, 2:36 PM Java while loop - Javatpoint
Learn Latest Tutorials
Splunk tutorial SPSS tutorial Swagger T-SQL tutorial
tutorial
Splunk SPSS Transact-SQL
Swagger
Tumblr tutorial React tutorial Regex tutorial Reinforcement
learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning
R Programming RxJS tutorial React Native Python Design
tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns
Python Pillow Python Turtle Keras tutorial
tutorial tutorial
Keras
Python Pillow Python Turtle
Preparation
https://www.javatpoint.com/java-while-loop 7/10
5/18/22, 2:36 PM Java while loop - Javatpoint
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
Company
Interview
Questions
Company Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence
Hadoop tutorial ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
https://www.javatpoint.com/java-while-loop 8/10
5/18/22, 2:36 PM Java while loop - Javatpoint
B.Tech / MCA
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System tutorial
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
Java Python
tutorial
Programs
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
https://www.javatpoint.com/java-while-loop 9/10
5/18/22, 2:36 PM Java while loop - Javatpoint
https://www.javatpoint.com/java-while-loop 10/10