4 Control Structures in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

23CSE201

PROCEDURAL PROGRAMMING USING C


LOOPING IN C
• A looping is an important control structure in higher level language
programming.
• The process of repeatedly executing a collection of statement is called
looping. The main objective of looping is to provide repetition of the parts
of program.
• There are two types of loop structure supported by languages as follows ...
1. Entry control
2. Exit control
ENTRY CONTROL LOOP
• In entry control loop the condition is
checked first and then body is executed.
And the body of loop is not executed at
all if the condition is false.
• Example:
For loop and While Loop
Nested for loop
EXIT CONTROL LOOP
• In exit control loop body is executed
first and then condition is checked
Means if condition is false first time,
the body is executed at least once.
• Example:
Do While Loop
FOR LOOP
• A for loop is a more efficient loop structure which
is used to execute a sequence of statements
multiple times.
• Syntax:
FOR LOOP

• Initial value is executed first and only once. This step allows you to declare
and initialize any loop control variable.
• Next the condition is evaluated/tested. if it is true the body of the loop is
executed and if it is false the body of the loop doesn't executed and the flow
of control jumps to the next statement just after the ‘for loop’.
• After the body of the ’for’ loop executes the control jumps back up to the
increment/decrement statement. This statement allows you to update any
loop control variable.
Example of FOR LOOP
#include<stdio.h>
Output:
void main() 1
{ 2
3
int i; 4
for(i=1;i<=10;i++) 5
6
{
7
printf(“%d\n”,i); 8
} 9
10
}
WHILE LOOP
• C while loop statement allows to repeatedly run the same block of code until
a condition is met.
• While loop is a most basic loop in C programming. while loop has one
control condition and it’s executes as long as the condition is true.
• The condition of the loop is tested before the body of the loop is executed,
hence it is called an entry controlled loop.
WHILE LOOP
• Syntax:
Example of WHILE LOOP
#include<stdio.h>
Output:
void main() 1
{ 2
int i=1; 3
4
while(i<=10)
5
{ 6
printf(“%d\n”,i); 7
i++; 8
9
} 10
}
DO - WHILE LOOP
• Unlike for and while loop which test the condition at the top of the loop, the
do while loop in c programming checks its conditions at the bottom of the
loop that’s why it is exit control loop.
• The do while loop is also called post tested loop.
• A do while loop is similar to while loop with one exception, that is it
executes the statement inside the body of do while loop before checking
condition.
DO- WHILE LOOP
• Syntax:
Example of DO-WHILE LOOP
#include<stdio.h>
Output:
void main()
inside the loop
{ outside the loop
int i =0;
do
{
printf(“inside the loop”);
}
while(i==1);
printf(“\n outside the loop);
}
NESTED FOR LOOP
• The placing of one loop inside the body of another loop is called nesting.
• C programming allows us to nesting all types of loops, but the most commonly
nested loops are nested for loops. The outer loop takes control of the number of
complete repetitions of the inner loop.
• Syntax:
Example of NESTED FOR LOOP
#include<stdio.h>
void main()
{
int n=5,i,j;
for(i=1;i<=n;i++) { Output:
*
for(j=1;j<=i;j++) {
**
printf("* "); ***
} ****
printf("\n"); *****
}
}
Example of NESTED FOR LOOP
#include<stdio.h>
void main()
{
int n=5,i,j;
for(i=1;i<=n;i++) { Output:
1
for(j=1;j<=i;j++) {
12
printf("%d ",j); 123
} 1234
printf("\n"); 12345
}
}
INFINITE LOOP
• If the condition is given in such a logic that the repetition continues any number of
times with no fixed condition to stop looping those statements, then this type of
looping is called “infinite looping”.
• Example:
THANK YOU

You might also like