## Learning Objectives

By the end of today's class, you should know...
• What is a counting loop?
• What are the three important components of a counting loop?
• What is a for loop and why was it designed?
• How does a for loop differ from a while loop?
• What are some applications of for loops?
• What is an off-by-one error and how can you avoid this type of error?

## 1. Lesson 10 Practice Exam Questions

• Label the different parts of the following while loop as: update statement, initialization or test condition.

int count = 1;

while (count <= 10) {

System.out.println(count);

count++;

}
• Fill in the missing parts of the below loop to display the numbers from 1 to 5 to the console
int count = _________;

while (___________________________) {
count++;
System.out.println(count);
}
• What is the difference between an infinite loop and an indefinite loop

• Correct the loops below. What will happen if you run the code BEFORE making the correction?

Loop 1:

String repeat = "y";

while (repeat.equals("y")) {

System.out.println("Playing an exciting game!\n");

System.out.println("Want to play again? (y/n): ");

}

Loop 2:

int counter = 1;

while (counter <= 10) {

System.out.println(counter);

}

## 2. Counting Loops

• Counting is a common task in our lives.
• In programming, counting is also very common.
• For example, we saw an example of counting in an activity, where we used a while loop to count down from 10 to 0.
• In programming we often use loops to count when we know exactly how many times we want a specific piece of code to repeat.
• In fact, counting is such a common programming task, that a special type of loop was designed specifically for counting.
• This loop is called a for loop, and we will be learning more about it in class today.

### For Statements

• As previously mentioned, counting is a common use for loops.
• Loops that are controlled by a counter variable are called counter-controlled loops.
• We can visualize a counter-controlled loop as a series of steps to reach a goal. • A counter-controlled while loop has the form:
```int i = start;
while (i < end) {
...
i++;
}
```
• Where:
• i: the name of a counter variable
• start: the initial starting value
• end: the final ending value
• We can write this same counting loop as a for loop:
```for (int i = start; i < end; i++) {
...
}```
• The for loop was especially designed for counting tasks.
• Notice its compact syntax compared to the while loop.
• All the conditions which define the loop are on a single line.
• When writing a counting loop, we often have a choice of using either a for or a while loop, as both will accomplish the same task.
• However the compact syntax of the for loop makes it preferable.
• The following example shows the same loop written as both a while loop and a for loop:
//counting from 10 to 20 using a while loop
int i = 10;
while (i <= 20)
{
System.out.println(i);
i++;
}

//counting from 10 to 20 using a for loop
for (int i = 10; i <= 20; i++)
{
System.out.println(i);
}
• What differences do you notice in the two loops above?
• What are the starting values for the loops? Where are they located?
• Test conditions?
• Update statements?
• Notice the use of the semi-colons in the for loop. Which statements end with semi-colon and which do not?

#### Another For Loop Example

public static void main(String[] args){
int max = 0;
Scanner input = new Scanner(System.in);

System.out.println("This program uses a loop to count!");
System.out.print("Enter the maximum number: ");
max = input.nextInt();

for (int i = 1; i <= max; i++)
{
System.out.println(i);
}
input.close();
}

### Anatomy of the For Loop

#### Diagram of `for` Loop Operation ### Group Activity: Altering a For Loop

• Copy and paste the below for loop into an Java file called MoreLoopy.java
• Now, try altering the for loop to see if you can achieve the following results:
• Can you make the loop print out the numbers from 0 up to and including the max?
• Can you make it print out the numbers from 0, up to, but not including, the max?
• Can you make it count up by 2s to the max?
• Can you make the loop count down from the max to 0?
/**
* @author
* CIS 36A
*/
import java.util.Scanner;

public class MoreLoopy {

public static void main(String[] args) {
int max = 0;
Scanner input = new Scanner(System.in);

System.out.println("This program uses a loop to count!\n");
System.out.print("Enter the maximum number: ");
max = input.nextInt();

System.out.println();

for (int i = 1; i <= max; i++)
{
System.out.println(i);
}
input.close();
}

}

### Activity 11.1: Counting Down Part 3 (10 pts)

• Make a new folder named Activity11 in Eclipse.
• Add a new class named Countdown.java into this folder.
• Open up your Countdown.java file from last class.
• Copy and paste this code into your new Countdown.java folder.
• Locate the while loop in your code.
• Alter this while loop to be a for loop.
• Hint: Make sure that your for loop has 3 parts:
1. initialization (where do you want to start counting?)
2. test condition (when should the loop fail?)
3. update statement (are you counting up or down?)
• Compile and run your program and verify that you still get the following output:
NASA Mission Control readying for liftoff.
Initializing countdown from 10...
10
9
8
7
6
5
4
3
2
1
We have liftoff!
• Submit your program to Canvas when you are finished.

## Wrap Up

• Rewrite the below while loop as the equivalent for loop:

## int count = 0;while (count < 10) {    System.out.println(count);     count++; }

• Trace the output of the below loop:
for (int i = 0; i <= 5; i++) {
if( i % 2 == 0 || i % 3 == 0) {
System.out.print("\$");
} else {
System.out.print("!");
}
}

## Upcoming Assignments

• Activities 11.1-11.4 due Tuesday at 11:59pm
• Assignment 11 due Friday at 11:59pm
• Quiz 6 due Friday at 11:59pm

~Have a Great Day!~