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
  1. 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.
  2. 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.
  3. Create an integer variable named counter and assign it the value from start_number.
  4. Create a while loop: While the counter is less than or equal to the end_number, do the following:
    • Display the current value of counter
    • Increment counter by 1.
  5. After the while loop is over, display "Goodbye!" at the end of the program.
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
  1. Create a boolean variable named running and set it to True (Python) or true (C++)
  2. Create a while loop that continues looping while running is True (Python) or running = 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 set running = False (Python) or running = false; (C++).
  3. After the while loop, display a "goodbye" message.
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
  1. Create an integer variable named min and assign it a value of 1.
  2. Create an integer variable named max and assign it a value of 10.
  3. Tell the user to enter a number between min and max. Don't hard code 1 and 10 here, display the variable values as part of the message.
  4. Get the user's input as an integer and store it in a variable named choice.
  5. Create a while loop that continues looping while the user input is invalid. This means, if choice is less than min OR choice is greater than max. 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.
  6. After the while loop, display a "Goodbye!" message.
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
  1. Create a variable named sum, a float variable. Store the value 0.0 in it.
  2. Create a variable named user_number, a float variable. Store the value 0.0 in it.
  3. 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 the sum variable, updating the sum: sum = sum + user_number. (In C++ don't forget the ;!)
  4. After the while loop, display a "goodbye!" message.

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!

  1. The program should contain at least one while loop.
  2. Your while loop should have some condition, such as a variable equals some value. The variable will have to be declared before the loop.
  3. 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.)
  4. Display "Goodbye!" at the end of the program

Author: Rachel Wil Sha Singh

Created: 2023-10-11 Wed 18:48

Validate