CS 134: While Loops
Table of Contents
1. Introduction
- WHILE LOOPS
While loops are similar to if statements in that you specify a CONDITION. With an if statement, if that CONDITION evaluates to true then the code within the if statement's codeblock is executed - once. If the CONDITION for a while loop evaluates to true, it will run the while loop's inner codeblock REPEATEDLY, until the CONDITION evaluates to false. Because of this, it is easy to write an INFINITE LOOP with while loops. While loops take this form:
while ( CONDITION ) { // This code is ran while the CONDITION is true. }
- FORCING THE PROGRAM TO STOP
While a program is running in the console, you can use "CTRL+C" to force the program to stop execution. This might be useful if you accidentally make a infinite loop.
2. Program 1: Count up
- Program steps
- Get input from the user; tell them "Enter a starting number:" and get their input as an integer and store it in a variable named
start_number
. - Get input from the user; tell them "Enter an ending number:" and get their input as an integer and store it in a variable
end_number
. - Create an integer variable named
counter
and assign it the value fromstart_number
. - Create a while loop: While the
counter
is less than or equal to theend_number
, do the following:- Display the current value of
counter
- Increment
counter
by 1.
- Display the current value of
- After the while loop is over, display "Goodbye!" at the end of the program.
- Get input from the user; tell them "Enter a starting number:" and get their input as an integer and store it in a variable named
- Example program output
Enter a starting number: 1 Enter an ending number: 5 1 2 3 4 5 Goodbye!
3. Program 2: Program loop
- Program steps
- Create a boolean variable named
running
and set it toTrue
(Python) ortrue
(C++) - Create a while loop that continues looping while
running is True
(Python) orrunning =
true= (C++). Within the while loop, do the following:- Display a main menu of options: 1: FAVORITE MOVIE, 2: FAVORITE BOOK, 3: QUIT
- Ask the user for their selection. Store this as an integer input in a variable named
choice
. - If the
choice
is 1, then display your favorite movie. - Else, if the
choice
is 2, then display your favorite book. - Else, if the
choice
is 3, then setrunning = False
(Python) orrunning = false;
(C++).
- After the while loop, display a "goodbye" message.
- Create a boolean variable named
- Example output
1. FAVORITE MOVIE, 2. FAVORITE BOOK, 3. QUIT Selection: 1 Favorite movie is The Lost Skeleton of Cadavra Selection: 2 Favorite book is Masters of Doom Selection: 3 Goodbye!
4. Program 3: Validate input
- Program steps
- Create an integer variable named
min
and assign it a value of 1. - Create an integer variable named
max
and assign it a value of 10. - Tell the user to enter a number between
min
andmax
. Don't hard code 1 and 10 here, display the variable values as part of the message. - Get the user's input as an integer and store it in a variable named
choice
. - Create a while loop that continues looping while the user input is invalid. This means, if
choice
is less thanmin
ORchoice
is greater thanmax
. Within the loop do the following:- Display a message, "Invalid selection! Try again!"
- Get the user's input again, overwriting the
choice
variable. - Display "Your selection was:", and then the value from
choice
.
- After the while loop, display a "Goodbye!" message.
- Create an integer variable named
- Example output
Enter a number between 1 and 10 choice: 100 Invalid selection! Try again! choice: -5 Invalid selection! Try again! choice: 5 Goodbye!
5. Program 4: Sum
- Program steps
- Create a variable named
sum
, a float variable. Store the value 0.0 in it. - Create a variable named
user_number
, a float variable. Store the value 0.0 in it. - Create a while loop that loops while
user_number
is greater than or equal to 0. Within the loop do the following:- Display "Sum:", and then the value from the
sum
variable. - Tell the user "Enter a non-negative number:" and store it in the
user_number
variable. - Add
user_number
to thesum
variable, updating the sum:sum = sum + user_number
. (In C++ don't forget the;
!)
- Display "Sum:", and then the value from the
- After the while loop, display a "goodbye!" message.
- Create a variable named
The program loop will end once the user enters a negative number.
- Example output
Sum: 0 Enter a non-negative number: 2 Sum: 2 Enter a non-negative number: 5 Sum: 7 Enter a non-negative number: -5 Goodbye!
6. Program 5: Freeform
Implement whatever you'd like, as long as you meet the requirements!
- The program should contain at least one while loop.
- Your while loop should have some condition, such as a variable equals some value. The variable will have to be declared before the loop.
- The while loop should eventually end - not be an infinite loop. (In other words, something should change your variable so that the condition evaluates to false, and then the loop ends.)
- Display "Goodbye!" at the end of the program