0% found this document useful (0 votes)
50 views7 pages

Looping / Iterative Statements: While Loop

1. The document discusses different types of looping statements in C programming including while loops, do-while loops, and for loops. It provides examples to illustrate how each type of loop works. 2. Nested for loops allow loops to be placed within other loops, enabling the generation of matrices through an outer loop to count rows and an inner loop to count columns. 3. Key looping concepts covered are the evaluation of loop conditions, using initialization and increment/decrement statements, and ensuring at least one iteration with do-while loops. Examples generate sequences, calculate sums and factorials, and output characters.

Uploaded by

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

Looping / Iterative Statements: While Loop

1. The document discusses different types of looping statements in C programming including while loops, do-while loops, and for loops. It provides examples to illustrate how each type of loop works. 2. Nested for loops allow loops to be placed within other loops, enabling the generation of matrices through an outer loop to count rows and an inner loop to count columns. 3. Key looping concepts covered are the evaluation of loop conditions, using initialization and increment/decrement statements, and ensuring at least one iteration with do-while loops. Examples generate sequences, calculate sums and factorials, and output characters.

Uploaded by

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

Looping / Iterative Statements

At the end of the session, students should be able to:


1. Impose programming looping statements on solutions to programming problems
2. Use looping statements to execute statements in a program repetitively

Lecture:

Often times there is a need to perform an action over and over repeatedly 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);
}
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.

Example
1. Write a program that generates the given sequence nos.
Sequences nos.
1
2
3
4
5
#include<stdio.h>
main()
{
int n;
clrscr();
printf(“While Loop Example –Sequence Nos. 1 – 5”);
n=1;
while (n<=5)
{
printf(“\n %d”, n);
n++;
}
getch();
}

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.

2. Write a program that will count the number of input numbers and display their sum
until 0 is entered.
#include<stdio.h>
main()
{
int count, num, sum;
clrscr();
printf(“Input numbers: \n”);
scanf(“%d”, &num);
count=0;
sum=0;
while (num!=0)
{
sum = sum + num;
scanf(“%d”, &num);
count++;
}
printf(“The total input number is: %d\n”, count);
printf(“The sum of all numbers: %d\n”, sum);
getch();
}
3. Write a program that will count the number of input numbers and display their sum,
press q to quit.

#include<stdio.h>
main()
{
int count=0, sum=0, num;
char quit;
clrscr();
printf("\n\nEnter a number [Press q to quit]: ");
scanf("%d",&num);
while((quit!='q')&&(quit!='Q'))
{
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 );

Example:
1. Display the numbers 1 to 5 in descending orders

#include<stdio.h>
main()
{
int n;
clrscr();
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();
}

2. Computes the factorial value of n(as input) and displays it


#include<stdio.h>
main()
{
int f,n,i;
clrscr();
printf(“\nEnter a number: ”);
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);
}
Here is the flow of control in a ‘for’ loop:
1. 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.
2. 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.
3. 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.

Example:

1. Display the lowercase alphabet a to z.


#include<stdio.h>
#include<conio.h>
main()
{
char ch;
for(ch='A';ch<='z';ch++)
{
printf("%c\n",ch);

getch();
}

2. Count the number for 10 boys whose weight is less than 50 kgs and height is
greater than 170 cm.
#include<stdio.h>
main()
{
int count, i;
float weight, height;
count = 0;
printf("Enter weight and height for 10 boys\n");
for (i =1; i <= 10; i++)
{
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();
}

The 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.

Syntax
for ( init; condition; increment/decrement )
{
statement(s);
for ( init; condition; increment/decrement )
{
body of inner loop;
}
statement(s);

Example: Write a program to generate a matrix of order 4*4 containing


symbol*(asterisk).
#include<stdio.h>
main ( )
{
int i, j;
clrscr ( );
for (j=1; j<=4; j++) /*outer for loop*/
{
for (i=1; i<=4; i++) /*inner for loop*/
{
printf (“*”)
}
printf (“\n”);
}
getch ( );
}

You might also like