Welcome to Lesson 11! Learning Objectives
By the end of today's class, you should know...
Announcements
Pre-Assessment ActivityReview of Numbers, Operators and Precision:
x = x + 1;
const double PI = 3.1415927;
Review of Conditions Involving Logical Operators:
b. if (numCaller == 19 && (pet == "rabbit" || "cat" || "dog") && (age >= 18 && <= 65)) int age = 20; bool is_student = true; if (is_student || age > 21) { cout << "Fi!"; } if (!is_student && age < 21) { cout << "Fo!"; } if (is_student && age < 21) { cout << "Fum!"; } Wrapping Up ConditionalsConditional Pitfalls
Strings of Inequalities
Strings of Inequalities
Strings of Logical Operators
Hand Back and Go Over Midterms
Introducing Loops
Simple Loops
While Loops
initialization statement;
string repeat = "y"; cout << "Do you want to play again? (y/n): " cin >> repeat;
Image source. Understanding the
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream>
using namespace std;
int main() {
string repeat = "y";
while ("y" == repeat) {
cout << "\nPlaying an exciting game!\n";
cout << "Do you want to play again? (y/n) ";
cin >> repeat;
}
cout << "\nThanks for playing!\n";
return 0;
}
|
- Notice the structure of the code for the looping application
- First was the statement to initialize the looping condition:
string repeat = "y"; //gives the variable its starting value
- Then came the while statement with the test condition
while ("y" == repeat)
- The loop executes the body of the loop if and only if the condition evaluates to
true
- Another important part of the loop is the update statement:
cin >> repeat; //changes the value of the variable
- Most loops have these parts:
- Initialization statement
- Test condition
- Update statement (allows the loop the option to fail)
- Initialization statement
- Question: What would happen if I replaced the while loop with an if statement?
- We will explore the
while
loop more in the next exercise.
Group Activity: Halloween Candy
- As a class, we are going to act out the following while loops
int num_pieces_candy = 10;
instructor.fill_with_candy(Bag, num_pieces_candy);
instructor.hand_bag(first_student);
while (num_pieces_candy > 0)
{
student.takeOnePieceCandy();
student.passBag(next_student);
num_pieces_candy--;
}
- With your partner, discuss for the while loop above:
- What is/are the initialization statement(s)?
- What is the loop test condition?
- What are the statements that repeat?
- Which statement will ultimately cause the test condition to fail?
- Now, let's act out While Loop 1!
While Loop 2
instructor.fill_with_candy(Bag);
instructor.hand_bag(first_student);
while (Bag != empty)
{
student.takeOnePieceCandy();
student.passBag(next_student);
}
- With your partner, discuss for the while loop above:
- How is the above while loop different from while loop 1
- How many times will the statements inside while loop 2 execute?
- Were you able to state an exact number for while loop 2?
- Now, let's act out While Loop 2!
Activity 11.1: Counting Down...
- Find a partner for pair programming.
- Open up CodeBlocks and create a new C++ file named countdown.cpp.
- Imagine we are at NASA Mission Control.
- A new space ship is about to be launched.
- Let's write a program that counts down until take off. See the sample output below:
NASA Mission Control readying for liftoff.Initializing countdown from 10...10987654321We have liftoff!
- We are going to write this program in two ways.
- The first program should use 13 cout statements to achieve the above output.
- Let's get to work!
- Add your name and section in appropriate block comments, include the iostream library and use the standard namespace.
- Don't forget a main() function.
- Declare an integer variable called countdown at the top of main and set it equal to 10.
- Now, write a cout statement to output the following:
NASA Mission Control readying for liftoff.
- Add another cout statement to print the following to the console:
Initializing countdown from 10...
- Next, in a pair of statements, lets print the contents of countdown and then decrease its value by 1.
- Add the following two statements to your code:
cout << countdown << endl;countdown--;
- You will need to repeat these two lines of code 10 times in your program.
- Copy the two lines and then paste them into your program 9 more times.
- You should have a very repetitive program by now!
- Finally, add a cout statement to print out "We have liftoff!" below the numbers.
- Run your code to make sure that your output is identical to my output above.
- When you are finished and the program is working properly, upload it to Catalyst.
Part 2: Using a While Loop (10 pts)
- Let's alter the above program to be written more efficiently with a loop.
- First, take a look at your code.
- Which part of the code is repetitive?
- Hint: The repetitive lines will go inside the curly braces of the while loop.
- We need a while loop to count down from the number entered by the user. Create a while loop that looks like this:
while (test will go here) {
//statements
}
}
- What should go inside the test?
countdown > 0
- Why do you use > 0 here?
- Inside the curly braces of the while loop, we need to print out the contents of the countdown variable and then decrease its value by 1.
- Take one of your pairs of cout statement and variable decrement and place it in the curly braces of the while loop.
- Now, you can erase the other 9 pairs of statements.
- Finally, below your while loop, there should now be only one cout statement
- Run your program to make sure it is working the same as before.
- When you are finished, upload this new version to Catalyst.
- If you finish early, work on your Lab 6 or answer the question from today's Wrap Up
Proper While Loop Formatting
About those Curly Braces
- Like the
if
-statement, the curly braces of awhile
loop are optional - Technically, the
while
statement affects only the single statement that follows - We use curly braces to make that one statement into a block of statements
- This allows us to put any number of statements within the body
- Curly braces are not always required, but the best practice is to always include them
Program Style: Indent Statements Inside a Loop
- It is important to format the loop code correctly:
string repeat = "y"; while ("y" == repeat) { // ... statements to repeat cin >> repeat; }
string repeat = "y";
while ("y" == repeat)
{
{
// ... statements to repeat
cin >> repeat;
}
}
- Note how the repeated code is indented inside the loop
- This lets us see easily which code is repeated and which is not
- Also note the placement of curly braces
- Different groups have different practices for placing curly braces in a loop statement
Wrap Up
With a partner, answer the following questions:
Upcoming AssignmentsWith a partner, answer the following questions:
- Label the different parts of the following while loop as: update statement, initialization or test condition.
int count = 1;
while (count <= 10) {
cout << count << endl;
count++;
}
- Correct the loop below (hint it is missing something!). What will happen if you run the code BEFORE making the correction?
string repeat = "y";
while (repeat == "y") {
cout << "Playing an exciting game!\n\n";
cout << "Want to play again? (y/n): ";
}
- Answer the questions from today's learning objectives.
- Assignment 11 due Thursday at 3:20pm
- Quiz 4 on Thursday
- Lab 6 due Friday at midnight
~See You Thursday!~