7.Loop Statement in C Programming l C Tutorials for beginers#7
7.Loop Statement in C Programming l C Tutorials for beginers#7
Loop in c
ans: Loop can execute a statement multiple times.
Types of Loop
1-for loop
2-while loop
3.Do-while loop
For Loop in c
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax :
for(init ; condition ; increament);
{
statament;
}
Example:
A program to print no from 1 to 10
#include<stdio.h>
void main() ;
int i;
for(i=1 ;i<11; i++)
{
printf("%d",i);
}
}
output:
12345678910
While Loop in c
A while loop executes the statement till the condition is true.
Syntax :
while(condition);
{
statement;
}
Example:
A program to print no from 1 to 10
#include<stdio.h>
void main() {
int i;
while( i<11 )
{
printf("%d",i);
i++;
}
}
output:
12345678910
Do While Loop in c
ans: If we want to execute a part of program several time then we use do while loop ,in do while loop statement or code executed at least one time
Syntax :
do
{
statement;
}while(condition);
Example:
A program to print no from 1 to 10
#include<stdio.h>
void main() {
int i;
do
{
printf("%d",i);
i++;
}
while(i<10);
}
output:
12345678910
Break statement in c
ans:Break statement is used to break the loop.
Example:
A program on break statement
#include<stdio.h>
void main() {
int i;
for(i=0;i<0;i++)
{
if(i==5)
{
break;
}
printf("no is %d",i);
}
}
output:
1234
Continue statement in c
ans:Continue statement is used to continue the statement.
Example:
A program Continue Statement
#include<stdio.h>
void main() {
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
continue;
}
printf("no is %d",i);
}
}
output:
123456789
No comments