Bifalgorithm post · Computer science one · While Loops

How is a basic while loop structured in Java?

How is a basic while loop structured in Java? (Computer science one · While Loops) — "Java supports while loops, for loops, and do-while loops using the keywor…

Ideas in this flowchart

  • Java loop structures and conventions
  • Java supports while, for, and do-while loops.
  • These loops use the keywords while, for, and do together with another while.
  • Loop continuation conditions are enclosed in parentheses, and associated code blocks are enclosed in curly brackets.
  • The opening curly bracket appears on the same line as the while keyword and condition; the inner block is consistently indented.
  • A continuation condition has no semicolon because it is not an executable statement.
  • Including a semicolon after the continuation condition can produce unintended results, as with an if-statement.
  • Code Sample 28.1 demonstrates a basic while loop in Java.
  • The loop initializes i to 1.
  • The loop continues while i <= 10.
  • The loop performs an action inside its block.
  • Each iteration increases i by one using i++.
Feed/

How is a basic while loop structured in Java?

No cover

Channel

Computer science one

1 page

"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"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.