C Language | Loops | Start your journey with C | Part 4
WHAT IS LOOP :
Loops are used to execute a piece of code several time according to the condition give to the loop. It means it execute same code multiple times so it saves code and also helps to traverse the elements of array.
There are three types of loops in C language. They are :
- FOR loop.
- WHILE loop.
- DO WHILE loop.
1. FOR loop :
Syntax :
for(initialization;condition;increment/decrement)
{ .....
......
......
...... // CODES
.....
.....
.....
}
Here is an example how FOR loop works :
#include<stdio.h>
void main()
{
for(int i=1;i<=10;i++)
{
printf("\n %d",i);
}
}
Output :
2. WHILE loop :
Syntax :
while(condition)
{ ......
......
...... //code
......
.....
}
Here is an example how WHILE loop works :
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("\n %d",i);
i++;
}
}
Output :
OUTPUT is same as like FOR loop.
3. DO WHILE loop :
Syntax :
do { ......
......
...... //code
......
.....
} while(condition);
Here is an example how DO WHILE loop works :
#include<stdio.h>
void main()
{
int i=1;
do {
printf("\n %d",i);
i++;
} while(i<=10);
}
Output :
OUTPUT is same as like FOR loop.
Loop Control Statements:
These control statements change the execution of the loop from its normal execution. The loop control structures are –
1. break statement – It is used to end the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
2. continue statement – It skip some statements according to the given condition.
3. goto statement – It transfer control to the labeled statement.
---------------------------------------------------------------------------------------------------------------
ADS BY AMAZON.IN
awesome carry it on......
ReplyDelete