Loops in C
Definition
 In any programming language, loops
are used to execute a set of
statements repeatedly until a
particular condition is satisfied.
 The loops in C language are used to
execute a block of code or a part of
the program several times.
 In other words, it iterates a code or
group of code many times.
Why use loops in C language?
 Suppose that you have to print table of
2, then you need to write 10 lines of
code.
 By using the loop statement, you can
do it by 2 or 3 lines of code only.
Advantage of loops in C
 1) It saves code.
 2) It helps to traverse the elements of
array.
How it works?
A sequence of statements are executed until a specified
condition is true. This sequence of statements to be executed is
kept inside the curly braces { } known as the Loop body. After
every execution of loop body, condition is verified, and if it is
found to be true the loop body is executed again. When the
condition check returns false, the loop body is not executed.
Types of Loops in C
There are 3 types of loops in C
language:
 while loop
 for loop
 do-while loop
While Loop
while loop can be addressed as
an entry control loop. It is completed
in 3 steps:
 Variable initialization.( e.g int x=0; )
 condition( e.g while( x<=10) )
 Variable increment or decrement ( x++
or x-- or x=x+2 )
 Syntax:
Flowchart:
Example
 Program to print first 10 natural
numbers
Program to print table for the
given number using while loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
printf("%d n",(number*i));
i++;
}
getch();
}
Infinitive while loop in C
 If you pass 1 as a expression in while
loop, it will run infinite number of
times.
For Loop
 for loop is used to execute a set of
statements repeatedly until a
particular condition is satisfied. we can
say it an open ended loop.
 Syntax:
 In for loop we have exactly two semicolons,
one after initialization and second after
condition. In this loop we can have more than
one initialization or increment/decrement,
separated using comma operator. for loop
can have only one condition.
 The for loop is executed as follows:
◦ It first evaluates the initialization code.
◦ Then it checks the condition expression.
◦ If it is true, it executes the for-loop body.
◦ Then it evaluate the increment/decrement
condition and again follows from step 2.
◦ When the condition expression becomes false, it
exits the loop.
Flowchart:
Example:
 Program to print first n natural
numbers
Print table for the given number
using for loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d n",(number*i));
}
getch();
}
Nested For loop
 We can also have nested for loops, i.e
one for loop inside another for loop.
 Syntax:
Example:
 Program to print half pyramid of
numbers
Infinitive for loop in C
 If you don't initialize any variable, check
condition and increment or decrement
variable in for loop, it is known as infinitive
for loop.
 In other words, if you place 2 semicolons in
for loop, it is known as infinitive for loop.
 If you run this program, you will see above
statement infinite times.
Do While Loop
 In some situations it is necessary to execute
body of the loop before testing the condition.
Such situations can be handled with the help
of do-while loop. do statement evaluates the
body of the loop first and at the end, the
condition is checked using while statement. It
means that for at least one time ,the body of
the loop will be executed, even though the
starting condition inside while is initialized to
false.
 Syntax:
Flowchart:
Example:
 Program to print first ten multiples of
5.
Program to print table for the
given number using do while
loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d n",(number*i));
i++;
}while(i<=10);
getch();
}
Infinitive do while loop
If you pass 1 as a expression in do
while loop, it will run infinite number of
times.

Loops in c language

  • 1.
  • 2.
    Definition  In anyprogramming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.  The loops in C language are used to execute a block of code or a part of the program several times.  In other words, it iterates a code or group of code many times.
  • 3.
    Why use loopsin C language?  Suppose that you have to print table of 2, then you need to write 10 lines of code.  By using the loop statement, you can do it by 2 or 3 lines of code only. Advantage of loops in C  1) It saves code.  2) It helps to traverse the elements of array.
  • 4.
    How it works? Asequence of statements are executed until a specified condition is true. This sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed.
  • 5.
    Types of Loopsin C There are 3 types of loops in C language:  while loop  for loop  do-while loop
  • 6.
    While Loop while loopcan be addressed as an entry control loop. It is completed in 3 steps:  Variable initialization.( e.g int x=0; )  condition( e.g while( x<=10) )  Variable increment or decrement ( x++ or x-- or x=x+2 )  Syntax:
  • 7.
  • 8.
    Example  Program toprint first 10 natural numbers
  • 9.
    Program to printtable for the given number using while loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); while(i<=10) { printf("%d n",(number*i)); i++; } getch(); }
  • 10.
    Infinitive while loopin C  If you pass 1 as a expression in while loop, it will run infinite number of times.
  • 11.
    For Loop  forloop is used to execute a set of statements repeatedly until a particular condition is satisfied. we can say it an open ended loop.  Syntax:
  • 12.
     In forloop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.  The for loop is executed as follows: ◦ It first evaluates the initialization code. ◦ Then it checks the condition expression. ◦ If it is true, it executes the for-loop body. ◦ Then it evaluate the increment/decrement condition and again follows from step 2. ◦ When the condition expression becomes false, it exits the loop.
  • 13.
  • 14.
    Example:  Program toprint first n natural numbers
  • 15.
    Print table forthe given number using for loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); for(i=1;i<=10;i++) { printf("%d n",(number*i)); } getch(); }
  • 16.
    Nested For loop We can also have nested for loops, i.e one for loop inside another for loop.  Syntax:
  • 17.
    Example:  Program toprint half pyramid of numbers
  • 18.
    Infinitive for loopin C  If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.  In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.  If you run this program, you will see above statement infinite times.
  • 19.
    Do While Loop In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that for at least one time ,the body of the loop will be executed, even though the starting condition inside while is initialized to false.  Syntax:
  • 20.
  • 21.
    Example:  Program toprint first ten multiples of 5.
  • 22.
    Program to printtable for the given number using do while loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); do{ printf("%d n",(number*i)); i++; }while(i<=10); getch(); }
  • 23.
    Infinitive do whileloop If you pass 1 as a expression in do while loop, it will run infinite number of times.