bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

Java While Loop

Loops

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

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

Java While Loop

The while loop repeats a block of code as long as the specified condition is true :

Syntax

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

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

Example

int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}

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

You can also use a while loop to count down. This example counts from 3 to 1, and then prints "Happy New Year!!" at the end:

Example

int countdown = 3;
while (countdown > 0) {
  System.out.println(countdown);
  countdown--;
}
System.out.println("Happy New Year!!");

While Loop With False Condition

In the previous examples, the condition was true at the start, so the loop ran one or more times. But if the condition is false at the beginning, the code inside the loop will never run:

Example

int i = 10;
while (i < 5) {
  System.out.println("This will never be printed");
  i++;
}

Note

A while loop may never run if the condition is false from the start. In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition.

Previous

Java Switch

Next

Java Do/While Loop

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

LoopsJava While LoopCountdown ExampleWhile Loop With False Condition