- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java while loop
while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop is −
while(Boolean_expression) { // Statements }
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non zero value.
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Flow Diagram
Here, the key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("
"); } } }
This will produce the following result −
Output
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
- Related Articles
- Infinite while loop in Java
- Java do-while loop example
- Java infinite do-while loop
- What are the differences between while loop and do-while loop in Java?
- How to use ‘while loop’ in Java?
- do…while loop vs. while loop in C/C++
- How to use ‘do while loop’ in Java?
- Difference Between while and do-while Loop
- Difference between for loop and while loop in Python
- The while loop in Javascript
- Do-while loop in Arduino
- While loop in Lua Programming
- Python - How to convert this while loop to for loop?
- How to convert a Python for loop to while loop?
- For Versus While Loop in C
