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 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 does a flag-controlled while loop work in Java?ComputerScienceOne · While Loops
- How does a do-while loop differ from a while loop in C?ComputerScienceOne · Do-While Loops
- How are while loops structured and styled in PHP?ComputerScienceOne · While Loops