0% found this document useful (0 votes)
2 views

CS2 5.3.2

This document outlines a module on event-controlled loops in programming, specifically focusing on their use in user input validation. It includes examples using C++ code to illustrate how these loops function, such as prompting users until they provide the correct input. The module aims to enhance understanding of event-controlled loops and their applications in programming tasks.

Uploaded by

kch.09082014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS2 5.3.2

This document outlines a module on event-controlled loops in programming, specifically focusing on their use in user input validation. It includes examples using C++ code to illustrate how these loops function, such as prompting users until they provide the correct input. The module aims to enhance understanding of event-controlled loops and their applications in programming tasks.

Uploaded by

kch.09082014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Subject Code: CS2 Introduction to Computational Thinking

Module Code: 5.0 More Logical Control Structure


Lesson Code: 5.3.2 Event-Controlled (Part 2)
Time Frame: 30 minutes

TARGET

After completing this module, you are expected to:


• Understand more uses of event-controlled loops
• Create a program using an event-controlled loop

HOOK Time Allotment: 2 minutes

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.

Image source: http://www.thedoghousediaries.com/2659

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.

NAVIGATE Time Allotment: 15 minutes

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

Event-controlled loops can be used to validate user input.

REFERENCES

CS 121. (n.d.). http://www.cs.uah.edu/~rcoleman/CS121/ClassTopics/Loops.html

Lab 7 - While Loop Construct. (n.d.). https://www.cs.mtsu.edu/~cs1170/manual/lab7/lab7.html#other

Loop control statements. (2014). Object- Oriented Programming in Python 1.0. https://python-
textbok.readthedocs.io/en/1.0/Loop_Control_Statements.html

Prepared by: Edlen Mari M. Sanchez Reviewed by : Trextan Thaddeus Sanchez


Position : SST I Position : SST III
Campus : PSHS- MC Campus : PSHS- SMC

© 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

You might also like