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

Lecture Looping and Machine Problem 3

1. The document discusses different looping statements in C including for, while, and do-while loops. It provides examples of how to write each type of loop and examples of programs using loops to iterate through and display sets of numbers. 2. Key aspects of loops covered include using initialization, condition, and incrementation in for loops to execute a fixed number of times. While and do-while loops execute based on a condition being true or false. Nested loops can execute one loop multiple times inside another loop. 3. Examples show using loops to display integers, factorials, patterns of asterisks, and perform calculations by iterating through input values.

Uploaded by

Mark
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lecture Looping and Machine Problem 3

1. The document discusses different looping statements in C including for, while, and do-while loops. It provides examples of how to write each type of loop and examples of programs using loops to iterate through and display sets of numbers. 2. Key aspects of loops covered include using initialization, condition, and incrementation in for loops to execute a fixed number of times. While and do-while loops execute based on a condition being true or false. Nested loops can execute one loop multiple times inside another loop. 3. Examples show using loops to display integers, factorials, patterns of asterisks, and perform calculations by iterating through input values.

Uploaded by

Mark
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Looping Statements

In order to repeat a statement multiple times, a control


structure known as loops are to be used. Loop allows a set of
instructions to repeat until a certain condition is reached. C
supports the same type of loop as other modern structured
languages.
A for-loop executes a set statement/s a fixed number of
times. It is used typically when you known in advanced how many
times you want to execute these statements.
C looping statements are written in the following format:

a. for-loop
for (initialization; condition; incrementation)
{
<statements/s>;
}

where:
Initialization – an assignment statement that is used to set the
loop control variable.
Condition – a relational expression that determines when the loop
will exist by testing the loop-control variable
against some values.
Incrementation – defines how the loop-control variable will
change each time the loop is repeated.

In an event-controlled loop, the number of times that the


loop is executed is not predetermined. Instead, termination of the
loop is triggered by the occurrence of some event. The while loop
and do/while loop does this kind of operation.

b. while-loop
while (<condition>) {
<statement/s>;
}

where:
statement – can be an empty statement, a single statement or a
block of statement that is to be repeated.
conditions – may be any valid expression. The loop iterates while
the condition is true and program control passes to
the next line following the loop if the condition is
false.
Another way of creating a loop that is not counter controlled
is to use the do-while loop. It is convenient when at least one
repetition of loop body must be ensured.

c. do/while-loop
do{
<statement/s>;
} while (<condition>);

Unlike the for and while loop, the do/while lop checks the
condition at the bottom of the loop. This means that a do/while
loop while always execute at least once. Its common use is in a
menu selection routine by testing a valid response at the bottom of
the loop, where can be reprompt the user until a valid response is
entered.

Nested loop
When one loop is inside of another, the loop is said to be
nested. It provides a means of solving some interesting
programming problems. It is sometimes important to note how
much iteration a nested loop executes. This number is determined
by multiplying the number of times the outer loop iterates by the
number of times the inner loop repeats each time it is executed.

Example 1. Create a program that will display integers 1 to 10 on


the screen using for loops.

#include<stdio.h>
#include<conio.h>
int ctr;
main()
{
for (ctr=1; ctr<=10; ctr++)
printf("%d \n",ctr);
getch();
}

Example 2. Make a program that will display integers 25, 21, 17,
13, 9 using while loops.
#include<stdio.h>
#include<conio.h>
int ctr;
main()
{
ctr=25;
while (ctr>=9)
{
printf("%d \n",ctr);
ctr = ctr - 4;
}
getch();
}

Example 3. Design a C code that will display all numbers divisible


by 11 from 100 down to 1 using do-while loops.
#include<stdio.h>
#include<conio.h>
int ctr;
main()
{
ctr=100;
do
{
if (ctr % 11 == 0)
printf("%d \n",ctr);
ctr = ctr - 1;
}while(ctr>=1);
getch();
}

Example 4. Display the following using nested loops:


*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
#include <conio.h>
int a,b;
main()
{
for (a = 1; a <= 5; a++)
{
for (b = 1; b <=a; b++)
printf(“ * ”);
printf(“\n”);
}
getch();
}

Example 5. Create a C program that will input any integer from 1


to 7 and display its factorial using looping.

#include<stdio.h>
#include<conio.h>
int num,ctr,factorial;
main()
{
printf("Enter number [1-7]: ");
scanf("%d",&num);
if (num>=1 && num<=7)
{
factorial=1;
for(ctr=num; ctr>=1; ctr--)
{
factorial=factorial * ctr;
}
printf("Factorial is %d",factorial);
}
else
printf("Overflow!");
getch();
}

Machine Problems#3 (MP#3):

1. Write a code for the following display using for loops.


a. 9 b. 117 c. 1
12 104 1
15 91 2
18 78 4
21 65 7
24 52 11
27 39 16
30 26 22
33 13 29
36 0 37

2. Design a code for the following display using while loops.


a. 14 b. 2 c. 1
13 6 1
12 12 2
11 20 3
10 30 5
9 42 8
8 56 13
7 72 21
6 90 34
5 110 55

3. Create a code for the following display using do-while loops


a. 100 b. 6 c. 64
82 16 32
66 25 16
52 33 8
40 40 9
30 46 27
22 51 81
16 55 243
12 58 729
10 60 2187

4. Build a code for the following display using nested loops.


a. ***** b. *
**** **
*** ***
** ****
* *****

c. 5 4321 d. 1 2345
5 432 1 234
5 43 1 23
5 4 1 2
5 1

5. Using nested loops make a program that will do the following


displays.
a. 1 b. 2
2 3
3 4
2 3
4 4
6 5
3 4
6 5
9 6

6. Create a C program that will display “I will pass NCE 1102” 10


times using any loops.

7. Design a program using C that will input 20 integers and display


the sum using any loops.

8. Write a code that will loop by inputting any integer and displays
whether the integer is “POSITIVE” or “NEGATIVE”, and terminate
the loop if the integer is equal to zero using any loops.

9. Build a program that will display all prime numbers from 1 to


1000 using any looping statement.

10. Design a program that will input ten integers and will compute
and display the sum and average.

Prepared By: Dr. Joan P. Lazaro

You might also like