Iteration Statements Java Help

Iteration Statements

Java’s iteration statements are for, while, and do-while. These statements create what we commonly call/oops. As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met. As you will see, Java has a loop to fit any programming need.

while

The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expressions true. Here is its general form:

The condition can be any Boolean expression. The body of the loop will be executed as long as the Conditional expression is true. When condition becomes false, control passes ib the next line of code immediately following the loop. The curly braces are
if-only a single statement is being repeated. Here is a while loop t~t counts down from 10~printing exactly ten airless of “tick

Since that loop evaluates Its.conditional expression at that top of the loop. that body of tilt’ loop will-not execute even once if the condition is false to begin with. For
example, in the following fragment, the call to print ln() is never executed:

This program finds the midpoint between i and j. It generates the following output:
Midpoint is 150 Here is how the while loop works. The value of j is incremented, and the value of j is decremented. These values are then compared with one another. If the new value of i is still less than the new value of j, then the loop repeats. If i is equal to or greater than j, the loop stops. Upon exit from the loop, i will hold a value that is midway between .the original values of i and j. (Of course, this procedure only works when i is less t han j.

to begin with.) As you can see, there is no need for a loop body; all of the Jackson occurs within the conditional expression, itself. In professionally written Java code, short loops are frequently coded without bodies when the controlling expression can handle  all of the details itself.

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 16, 2014 in The Complete Reference

Share the Story

About the Author

Back to Top
Share This