Concept
How is a basic while loop structured in Java?
ComputerScienceOne / While Loops
"Java supports while loops, for loops, and do-while loops using the keywords while, for, and do (along with another while). Continuation conditions for loops are enclosed in parentheses, (...) and the blocks of code associated with the loop are enclosed in curly brackets. Code Sample 28.1 contains an example of a basic while loop in Java. Just as with conditional statements, our code styling places the opening curly bracket on the same line as the while keyword and continuation condition. The inner block of code is also indented and all lines in the block are indented to the same level. In addition, the continuation condition does not have a semicolon since it is not an executable statement. Just as with an if-statement, if we had included a semicolon it would have led to unintended results.\n\n1 int i = 1; //Initialization\n\n2 while(i <= 10) { //continuation condition\n\n3 //perform some action\n\n4 i++; //iteration\n\n5 }\n\nCode Sample 28.1.: While Loop in Java"
Related Ideas
- How does a flag-controlled while loop work in Java?ComputerScienceOne · While Loops
- How are for loops structured and scoped in Java?ComputerScienceOne · Do-While Loops
- How do do-while loops differ from while loops in Java?ComputerScienceOne · For Loops
- How can a while loop normalize a number by repeatedly dividing it by 10?ComputerScienceOne · Normalizing a Number
- What are the basic syntax rules of Java?ComputerScienceOne · Basic Syntax Rules
- What are enhanced for loops in Java?ComputerScienceOne · Enhanced For Loops
- How do nested loops execute in Java?ComputerScienceOne · Nested Loops
- How does Java use variables and collections in enhanced for loops?ComputerScienceOne · Enhanced For Loops