Welcome to Lesson 13!By the end of today's lesson, you should be able to answer the following questions...
Announcements
Review ActivityWith a partner, answer the following questions:
int count = 1; while (count <= 10) { cout << count << endl; count++; }
Loop 1: string repeat = "y"; while (repeat == "y") { cout << "Playing an exciting game!\n\n"; cout << "Want to play again? (y/n): "; Loop 2: int counter = 1; while (counter <= 10) { cout << counter << endl; } Common Loop Pitfalls
Infinite Loops
Missing Curly Braces
{ } to expand the single statement
Empty Statements
;
Counting Loops
For Statements
Another For Loop Example#include <iostream> using namespace std; int main() { int max = 0; cout << "This program uses a loop to count!\n"; cout << "Enter the maximum number: "; cin >> max; for (int i = 1; i <= max; i++) { cout << i << endl; } } Anatomy of the For Loop
for loop is reached, execute the initialize statement (starting point).
condition is true.
update statement.
Diagram of |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #include <iostream>
using namespace std;
int main() {
int number = 0;
cout << "Enter a number and I will show its"
<< " bar graph.\nEnter your number: ";
cin >> number;
cout << "\nBar graph:\n";
for (int i = 0; i < number; i++) {
cout << '*';
}
cout << endl;
return 0;
}
|
Summing Numbers
- One common looping task is to input a list of numbers and calculate their sum
- For example, if we want to add the sum of the first four integers:
sum = 1 + 2 + 3 + 4 = 10
- As another example, we can add a list of 5 numbers:
sum = 1 + 2 + 3 + 4 + 5 = 15
- We can generalize our examples to sum from 0 to any number n.
Example Application to Sum a Sequence of Numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| #include <iostream>
using namespace std;
int main() {
cout << "Enter the number to sum to: ";
int number = 0;
cin >> number;
int sum = 0;
for (int i = 1; i <= number; i++) {
sum = sum + i; //using the counter as a value to add to my sum
}
cout << "Total sum = " << sum << endl;
return 0;
}
|
Activity 13.2: Tracing a Loop (10 pts)
In this exercise, we will trace the execution of the following for loop.
for (int i = 10; i <= 15; i++){if (i == 11 || i == 13 || i== 15)cout << "*";elsecout << "!";}
- Find a partner. Then, open up Canvas and locate the text box under this activity
- In the text box write both your names at the top
- Then, type what the output of the above for loop would be to the console.
- Note, you should do this without running the code.
- Then, change the statement i <=15 to be i < 15.
- Draw the output of the for loop with this alteration.
- How is it different?
- The instructor will be going over the examples so you can verify your answers and understanding.
- Don't forget to submit when you are finished.
More Loopy Errors
- A common problem with counting loops is the off-by-one error
- Finding the correct upper and lower bounds can be confusing
- Should you start at 0 or 1?
- Are you counting up to a number (<), or up to and including a number (<=)?
- To understand the counting loop you need to count iterations.
- For instance, the following loop executes b - a times:
for (int i = a; i < b; i++)
- However, the following loop is executed b - a + 1 times:
for (int i = a; i <= b; i++)
- The "+1" is the source of many errors
- For example, to sum the numbers from 1 to 5, what is wrong with:
#include <iostream> using namespace std; int main() { int sum = 0; for (int count = 0; count < 5; count++) { sum += count; } cout << sum << endl; return 0; }
- One way to visualize the "+1" error is by looking at a fence
- How many posts do you need for a fence with three sections?
- It is easy to be "off-by-one" with problems like this
- Forgetting to count the last value is sometimes called a fence-post error
Debugging Technique: Tracing Variables
- One good way to discover loopy errors is to display the variables that are part of the loop
- Tracing variables means watching their values change as the program executes
- You can insert temporary output statements in your program to watch the variables, like:
cout << "count=" << count << ", sum=" << sum << endl;
Activity 13.3: ASCII (10 pts)
In this exercise we continue exploring some uses of counter-controlled loops.
Background
Recall that a char
data type is stored by the computer as a number using the ASCII code (ASCII Table). Since a char
is stored as an int
by the computer, C++ lets you cast an int
to a char
.
int count = 65;
cout << (char) count << endl; //What does the (char) do?
This casting feature lets us construct our own ASCII table.
Specifications
- Find a partner for pair programming.
- Copy and paste the starter code into a file called ascii.cpp
- Alter the
for
-loop so that it counts from 32 to 127 - Inside the for-loop, alter the cout statement as follows:
- Compile and run your modified program and make sure your output looks like:
(some output not shown) 65 A 66 B 67 C (more output not shown)
- When you are finished, play around with the starting and ending values of your for loop.
- Can you find the ASCII value for any unusual characters, such as the 4 ranks of playing cards, smiley faces and musical notes.
- It turns out that each of these symbols has its own ASCII value in C++!
- Submit the final version of your file to Canvas when you are finished.
Wrap Up
- With your partner, answer the questions from today's learning objectives. Additionally:
- How many times will the following loop execute?
for (int i = 1; i < 5; i++){
cout << i << endl;
}
- And this one?
for (int i = 1; i <= 5; i++){
cout << i << endl;
}
- Alter the nested loop below so that it will print rectangles instead of squares:
#include <iostream>
using namespace std;
int main() {
cout << "I will print rectangles for you!\n\n";
cout << "Enter the width of the rectangle: ";
int width;
cin >> width;
cout << "Enter the length of the triangle: ";
int length;
cin >> length;
for (int row = 1; row <= ???; row++)
{
for (int col = 1; col <= ???; col++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
- Assignment 13 due Thursday at 9:20am on Canvas
- Quiz 5 on Thursday.
- Lab 7 due Friday
- With your partner, answer the questions from today's learning objectives. Additionally:
- How many times will the following loop execute?
- And this one?
- Alter the nested loop below so that it will print rectangles instead of squares:
#include <iostream> using namespace std; int main() { cout << "I will print rectangles for you!\n\n"; cout << "Enter the width of the rectangle: "; int width; cin >> width;
cout << "Enter the length of the triangle: ";
int length;
cin >> length;
for (int row = 1; row <= ???; row++)
{
for (int col = 1; col <= ???; col++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
for (int row = 1; row <= ???; row++) { for (int col = 1; col <= ???; col++) { cout << "*"; } cout << endl; } return 0; }
- Assignment 13 due Thursday at 9:20am on Canvas
- Quiz 5 on Thursday.
- Lab 7 due Friday