for loop examples in c,loops in c with examples,for loop example in c,for loop in c example,for loop example in c language,loop examples in c,loops examples in c,loops in c language examples,for loop examples in c language,for loop in c with example,loop example in c,for loop in c language with example,examples of for loop in c,c language for loop examples with output,c for loop examples,example for for loop in c,for loop in c examples,c loop examples,
Explain Various Loop Control structures in c,
Explain Do while, for, while
Loop present in c ,break,continue example with source code
Explain Various Loop Control structures in c
Or
Explain Do while, for, while
Loop present in c
Different
loops
1.while loop
2. do while loop
3. for loop
a) all loops use same logical(true /false)b) stop
1.While loop: while(something is true)
{
(body of program)
}
If
the loop condition is not true then at the beginning loop never executed
2.do while loop: do while loop is always executed once.do while loop statement allows you to execute code block in loop body at least one.
2.do while loop: do while loop is always executed once.do while loop statement allows you to execute code block in loop body at least one.
syntax:
do { // statements } while (expression);
do { // statements } while (expression);
do
{loop body}
While loop(loop condition);
While loop(loop condition);
Void main {
do{
printf(“hello world”);
}
while (4<1 data-blogger-escaped-br=""> for example using do while loop statement
#include
void main()
{ int x = 5;
int i = 0;
// using do while
loop statement
do{ i++;
printf("%d\n",i);
}while(i < x);
}
The program output
1
2
3
4
3.
For loop:Thefor loop is used to implement any
type of loopcondition. It is based on numeric
variables.There are three parts which is separated by semi-colons in control
block of the forloop.initialization_expression is executed before execution of
the loop starts.
The execution of the loop continues until the loop_condition is false
Theincrement_expression, is usually used to increment the loop counter. This is executed at the end of each loop iteration.
for example of using for loop statement to print an integer five times
The execution of the loop continues until the loop_condition is false
Theincrement_expression, is usually used to increment the loop counter. This is executed at the end of each loop iteration.
for example of using for loop statement to print an integer five times
#include
void main()
{
// using for loop statement
int max = 5;
int i = 0;
for(i = 0; i
printf("%d\n",i);
}
}
And the output is
1
2
3
4
5
void main()
{
// using for loop statement
int max = 5;
int i = 0;
for(i = 0; i
printf("%d\n",i);
}
}
And the output is
1
2
3
4
5
For
Loop
Example Using C
For example:
Void main()
{
intp,n,count;
float r, si;
for (count =3;count <=3;count=count ++ )
{
printf (“enter the value of p,n,r”);
scanf(“%d%d%f”,&p,&n,&r);
si =(p*n*r)/100;
printf(“simple interest =%f”,si);
}
getch();
}
nesting for loop: void main()
{
int r ,c,sum;
for(r =1;r<=3;r++)
{
for (c=1;c=2;c++)
{
sum =r+c;
printf(“r=%d,c=%d,sum=%d\n”,r,c,sum);
}
}
}
Void main()
{
intp,n,count;
float r, si;
for (count =3;count <=3;count=count ++ )
{
printf (“enter the value of p,n,r”);
scanf(“%d%d%f”,&p,&n,&r);
si =(p*n*r)/100;
printf(“simple interest =%f”,si);
}
getch();
}
nesting for loop: void main()
{
int r ,c,sum;
for(r =1;r<=3;r++)
{
for (c=1;c=2;c++)
{
sum =r+c;
printf(“r=%d,c=%d,sum=%d\n”,r,c,sum);
}
}
}
7. Explain Continue and Break Statement in c
Continue Statement
continue statement is used to
break current iteration. After continue statement the control returns to
the top of the loop test conditions.
Void main ()
{
int r ,c;
for(r=1;r<=10;r++)
{
if (r==5)
Continue;
else
printf(“r=%d,\n”,r);
}
getch();
}
Void main()
{
int a;
for (a=1;a<=10;a++)
{
if (j==5)
Break;
else
printf(“%d\n”,a);
}
Printf(“hello”) // control pas here
getch();
}
Void main ()
{
int r ,c;
for(r=1;r<=10;r++)
{
if (r==5)
Continue;
else
printf(“r=%d,\n”,r);
}
getch();
}
Break statement:
break statement is used to break any type of loop such as while, do while an for loop. break statement terminates the loop body immediately.Void main()
{
int a;
for (a=1;a<=10;a++)
{
if (j==5)
Break;
else
printf(“%d\n”,a);
}
Printf(“hello”) // control pas here
getch();
}