GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 06: Intro to Loops


Objectives

By the end of this module, for simple HelloWorld-like programs, you will be able to:




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Catching Up

Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:

  1. Numeric Variables: Declaring, assigning and using in operations




Some preliminaries



Bolean Values

Boolean values are true or false. And we'll be using them often in a few weeks.

Since these are a new type of values, then we need another variable type to store them.

That variable type is calle boolean, and it can store either true or false.

in Java, we declare a boolean variable like this:

boolean myBool;


and we can assign them like this:

myBool = false;


or like this:

myBool = true;


And we can also do declaration and assignment in the same line:

boolean myBool = true;

Conditions

Conditions refer to questions that can be asked and that result in either true or false. In other words:

a Condition evaluates to a boolean.



Example: In the sentence "I will give you a slize of pizza if you finish your homework", the condition to be evaluated is "is the homework finished?", which evaluates to either true or false. We'll see the Java version below.

We'll talk more about conditions and Conditionals in a few weeks, bit for now, just remember that a condition is something that is resolved into true or false (which are boolean values).




Loops: the while-loop



An example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

  public class PrintCountdown
  {
    public static void main(String args[])     
    {
      // Set up an initial count value
      double count = 10;

      // Now print and decrease it one by one
      System.out.println(count--);  // "--" acts AFTER we resolve the value, so it prints a 10
      System.out.println(count--);  // it prints a 9
      System.out.println(count--);  // it prints a 8
      System.out.println(count--);  // it prints a 7
      System.out.println(count--);  // it prints a 6
      System.out.println(count--);  // it prints a 5
      System.out.println(count--);  // it prints a 4
      System.out.println(count--);  // it prints a 3
      System.out.println(count--);  // it prints a 2
      System.out.println(count--);  // it prints a 1
      System.out.println("Blastoff!");  // it prints Blastoff!
    }
  }  
  
Activity 1: Let's Visualize this execution using our favourite visualizer,
Visualize PrintCountdown.java

This is nice... but. You already know all of this. How is this cool?

Why is this cool?: Because it sets up a couple of the most awesome things you can do in a computer, which is: to automate repetition!.

Let's see this same idea but with a new structure: The while loop:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

  public class AwesomePrintCountdown
  {
    public static void main(String args[])     
    {
      // Set up an initial count value
      double count = 10;

      // Now print and decrease it one by one
      while (count > 0)
      {
        System.out.println(count--);
      }
      System.out.println("Blastoff!");  // it prints Blastoff!
    }
  }  
  


The new structure is:



The flow of execution for a while statement is:

  1. Evaluate the condition in parentheses, yielding true or false.
  2. If the condition is false, skip the following statements in braces.
  3. If the condition is true, execute the statements and go back to step 1.



Activity 2: Let's Visualize this execution using our favourite visualizer,
Visualize AwesomePrintCountdown.java

Some issues with the while

Consider the following program and figure out if there is some sort of potential issue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

  public class ForAWhile1
  {
    public static void main(String args[])     
    {
      // Set up an initial count value
      double count = 10;

      // Now print and decrease it one by one
      while (count < 0)
      {
        System.out.println(count--);
      }
      System.out.println("Blastoff!");  // it prints Blastoff!
    }
  }  
  


Now, look at the following program and figure out if there is some sort of potential issue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

  public class ForAWhile2
  {
    public static void main(String args[])     
    {
      // Set up an initial count value
      double count =10;

      // Now print and decrease it one by one
      while (count > 0)
      {
        System.out.println(count++);
      }
      System.out.println("Blastoff!");  // it prints Blastoff!
    }
  }  
  

Finally, check the following program and figure out if there is some sort of potential issue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

  public class ForAWhile3
  {
    public static void main(String args[])     
    {
      // Set up an initial count value
      double count = 10;

      // Now print and decrease it one by one
      while (count > 0)
      {
        System.out.println(count);
      }
      System.out.println("Blastoff!");  // it prints Blastoff!
    }
  }  
  


This last issue is common! Not updating the code that affects the condition may result in trouble!!!




Loops: the for-loop



An example

Consider the following program:

What we would like is a way to organize repetition.

We will do this using a version of the for-loop:

Let's zoom in on the for-loop and dissect it:




Printing the loop variable



Consider this example:

Activity 3: Type, compile and execute the above program.

Activity 4: In MyForLoopPrint.java, change the loop conditions so that the numbers 1 through 10 are printed. Also add a print statement after the for-loop, that prints anything.

Observe:

Mental execution:




Variations



To explore for-loops further, we'll look at some variations of the basic for-loop:



Activity 6: Write a program to print out the odd numbers from 25 down to 1. Write your code in Odd25Down.java.




Nested for-loops



We'll start by writing a program to print a little number triangle like this:

  1
  22
  333
  4444

Notice: there's repetition across a row of numbers: a potential use of for-loops!

Here's the program, using for-loops:

Observe:

Activity 7: Add a row for 5 (with five of them), writing your code in MyForLoopPrint2.java

Activity 8: Just for the heck of it, could one use a for-loop to achieve the printing of 1? That is, answer the question: can a for-loop be set up so that you go into it exactly once? Write your code in MyForLoopPrint3.java

Identify repetition and try a Nested For Loop

Next, observe that the upper-limits of the for-loops are themselves increasing:

Another way to say this:

    When the value is 2, print a row of 2's
    When the value is 3, print a row of 3's
    When the value is 4, print a row of 4's
  


Think for next class: Can we use a for loop to automate the upper limits of the for loops themselves?

... To Be Continued ...