STE Computer Programming Q3 MODULE 4
STE Computer Programming Q3 MODULE 4
Computer
Programming
Quarter III – Module 4:
Counting-Based Loops
"Designed by macrovector /
Freepik"
"Designed by macrovector /
Freepik"
Computer Programming– Grade 10
Self-Learning Module
First Edition, 2020
Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for exploitation of such work for profit.
Such agency or office may, among other things, impose as a condition the payment of
royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from
their respective copyright owners. The publisher and authors do not represent nor claim
ownership over them.
continue your studies and learn while at home. Activities, questions, directions,
exercises, and discussions are carefully stated for you to understand each lesson.
Each SLM is composed of different parts. Each part shall guide you step-by-
step as you discover and understand the lesson prepared for you.
At the end of each module, you need to answer the test to self-check your
learning. Answer keys are provided for each activity and test. We trust that you will be
In addition to the material in the main text, Notes to the Teacher are also
provided to our facilitators and parents for strategies and reminders on how they can
Please use this module with care. Do not put unnecessary marks on any part of
this SLM. Use a separate sheet of paper in answering the exercises and tests. And
If you have any questions in using this SLM or any difficulty in answering the
Thank you.
ii
For the learner:
Loops!
The hand is one of the most symbolized part of the human body. It is often used to
depict skill, action, and purpose. Through our hands we may learn, create, and
accomplish. Hence, the hand in this learning resource signifies that you as a learner is
capable and empowered to successfully achieve the relevant competencies and skills at
your own pace and time. Your academic success lies in your own hands!
This module was designed to provide you with fun and meaningful opportunities for
guided and independent learning at your own pace and time. You will be enabled to
process the contents of the learning resource while being an active learner.
iii
understand new concepts and skills.
this module.
1. Use the module with care. Do not put unnecessary mark/s on any part of the
3. Observe honesty and integrity in doing the tasks and checking your answers.
iv
4. Finish the task at hand before proceeding to the next.
5. Return this module to your teacher/facilitator once you are through with it.
If you encounter any difficulty in answering the tasks in this module, do not hesitate
to consult your teacher or facilitator. Always bear in mind that you are not alone.
We hope that through this material, you will experience meaningful learning and
v
Explore
Introduction:
You can do the same thing with the if...else..if ladder. However, the syntax of
This user friendly module will guide in your learning & understanding of the
different parts of a Computer Program. The module will permit you use your different
learning in diverse programming languages. We used the language that is appropriate for
your grade level. The lessons were designed to follow the standard SLM format.
Have you ever wondered about the ever-growing world? Have you been
fascinated by the massive field of Technology? This introductory module will give you
insights in the diversities in the field of the different parts and components of computer
software. We will explore the different paradigms in Software Development and engage
you to concepts that will enable you to study, utilize and master any programming
language you will like in the future.
Sub-Task:
PRE-ASSESSMENT
Multiple Choice. Choose the letter of the best answer. Write the chosen letter on a
separate sheet of paper.
1. Which of the following best describes counting – based loops?
2. Which of the following are used to define loops that are executed a finite number of
times based on the value of the counting variable?
a. Do While Loop b. FOR loops
c. Condition checked at the end d. While Loop
3. What happened to the execution of loops if the value of i becomes greater than k?
a. the execution of the loop continue b. Condition checked at the beginning
c. the execution of the loop stops d. Do While Loop
Usually you want a top-driven loop such as the one at right, where the test is
performed at the top before control enters the loop body.
Be clear about the loop you want before you program it,
because assembly language allows any sort of weird loop.
Counting-Based Loops
Counting-based loops execute the set of statements for a fixed number of times based
on the definition at the beginning. Most modern programming languages use the For ...
Next construct for using this type of loop. “FOR” loops are the primary tools in handling
arrays in programs. Generally, the FOR loop has the following syntax:
For i = j to k Step l
Statement 1
Statement 2
Statement n
Next
In the earlier example, i, j, k, l, and n are numeric variables. The variable j denotes the
initial number at which the loop begins, and k is the last number at which the loop stops.
Once the value of i becomes greater than k, the execution of the loop stops. The
keyword “For” signifies the beginning of the loop. The keyword “Step” indicates the
value by which the variable i would be incremented after every iteration. The keyword
“Next” indicates incrementing the value of the variable i by the amount of the value
indicated after the keyword Step.
The variable i would assume an initial value of j and executes the statements in the
loop block. The set of statements in the loop block would be executed even when the
value of the variable i becomes equal to k. Therefore, the number of times the set of
statements, in the loop block, are executed, is equal to (k − j + 1). For example, if the
statement is “For i − 1 to 6 Step 1,” then it would be executed (6 − 1 + 1 = 6) times. Let
us consider an example. Let us assume a single-dimensional array having six cells. Let
us fill this array using a FOR loop:
For i = 0 to 5 Step 1
Read m
arr(i) = m
Next
In the earlier example, i is a counting variable and m is the input variable. Note that we
began the loop with the initial value of zero. Generally, the first element of an array is
denoted as 0th element/cell. With the value of i being incremented by 1 in each of its
iterations, it assumes the values of 0, 1, 2, 3, 4, and 5. As you can see, the loop would be
executed six times to fill the array with values received as input. In each of the iterations,
the computer performs two operations; namely, it receives one value as input and fills
the corresponding array element with that value. When i is incremented and reaches a
value of 6, the execution of the loop stops and moves to the next statement in the
program. Let us now consider a two-dimensional array and fill it with values input by the
user. Let arr be a two-dimensional array with 3 columns and 6 rows. Here is the pseudo
code for it:
For i = 0 to 5
For j = 0 to 2
Read m
arr(i, j) = m
Next
Next
Control Statements
In the earlier example, i and j are counters. The variable m is the input variable. The first
FOR loop sets the value of the row of the array into which the values are being filled. The
second FOR loop sets the value of the array column for filling the value. The statement
arr(i, j) indicates the array element where i is the row number and j is the column number.
If i = 2 and j = 2, then the value goes into row #3 and column #3 (as the computer
counts from zero and we count from 1!).
Here are the general rules for the FOR loop.
1. FOR loops are used to define loops that are executed a finite number of times based
on the value of the counting variable.
2. A FOR loop needs a counter, a numeric variable used for counting the number of
iterations executed by the loop.
3. It also needs an incrementing value/variable to increment the counter by after
completing every iteration. If we do not define an incrementing variable, most
computers assume the increment to be 1.
4. The FOR loop needs some indicator to indicate the last statement in the loop. Some
programming languages use Next, and some simply use a closing curly brace—“}.”
5. The FOR loop can be blank, that is, there need not be any statements between the For
statement and the Next statement. Such loops are called as delay loops and are used
sometimes in the programs. It just delays the program execution for the time it spends
in counting the numbers specified in the loop.
FOR loops are used mostly in programs solving mathematical problems. Matrix algebra,
factorials, prime number generation, random number generation, and others that require
the repeated execution of a set of statements when the number of times those
statements need execution is known beforehand. Of course, these can be used in other
areas too, but the prerequisite to use this construct is that we need to know the number
of times the set of statements needs to be executed.
While it is common to use numeric constants in the FOR loop for the counter and the
step, it is better to use variables and input the values using a data file. This helps in
software maintenance when the values need change. Then we can simply change the
data file without resorting to a program change, which is more tedious than changing the
data.
Engage
For i = j to k Step l
Statement 1
Statement 2
Statement n
Next
For i = 0 to 5 Step 1
Read m
arr(i) = m
Next
7. assume an initial value of j and executes the statements in the loop block = ___
8. is the input variable = ___
9. the first element of an array is denoted as = __
10. used to define loops that are executed a finite number of times based on the value of
the counting variable = ______
Apply
Activity 2
INSTRUCTION: Code Me!
1. In your laptop, desktop computer or cell phone, write a simple code and apply
counting-based loop statement;
For smartphone (use android version of any computer programming
language)
2. Then write your explanation in a separate paper. Five (5) points each.
3. The activity will be scored using the Rubrics below:
Example:
CODE Programming Language
#include <stdio.h>
main()
{
int i,j,num,k,count;
printf("enter num of rows\n");
scanf("%d",&num);
count=num-1;
for(i=1;i<=count;i++)
{
for(k=1;k<=count;k++)
{
printf(" ");
C language
}
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
count--;
}
}
Explanation of the Output:
Use the Rubrics in evaluating the code:
Trait 5 4 3 2
Excellent Good Fair Poor
The program works The program works and The program The program is
and meets all of the produces the correct produces correct producing incorrect
specifications. results and displays them results but does results.
Specifications
correctly. It also meets not display them
most of the other correctly.
specifications.
The code is . The code is The code is poorly
exceptionally well The code is fairly easy to readable only by organized and very
organized and very read. someone who difficult to read.
Readability
easy to follow. knows what it is
supposed to be
doing.
The documentation is The documentation The documentation The documentation is
well written and consists of embedded is simply comments simply comments
clearly explains what comment and some simple embedded in the embedded in the
Documentation
the code is header documentation that code with some code and does not
accomplishing and is somewhat useful in simple header help the reader
how. understanding the code. comments understand the code.
separating routines.
Assess
Multiple Choice. Choose the letter of the best answer. Write the chosen letter on a
separate sheet of paper.
_________________________________________________________________
_________________________________________________________________.