Do While Statement Assignment Help

Assignment Help: >> Elements of Java - Do While Statement

Do-While

As you earlier saw, if the conditional expression controlling a while loop is initially false, so the body of the loop will not be executed at all.  Moreover, sometimes it is desirable to run the body of a while loops at least once still if the conditional expression is false to start along with.  Alternatively, there are times whenever you would like to test the termination expression at the end of the loop rather than at the starting. Fortunately, Java supplies a loop which does just with the do-while. The do- while loop constantly executes their body at least once, since its conditional expression is at the bottom of the loop. Their common form is

do {

/ / body of loop

} while (condition);

Every iteration of the do-whole-loop first executes the body of the loop and after that evaluates the conditional expression.  The loop will repeat if this expression is true. Or else the loop terminates. As along with all of Java's loop's condition must be a Boolean expression.

Here is a reworked version of "tick" program which demonstrates the do-while-loop. It produces the similar output as before.

/ / Demostrate the do-while loop.

class Dowhile {

public static void main (String args [ ] ) {

int n = 10;

do {

System.out.println("tick" +n );

n - - ;

}while (n>0);

}

}

The loop in the preceding program, although technically right, can be written more efficiently as given below.

do {

System.out.println ("tick") + n);

} while ( - - n > 0);

Within the above example, the expression (- -n >0) merges the decrement of n and the test for zero into one expression. Here is describes how it works. At First, the - - n statement executes which decrementing n and returning the new value of n.  There value is then compared with zero. The loop continues if it is greater than zero or else it terminates.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd