CS2 5.3.2
CS2 5.3.2
TARGET
You’re probably familiar with the game Pictionary. (Although nowadays, you might be more
familiar with skribbl.io). It’s a game where you have to guess what someone’s drawing is. The
flowchart below jokingly describes how the game is played.
It might be frustrating to draw something that other people can’t guess. Sometimes you try to
redo the drawing. Sometimes you add new stuff to make it more understandable. But you only
stop when you’ve guessed the word (or when the time is up).
This can be an example of an event controlled loop. If the person hasn’t guessed the correct
answer yet, then the game just keeps going.
LG-SANCHEZ-CS2-5.3.2 CS 2 Page 1 of 4
IGNITE Time Allotment:13 minutes
If we were to make a code loosely based on Pictionary, it could look like this:
1. #include<iostream>
2. using namespace std;
3. int main(){
4. string guess;
5. string correctAnswer = "dog";
6. do{
7. cout << "guess a word: ";
8. cin >> guess;
9. } while(guess!=correctAnswer);
10. return 0;
11. }
Here, the program keeps asking for a word for as long as guess is not yet equal to correctAnswer.
This makes use of a do-while loop to control the event. In a previous module, it was stated that a do-
while loop always performs at least one iteration. The user will always be able to guess at least one
time.
Output:
Event controlled loops can also be used to validate user input before proceeding to the rest of the
program. The example below shows a loop that will keep asking for an input until the user inputs a
number that meets the desired specifications (i.e. is positive).
1. #include<iostream>
2. using namespace std;
3. int main(){
4. int num;
5. cout << "Enter a positive number: ";
LG-SANCHEZ-CS2-5.3.2 CS 2 Page 2 of 4
6. cin >> num;
7. while(num < 0){
8. cout << "The number has to be positive. Try again.";
9. cin >> num;
10. }
11. //rest of the program
12. return 0;
13. }
Output:
Output:
In both examples, the loop was checking an input and would only terminate if the input was correct.
In module 5.2.1, you were asked to create a program that would get the Factorial of N. Recall
the instructions below:
LetRewrite
us check
theyour knowledge.
program so that the user should enter a number between 10 and 50 instead. If the
inputted number does not fit that range, then the program should continue to prompt the user for a
correct input.
Challenge yourself (not required): Prompt the user at the end if they want to get another
factorial and repeat the program as necessary.
LG-SANCHEZ-CS2-5.3.2 CS 2 Page 3 of 4
Note: This is a non-graded assessment
KNOT
REFERENCES
Loop control statements. (2014). Object- Oriented Programming in Python 1.0. https://python-
textbok.readthedocs.io/en/1.0/Loop_Control_Statements.html
© 2020 Philippine Science High School System. All rights reserved. This document may contain proprietary informaHon and may only be
released to third parHes with the approval of management. Document is uncontrolled unless otherwise marked; uncontrolled
documents are not subject to update noHficaHon.
LG-SANCHEZ-CS2-5.3.2 CS 2 Page 4 of 4