C for Loop
Loops are used in programming to execute a block of code repeatedly until a specified condition is met. In this tutorial, you will learn to create for loop in C programming.
C programming has three types of loops:
for loop
while loop
do...while loop
2. 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.
3. 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.
4. 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.
5. Types of Loops in C
There are 3 types of loops in C
language:
while loop
for loop
do-while loop
6. 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:
9. 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();
}
10. Infinitive while loop in C
If you pass 1 as a expression in while
loop, it will run infinite number of
times.
11. 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:
12. 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.
15. 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();
}
16. Nested For loop
We can also have nested for loops, i.e
one for loop inside another for loop.
Syntax:
18. 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.
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:
22. 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();
}
23. Infinitive do while loop
If you pass 1 as a expression in do
while loop, it will run infinite number of
times.