0% found this document useful (0 votes)
1 views5 pages

Java While Loop

Uploaded by

neuralhack.io
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Java While Loop

Uploaded by

neuralhack.io
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/14/25, 9:51 PM Java While Loop

 Tutorials  Exercises  Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

Java While Loop


❮ Previous Next ❯

Loops
Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more
readable.

Java While Loop


The while loop loops through a block of code as long as a specified condition is true :

Syntax Get your own Java Server

while (condition) {
// code block to be executed
}

In the example below, the code in the loop will run, over and over again, as long as a
variable ( i ) is less than 5:

Example
https://www.w3schools.com/java/java_while_loop.asp 1/5
8/14/25, 9:51 PM Java While Loop

int iTutorials
= 0;  Exercises  Services   Sign In
while (i < 5) {
HTML
 CSS JAVASCRIPT SQL
System.out.println(i); PYTHON JAVA PHP HOW TO W3.CSS C
i++;
}

Try it Yourself »

Note: Do not forget to increase the variable used in the condition ( i++ ), otherwise the loop
will never end!

Do you wonder why we used the letter i in the example above? It's a counter variable and
a common choice in simple loops because it's short, traditional, and stands for 'index' or
'iterator'.

Countdown Example
This example counts down from 3 to 1 and then displays "Happy New Year!!" at the end:

Example

int countdown = 3;

while (countdown > 0) {


System.out.println(countdown);
countdown--;
}

System.out.println("Happy New Year!!");

Try it Yourself »

https://www.w3schools.com/java/java_while_loop.asp 2/5
8/14/25, 9:51 PM Java While Loop

 Tutorials  Exercises  Services 


?
 Sign In

HTML
 CSS JAVASCRIPT SQL
Exercise
PYTHON JAVA PHP HOW TO W3.CSS C

How many times will the following loop execute?


int i = 0;
while (i < 4) {
i++;
}

Submit Answer »

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

https://www.w3schools.com/java/java_while_loop.asp 3/5
8/14/25, 9:51 PM Java While Loop

 Tutorials  Exercises  Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

COLOR PICKER

 

 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial

https://www.w3schools.com/java/java_while_loop.asp 4/5
8/14/25, 9:51 PM Java While Loop
JavaScript Tutorial

 Tutorials  How To Tutorial


Exercises
SQL Tutorial
 Services   Sign In
Python Tutorial
HTML
 CSS W3.CSS
JAVASCRIPT TutorialSQL PYTHON JAVA PHP HOW TO W3.CSS C
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie
and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

https://www.w3schools.com/java/java_while_loop.asp 5/5

You might also like