Loops in Java

Loops in Java

To repeat a set of statements more then one time without using any structure is pretty hard. For example : if we want to display 100 times “Welcome to Java” on the screen. The following line we have to write 100 times to achieve results.

Instead of repeating hundred lines java facilitates a control structure know as loop with great flexibility to repeat the set of statements.
For example, the following peace of code will print “Welcome to java” hundred time over the screen.

While Loop

While loop is based on boolean expression to control the execution and body having set of statement to perform a number iteration on it. One time iteration of body after successful evaluation of boolean express is known as one iteration. Pseudocode for while loop is given bellow.

Programming Example :

while loop

Above example prints welcome to java hundred times. While loop evaluates boolean expression if it is true, continue iteration otherwise terminate entire loop. Boolena expression in example remain true hundred time and successfully prints “Welcome to java” hundred time.

do – while Loop

do while loop is a variation of while loop. In do while body is executed first then boolean expression is evaluated. If the evaluation is true the body will be executed again otherwise terminate the loop. Pseudocode for do-while loop is given bellow.

Programming Example :

for Loop

for loop uses the variable to control the sequence. Pseudocode for for loop is given bellow:

Programming Example :

for loop

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.