- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 12
Table of Contents
<< Lesson 11 | Lesson 13 >>
Lesson 12: Control Structures in Java – The DO…WHILE Loop
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.
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 {
//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.
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 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ... - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 8
IF .. ELSE statement has two parts, one to cater for when condition is TRUE and another for when condition is FALSE. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program.
3