Lecture 7 - Program Control Structures - Loops
Lecture 7 - Program Control Structures - Loops
Lecture 7 - Program Control Structures - Loops
Methodologies
Lecture 7 – Loops
C Loops
2
The looping can be defined as repeating the same process multiple times
until a specific condition satisfies. There are three types of loops used in the
C language.
do while
while
for
The looping simplifies the complex problems into the easy ones. It enables
us to alter the flow of the program so that instead of writing the same code
again and again, we can repeat the same code for a finite number of
times.
Example
For example, if we need to print the first 10 natural numbers then, instead
of using the printf statement 10 times, we can print inside a loop which runs
up to 10 iteration
Advantage of loops in C
3
The do-while loop continues until a given condition satisfies. It is also called
post tested loop. It is used when it is necessary to execute the loop at least
once (mostly menu driven programs).
Syntax
Do
{
//code to be executed
}while(condition);
While Loop
5
The while loop in c is to be used in the scenario where we don't know the
number of iterations in advance.
The block of statements is executed in the while loop until the condition
specified in the while loop is satisfied. It is also called a pre-test loop.
Syntax
while(condition)
{
//code to be executed
}
6
#include<stdio.h>
int main(){
int i=1;
while(i<=10)
{
printf("%d \n",i);
i++;
}
return 0;
}
Example: Program to print table for the
given number using while loop
7
#include<stdio.h> #include<stdio.h>
int main(){ void main ()
int i=1,number=0; {
printf("Enter a number: "); int j = 1;
scanf("%d",&number); while(j+=2,j<=10)
while(i<=10) {
{ printf("%d ",j);
printf("%d \n",(number*i)); }
i++; printf("%d",j);
} }
return 0;
}
Properties of while loop
8
The for loop is used in the case where we need to execute some part of the
code until the given condition is satisfied.
The for loop is also called as a per-tested loop. It is better to use for loop if
the number of iteration is known in advance.
Syntax
for(initialization; condition; incr/decr)
{
//code to be executed
}
10
The do while loop is a post tested loop. Using the do-while loop, we can
repeat the execution of several parts of the statements.
The do-while loop is mainly used in the case where we need to execute the
loop at least once.
The do-while loop is mostly used in menu-driven programs where the
termination condition depends upon the end user.
Syntax
do{
//code to be executed
}while(condition);
12
The break is a keyword in C which is used to bring the program control out
of the loop.
The break statement is used inside loops or switch statement.
The break statement breaks the loop one by one, i.e., in the case of nested
loops, it breaks the inner loop first and then proceeds to outer loops.
The break statement in C can be used in the following two scenarios:
With switch case
With loop
Syntax:
//loop or switch case
break;
14
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
C continue statement
15
#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}