Archive

Archive for the ‘Using loop constructs’ Category

Using loop constructs in Java

November 18, 2009 Leave a comment

 

Module 2: Using loop constructs:

 

Objectives:

  • Create while loops.
  • Develop for loops.
  • Create do / while loops.

 

 

1. Create while loops:

The while loop is used to execute a set of code for multiple times as long as a condition is true. Once the condition becomes false, the code block is no longer executed and the control exits the while loop and continues with the execution of rest of the program.

 

 Create while loops

We can see the syntax for the while loop in the syntax figure above. We begin with the keyword while, and then we put a boolean expression inside a set of paranthesis. If this boolean expression is true, we execute the code block between the left and the right curly braces. If the boolean expression is false, then we do not execute the code block and we leave the while loop. This code block will be executed as many number of times as long as the boolean expression continues to be true.

This is illustrated in a flow chart to the right. The boolean expression is tested for true / false. If true, then we execute the code block, and you can also see that we return back to again test the boolean expression after executing the code block, and again the expression will be tested for true / false. As long as the boolean expression continues to be true, the flow will be the same. Once the expression evaluates to false, the code block will no longer be executed and the control leaves the while loop.

 

While loop example:

Let’s take a look at an example of while loop. As you can see from the figure below, we have a while loop that is checking if the variable i is less than 5. If it is, we will enter the code block, and we will print out “Hello Thomas” to the command prompt / console. And then we are incrementing the i variable by 1. We will then again go back to the while loop condition and check if the i variable is still less than 5. As long as it is, we will continue to print to the console and continue to increment i and continue back to the while loop condition. Once its not, we will leave the while loop and continue with the rest of the program. Let’s see this for 2 different values of i.

 

For i = 0: 

 While loop example

 

When i = 0, the while condition is true for the first time, and we enter the loop and print “Hello Thomas” to the console, increment i by 1 because of which it becomes 1, and again go back to the while loop condition and check it again. Here i is 1 and still less than 5 so the whole loop is repeated for 5 times until i < 5 and “Hello Thomas” is printed 5 times to the console. Once i is 5, i < 5 will be false so it will exit the while loop and continue with the execution of the rest of the program.

 

For i = 5:

 While loop example 1

 

 

When i = 5, we check the while loop condition first, which is i < 5, as we have i as 5, we get this condition to be false. Hence we wont enter the code block, hence not printing anything to the console and continue with the execution of rest of the program.

 

Nested while loops: Read more…