You are on page 1of 5

Activity No.

4 – Loop Control Structure (For and Do-While)


Course Code: COMP1/CPTR1L Program:
Course Title: Computer Programming 1 Date Performed:
Name: Date Submitted:
Section: Instructor: Gemma D. Belga
1. Objective:

This activity aims to develop programming skills using loop control structures for and do-while.
2. Intended Learning Outcomes (ILOs):
The students should be able to:
2.1 Explain the for and do/while repetition control structures.
2.2 Apply the for and do/while repetition structures in the algorithm exercises.
3. Discussions

The FOR Repetition Structure


The for repetition structure handles all the details of counter-
controlled repetition automatically. Below is an illustration of
the power of for.

The general format of the for structure is


for ( expression1; expression2;
expression3) statement

where expression1 initializes the loop’s control variable,


expression2 is the loop-continuation condition, and
expression3 increments the control variable. In most cases,
the for structure can be represented with an equivalent while
structure as follows:

expression1;
while (expression2)
{ statement
expression3;
}
**As discussed from Laboratory Activity 3.

The following expressions for increment are equivalent:


counter = counter + 1
counter += 1
++counter
counter++

Many programmers prefer to use the form counter++ because the incrementing occurs after the loop
body is executed.
Exercise: Try to create an output of the following:
1. for (i = 1; i <= 10; i++)
2. for (i = 100; i>= 1; i--)
3. for (i = 7; i <=77; i += 7)
4. for (i = 20; i >=2; i-= 2)
5. for (i = 2; i <= 20; i+=3)
6. for (i = 99; i >= 0; i -= 11)

The do/while Repetition structure


The do/while repetition structure is similar to while
structure. In the while structure, the loop-continuation
condition is tested at the beginning of the loop before the
body of the loop is performed.
The do/while structure tests the loop-continuation
condition after the loop body is performed, therefore, the
loop body will be executed at least once. When a do/while
terminates, execution continues with the statement after the
while clause. Note, that it is necessary to use braces in the
do/while structure if there is only one statement in the body.

However, the braces are usually included to avoid


confusion between the while and do/while structures. For
example,

while ( condition)

is normally regarded as the header to a while structure. A do/while with no braces around the single
statement body appears as

do
statement
while (condition);

which can be confusing. The last line - while (condition); may be misinterpreted by the reader as a while
structure containing an empty statement. Thus, the do/while with one statement is often written as follows
to avoid confusion:

do {
statement
} while (condition);
4. Resources:
Personal Computer
Word App
Visio Software
5. Activities:
Prepare a pseudo code, flowchart, and create a program using do-while and for statements on the
following problems:

1. Print Hello World 10 times.

2. Log in to facebook account.

To log in to facebook account we first enter the facbook URL www.facebook.com in our browser
like Google, Firefox, Safari, Internet Explorer etc. This request is sent to the facebook server and
it responds by sending us the home page of facebook.

Next, we enter our registered Email ID and Password and click the Login button.
Then our login credential is checked. If it is correct, we are show our profile. On the other hand, if
the login credential is wrong then an error occurs and we are prompted to re-enter our Email ID and
Password. There are only 3 tries to log in the facebook account. In case after the third try log-in
credentials are still wrong, send an error message and terminate the process.
6. Assessment (Rubric for Laboratory Performance):

You might also like