Lesson 6C - Handling Repeition - Do While Loop

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Grade 10

Information and communication Technology

Lesson 6c: handling repetitions _do-while loop /:/ Quarter 3/:/Week 3


MODULE CALENDAR / STUDENT STUDY GUIDE
2
GRADE TEN - ICT

Week: January 25– January 29


Day 1 Day 2
Grade 10-OLPH (January 25-10:00-11:00) Grade 10-OLPH Deadline of
Grade 10-OLL (January 26-11:00-12:00) (January 26-10:00-11:00) Performance
Grade 10-OLL Task :
(January 28 -8:00-9:00)

-Synchronous Session: -Asynchronous Session:


Grade 10-OLPH
What to do? What to do? (January 26-
10:00-11:00)
Join the synchronous meeting. The teacher will discuss and 1. Practice coding c++ programs.
demonstrate the C++ programming, lesson 6A- handling On the Discussion part of this Grade 10-OLL
(January 28 -8:00-
repetitions using do-while loop statements. module, there are examples of c+
9:00)
+ programs.
Note: If you cannot join the synchronous meeting, please refer the Encode, examine, and analyze
lessons to the given hand-outs and on the following resources and each program for you to develop
- Program 1
links: the skills of formulating c++ and 2
programs using the do-while - Submit the
Sources: loop statements. screenshot of
a. C++ programming fundamentals textbook pages ________. your program
2. Answer the Deepen part of the and output.
b. Tekteach LMS Lesson 6: Handling repetitions. module to test if you have
learned the skills of using the do- Please don’t
c. w3schools.com: while loop statement. forget to label
https://www.w3schools.com/cpp/cpp_do_while_loop.asp your
3. Do the performance task on submissions.
d . BeginnersBook.com : the demonstrate part of the
https://beginnersbook.com/2017/08/cpp-do-while-loop/ modules. Do Program No 1 and
2. Encode your program on
To learn more on do-while loop statement programs, code:blocks or any available c++
watch the following on YouTube: compiler apps at your hand.
Ad in 5 Once the program is successful,
a.CPP Do While Loop with Example | C++ Video Tutorial screenshot the codes and output
then submit to your subject
https://www.youtube.com/watch?v=IqKweotABQI
teacher.
b. C++ TUTORIAL: do-while Loop (TAGALOG):
https://www.youtube.com/watch?v=AnZRRYfHLhU

Lesson 6C: HANDLING REPETITION using do-while LOOP statement

As discussed in the last tutorial about while loop, a loop is used for repeating a block of statements until
the given loop condition returns false. In this module , we will use do-while loop. do-while loop is similar
to while loop, however there is a difference between them: In while loop, condition is evaluated first and
then the statements inside loop body gets executed, on the other hand in do-while loop, statements inside
do-while gets executed first and then the condition is evaluated.

After doing this module, you are expected to:

1. Identify the flow of control using do-while loop statement


2. Use the do-while loop statement to formulate loop statements
3. Create a program using the do-while loop statement to satisfy certain problem
3
INTRODUCTION

Do while loop is an exit controlled loop, meaning the test condition is verified after the execution
of the loop, at the end of the body of the loop. Hence, the body executes at least once, regardless of the
result of the test condition, whether it is true or false. This happens to be the foremost difference in
between while loop and do while. In while loop, the condition is tested beforehand, whereas in do while
loop the condition is verified at the finish of body of the loop.

 
DISCUSSION

do-while loop
First, the statements inside loop execute and then the condition gets evaluated, if the condition returns
true then the control jumps to the “do” for further repeated execution of it, this happens repeatedly until
the condition returns false. Once condition returns false control jumps to the next statement in the
program after do-while.

Syntax of do-while loop


do
{
statement(s);
}
while(condition);

Flow of Execution of the do-while loop


In do while loop, we end the body of the loop with a semicolon, whereas the other two loops do not have
any semicolon to end the body of their loops.

Example No. 1: This program will print the number of iteration and Hello World 3 times

#include <iostream>
using namespace std;

int main(){
Example explained:
int num=1;
(int num = 1) sets a variable before the loop starts, 1 is
do { initially stored to num.
cout <<num <<" Hello World! " <<endl;
num++; The blocks to be executed before the condition
}
while(num<=3); (num++) the iteration increases to 1 each time the code
return 0; block in the loop has been executed.
}
Defines the condition for the loop to run. If the condition is
true, the loop will to back to do statement, if it is false, the loop
Output: will end.

1 Hello World!
2 Hello World!
3 Hello World!

Example No. 2: This program will ask the user’s name and the number of times his/her name
will be displayed.

#include <iostream> Example explained:


using namespace std;
int main(){ Declare the variable for the name

string name; Declare and initialize the variable that indicates the
int num=0; numbers of times the name will be displayed
int i=1;
Declare and initialize the iteration variable
cout <<" What is your name? ";
cin>> name;
cout <<"\n How many times you would like to print your name ?" ;
cin>> num;

do { The statement that will display the name of the user


cout <<i <<" " << name <<endl;
i++; the iteration increases to 1 each time the code block in
} the loop has been executed.

while(i<=num); Defines the condition for the loop to run. If the condition is
return 0; true, the loop will to back to do statement, if it is false, the
loop will end.
Sample Output:

What is your name? Cathy


How many times you would like to print your name ? 3
1 Cathy
2 Cathy
3 Cathy
6
DEEPEN

C++ Exercises: Test yourself : Complete the program codes by filling-up the boxes.

1. Use a do-while loop to print numbers 1 to 10:

int i = 1;
{
cout << i << "\n";
;
}
(i < 11);

2. Use a do-while loop to print numbers 1 to 100 decreasingly:

int i = 100;
do {
cout << i << "\n";
;
}
while ;

DEMONSTATE

Performance Task. Apply what you have learned from the previous activities by creating the
program needed on the following situations.

Program No 1. Create a c++ program using d0-while loop statement that will display “ LOADING…...”once
the user entered the correct password.

Example output:

Please enter the password: CODES


LOADING …..

Program No 2. Create a c++ program using do-while statement that will display the following output:

Enter 2 numbers…
First number : 20
Second number : 10
20 x 10 = 200
Try Again? Enter YES if you want to try again and NO if you want to exit.

Note: The 2 numbers will be entered by the user.

Procedure:
1. Encode your program on C++ app. Build and run the program.
2. Screenshot your program and output then submit to your subject teacher.

You might also like