Groundhog Day: Loop Constructs

12.01.2018 |

Episode #4 of the course An introduction to Java programming by Alex Theedom

 

Welcome to the fourth of ten lessons about the Java programming language. I hope you are still enthused from the last lesson. Let’s get to it. Today, you are going to learn about while loop structures. It is used to execute the same piece of code until a logical condition is false.

 

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 4.

• Create a class file called, GroundhogDay.

• Delete the line, System.out.println(“Hello Codiva”);.

Now you are ready to start coding.

 

Step 2: Start Coding

You’re going to write code that prints the same message to the screen an infinite number of times, using the while loop structure. The while loop’s structure looks like this: while(condition) { … code here … }.

If the condition is true, the code between the braces {… code here …} is run. Then once that code is run, the condition is tested again, and if it is still true, the code between the braces is run again. This continues until the condition is false. If the condition is never false, the code runs forever. This is called an infinite loop.
Okay, let’s write an infinite loop.

• For a loop to be infinite, the while condition must always be true, so let’s set the while condition to the boolean value true. It will look like this:

class GroundhogDay {
   public static void main(String[] args) {
     while (true){

     }
   }
}

• Now, the while loop needs some code to run. So, let’s make it print a message to the console. By now, you should know how to do this. Refer back to previous lessons for a hint.

 

Step 3: Check and Run Your Program

Here is the infinite loop code in full:

class GroundhogDay {
   public static void main(String[] args) {

     while (true){
       System.out.println(“forever and ever and ever…”);

     }
   }
}

Run your program by clicking the Run button. If all went well, you will see a message printed to the console at the bottom of the screen.

NOTE: Infinite loops are dangerous because they continually use memory and can cause the program to crash. To prevent this from happening, the codiva.io website detects infinite loops and forces them to stop after a few iterations. This protects the website from crashes.

 

Step 4: Extend Your Groundhog Day Program

Let’s make the loop do something a little bit more useful: Print out the five times tables. Remember the calculation program? You are going to do something very similar now.

• You need a variable to store the starting value for the times table. You need an integer variable and to assign it the value 1. This will be the starting point for the times table (i.e. 1 x 5). This will look like this: int x = 1;.

• Next, you need to perform the actual multiplication, which will look like this: x * 5;.

• Then you need to increase the value of x by one, so the calculation can be sequential: 1 x 5, 2 x 5, 3 x 5, and so on. This will look like this: x++;.

• The final piece of the puzzle is to limit the number of loops to ten. You only want the five times table up to 10 x 5. This is done by setting the while condition to test the value of x. It looks like this: while( x <= 10 ). This means that the loop will continue while the value of x is less than or equal to 10.

 

Step 5: Check and Run Your Program

Here is the code in full:

class GroundhogDay {
   public static void main(String[] args) {

     int x = 1;
     while (x <= 10){
       System.out.println(x * 5);
       x++;

     }
   }
}

Run your program. If all went well, you will see the five times tables printed to the console.

Congratulations, you have successfully completed your fourth Java program!

In Lesson 5, you will learn how to write code that makes decisions. So, see you then.

 

Recommended book

Intro to Java Programming, Comprehensive Version by Y. Daniel Liang

 

Share with friends