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

ITE 112 Module 7

Uploaded by

masayayhomestay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ITE 112 Module 7

Uploaded by

masayayhomestay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Module No. 7

LOOPS

I. Topic:

1. while loop
2. for loop
3. do-while loop
4. nested loops

II. Time Frame: 10 hours

III. Introduction:

The versatility of the computer lies in its ability to perform as set of instructions repeatedly.
This involves repeating some portion of the program either a specified number of times or until
a particular condition is being satisfied. This repetitive operation is done through a loop control
instruction.

IV. Objectives:
At the end of the lessons the student can:

1. Compare and contrast loop statements.

V. Pretest

Name: _________________________ Date: _______________

Course & Year: __________________ Score: ______________

Module 7 Pretest

1. What are the types of loops available in C?

2. Construct program that will display numbers from 1 to 9 in vertical direction using
while loop.

3. What is the output of this program?

main() {

int i=0;
for(i=1;i<=5;print(“\n%d”,i));
i++;
}

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 1


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

4. Determine the output of the following code.

int y, x=0;

do
{
y=x;

} while (x==0);

5. Make a program that will display the multiplication table.

VI. Learning Activities

DECISION MAKING

A loop statement allows us to execute a statement or group of statements multiple times. Given
below is the general form of a loop statement in most of the programming languages.

Flow Diagram

C programming language provides the following types of loops to handle looping requirements.

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 2


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

while Loop

A while loop in C programming repeatedly executes a target statement as long as a given


condition is true.

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

Flow Diagram

Here, the key point to note is that a while loop might not execute at all. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.

Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 3


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

/* while loop execution */


while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;
}

Ouput

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15s
value of a: 16
value of a: 17
value of a: 18
value of a: 19

for Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.
Syntax

for ( init; condition; increment ) {


statement(s);
}

Here is the flow of control in a 'for' loop −


 The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.
 After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 4


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

 The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.

Flow Diagram

Example

#include <stdio.h>

int main () {

int a;

/* for loop execution */


for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}

return 0;
}

Output

value of a: 10
value of a: 11
value of a: 12

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 5


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

do-while Loop

Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at
least one time.
Syntax
do {
statement(s);
} while( condition );

Notice that the conditional expression appears at the end of the loop, so the statement(s) in
the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the
loop executes again. This process repeats until the given condition becomes false.

Flow Diagram

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 6


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;
}

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Nested Loops

C programming allows to use one loop inside another loop. The following section shows a
few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement.

for ( init; condition; increment ) {

for ( init; condition; increment ) {


statement(s);
}
statement(s);
}

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 7


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

The syntax for a nested while loop statement

while(condition) {

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

The syntax for a nested do...while loop statement.


do {
statement(s);

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

}while( condition );

Final note on loop nesting is that you can put any type of loop inside any other type of loop.

Example
The following program uses a nested for loop to find the prime numbers from 2 to 100
#include <stdio.h>

int main () {

/* local variable definition */


int i, j;

for(i = 2; i<100; i++) {

for(j = 2; j <= (i/j); j++)


if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) printf("%d is prime\n", i);
}

return 0;
}

Output
2 is prime
3 is prime

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 8


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

VII. Review of Concept:

while loop tests the condition before executing any of the statements within the loop.

for loop allows programmer to specify three things about a loop in a single line.

- Setting a loop counter to an initial value


- Testing te loop counter to determine whether its value has reached the
number of repetitions desired.
- Increasing the value of loop counter each time the program segment

do-while loop tests the condition after having executed the statements within the loop
has been exuted

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 9


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

VIII. Post Test

Name: ______________________________ Date: _______________


Course & Year: ______________________ Score: ______________

Module7 Post Test

1. What are the types of loops available in C?

2. Construct program that will display numbers from 1 to 9 in vertical direction using
while loop.

3. What is the output of this program?

main() {

int i=0;
for(i=1;i<=5;print(“\n%d”,i));
i++;
}

4. Determine the output of the following code.

int y, x=0;

do
{
y=x;

} while (x==0);

5. Make a program that will display the multiplication table.

IX: References

 https://www.tutorialspoint.com/cprogramming/c_loops.htm

ITE 112 – FUNDAMENTAL PROGRAMMING (Shem L. Gonzales) 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy