KEMBAR78
Module 5.2 - Looping (Iterative Statements) | PDF | Control Flow | Php
0% found this document useful (0 votes)
10 views20 pages

Module 5.2 - Looping (Iterative Statements)

Uploaded by

2301710
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)
10 views20 pages

Module 5.2 - Looping (Iterative Statements)

Uploaded by

2301710
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/ 20

CMPRGE

C Programming
Looping
(Iterative Statements)
Learning Objectives:
At the end of the session, students should be
able to:
• Impose programming looping statements on
solutions to programming problems
• Use looping statements to execute
statements in a program repetitively
Loops
In the programming often there is a need to
perform an action over and over or repeatedly,
often with variations in the details each time
we repeat. This repetitive operation is done
through the control structure called loop or
iteration. There are three types of looping,
namely:
• while loop
• do-while loop
• for loop
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);
}
while Loop
The condition expression (something like (a >
b) etc...) is evaluated first. If the expression
evaluates to True the looping continues; that
is, the statements inside the statement block
are executed. After the execution, the
expression is evaluated again. The process is
repeated over and over until the expression
evaluates to false.
while Loop
Write a program #include<stdio.h>
main()
that generates the {
given sequence int n;
clrscr();
nos. printf(“While Loop Example –Sequence Nos.
1 – 5”);
Sequences nos.
n=1;
1 while (n<=5) {
printf(“\n %d”, n);
2 delay(3000);
3 n++;
}
4 getch();
}
5
while Loop
• The initial value for n is 1. The program pointer evaluates
the condition expression n<=5. Since n has a initial value
of 1, therefore the condition is evaluated to be true. Once
the condition is evaluated to true, all the statements within
the body of the loop are executed.
• The program pointer iterates(loop) all over again until the
condition is evaluated and tested to false. This is the only
time the loop process stops executing the statements
within the body of the loop.
• The delay (3000) animate the looping or iteration process.
The value inside the parenthesis of the delay() function
means the computer would delay or pause the execution
for about 3000 milliseconds before proceeding to the next
iteration process.
while Loop
Write a program #include<stdio.h>
main()
that will count the {
int count, num, sum;
number of input clrscr();
printf(“Input numbers: \n”);
numbers and scanf(“%d”, &num);
display their sum, count=0;
sum=0;
for the loop to while (num!=0)
{
terminate, 0 sum = sum + num;
scanf(“%d”, &num);
number is used. count++;
}
printf(“The total input number is: %d\n”, count);
printf(“The sum of all numbers: %d\n”, sum);
getch();
}
while Loop
#include<stdio.h>
Write a program main()
{
that will count the int count=0, sum=0, num;
char quit;
number of input clrscr();
numbers and printf("\n\nEnter a number [Press q to quit]: ");
scanf("%d",&num);
display their sum, while((quit!='q')&&(quit!='Q'))
{
press q to quit. sum=sum+num;
count++;
printf("\nEnter a number [Press q to quit]:
");
scanf("%d",&num);
quit=getchar();
}
printf("\n\nThe total input number is %d\n", count);
printf("\n\nThe sum of all numbers: %d",sum);
getch();
}
do-while Loop
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 );
do-while Loop
#include<stdio.h>
Display the main()
{
numbers 1 to 5 in int n;
clrscr();
descending orders. printf(“do while Loop Example –Nos. 1 –
5 in descending orders”);

n=5;
do {
printf(“\n %d”, n);
delay(3000);
n--;
} while (n>=1);

getch();
}
do-while Loop
#include<stdio.h>
Computes the main()
{
factorial value of int f,n,i;
clrscr();
n(as input) and printf(“\nEnter a number: ”);
displays it. scanf(“%d”, &n);

f=1;
i=n;
do {
f=f*i;
i--;
} while (i>=1);

printf(“\n The factorial value: %d”,f);


getch();
}
do-while Loop
#include<stdio.h>
Computes the main()
{
factorial value of int f,n,i;
clrscr();
n(as input) and printf(“\nEnter a number: ”);
displays it. scanf(“%d”, &n);

f=1;
i=n;
do {
f=f*i;
i--;
} while (i>=1);

printf(“\n The factorial value: %d”,f);


getch();
}
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/decrement )
{
statement(s);
}
for Loop
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/decrement 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.
for Loop
#include<stdio.h>
Display the int main()
lowercase alphabet {
clrscr();
a to z. char ch;

for(ch='A';ch<='z';ch++) {
printf("%c\n",ch);
delay(300);
}

getch();
return 0;
}
for Loop
#include<stdio.h>
Count the number main()
for 10 boys whose {
int count, i;
weight is less than float weight, height;
count = 0;
50 kgs and height is printf("Enter weight and height for 10 boys\n");
for (i =1; i <= 10; i++)
greater than 170 {
cm. scanf("%f %f", &weight, &height);
if (weight < 50 && height > 170)
count = count + 1;
}
printf("Number of boys with weight < 50
kgs\n");
printf("and height > 170 cm = %d\n", count);
getch();
}
Nested for Loop
The concept of using a loop within a loop is
called nested loop. If a for loop contains
another for loop statement, such loop is
called nested for loop. A nested for loop can
contain any number of for loop statements
within itself. Usually only two loops are used.
In this the first loop is called outer loop and
the second is called inner loop. These types of
loops are used to create matrix. In this the
outer loop is used for counting rows and the
internal loop is used for counting columns.
Nested for Loop
Syntax:

for ( init; condition; increment/decrement ) {


statement(s);
for ( init; condition; increment/decrement ) {
body of inner loop;
}
statement(s);
}
Nested for Loop
#include<stdio.h>
Write a program to main ( )
generate a matrix of {
int i, j;
order 4*4 clrscr ( );

containing symbol * for (j=1; j<=4; j++) /*outer for loop*/


{
(asterisk). for (i=1; i<=4; i++) /*inner for loop*/
{
printf (“*”)
}
Output: printf (“\n”);
**** }

**** getch();
return 0;
**** }

****

You might also like