Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 12

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 11 | Lesson 13 >>

Lesson 12: Control Structures in Java – The DO…WHILE Loop

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-12

In the previous lesson, we learnt how to use the WHILE loop in Java and we also did some examples involving the WHILE loop. In this lesson we shall look at the DO … WHILE loop.

The DO … WHILE loop is almost similar to the WHILE loop, the difference is that in this loop, the execution of statements occur before checking of a condition.

The DO…WHILE loop execute statements a number of times so long as the specified condition remains TRUE.

The structure of the DO … WHILE loop is as follow:


do {

//Statements to execute

}

while( condition to check );


Notice in DO…WHILE loop we have a semicolon after the closing round bracket in the condition part unlike in WHILE loop. Let us try one practical example. We’ll use the DO…WHILE loop to print a string of text for ten times just like we did in Lesson 11. Create another class, call it Dowhile, input the following code and run your program.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-12

Exercise:

Modify the above program so that it will work like the “Password” example we did in Lesson 11

Let us now compare the WHILE loop and DO…WHILE loop in term of structure, execution and outputs

The structure of the DO … WHILE loop is as follow:


do {

Scroll to Continue

//Statements to execute

}

while( condition to check );


And the structure of the WHILE loop is as follow:


while( condition to check ) {

//Statements to execute

}


The reason why I have shown the two loops structures above is because I would like you to notice the difference in them.

In the DO…WHILE loop, we first execute the statements and check the condition later, but in WHILE loop, we first check the condition before we execute the statements. It therefore means that in DO…WHILE loop, at least one statement or group of statements will be executed even if the condition is FALSE.

Let us test this with a practical example. In the program you wrote ealier, Dowhile program, change the initial value of the count variable from 1 to 11 and run the program again.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-12

Notice that, in the above example even if 11 is not less or equal to 10 in the condition, we still get at least one output. This is because of the structure nature of the DO…WHILE loop i.e. do something while we check the condition!

I believe by now you have learnt the basics about Java control structures. The key in being an expert in loops is working with them and getting used to them. The FOR loop is widely used in Java applications and many other programming languages and I would recommend you get used to it by practicing a lot.

It is time to move to something else. In the next lesson, we shall learn about Arrays, how we can implement Arrays in Java and how we can manipulate Arrays using loops.


<< Lesson 11 | Lesson 13 >>

Want to make money online?

Comments

Sean on November 08, 2013:

These tutorials were a great help. Thank you.

Danson Wachira (author) from Nairobi, Kenya on February 13, 2013:

Hi Jennah,

Thanks for the visit and comment, am glad that this tutorial was helpful to you.

Jennah on February 12, 2013:

I was looking for a Java simple tutorial and luckily i got it here. You have done a superb work and i would like to follow this tutorial up to the end. Thank you!

Related Articles