1. For Loop
#include<stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i%2==0)
continue;
printf("%d ",i);
}
return 0;
}
Output:
1 3 5 7 9
2.While Loop
#include<stdio.h>
int main()
{
int i;
i=0;
while(i++<10)
{
if(i%2==0)
continue;
printf("%d ",i);
}
return 0;
}
Output:
1 3 5 7 9
3. Do-While Loop
#include<stdio.h>
int main()
{
int i;
i=0;
do
{
if(i%2==0)
continue;
printf("%d ",i);
}while(i++<10);
return 0;
}
Output:
1 3 5 7 9
4. Nested Loop
#include<stdio.h>
int main()
{
int i,j;
for(j=0;j<3;j++) // outer loop
{
for(i=0;i<10;i++) //inner loop
{
if(i%2==0)
continue;
printf("%d ",i);
}
}
return 0;
}
Output:
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
5. Continue the outer loop:
#include<stdio.h>
int main()
{
int i,j;
Outer: for(j=0;j<3;j++)
{
for(i=0;i<10;i++)
{
if(i%2==0)
continue Outer;
printf("%d ",i);
}
}
return 0;
}
Note :
If increment is written at last that line never gets executed
Sample:
#include<stdio.h>
int main()
{
int i;
i=0;
do
{
if(i%2==0)
continue;
printf("%d ",i);
i++;
}while(i<10);
return 0;
}
It will never terminate the loop as increment will never happen.
#include<stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i%2==0)
continue;
printf("%d ",i);
}
return 0;
}
Output:
1 3 5 7 9
2.While Loop
#include<stdio.h>
int main()
{
int i;
i=0;
while(i++<10)
{
if(i%2==0)
continue;
printf("%d ",i);
}
return 0;
}
Output:
1 3 5 7 9
3. Do-While Loop
#include<stdio.h>
int main()
{
int i;
i=0;
do
{
if(i%2==0)
continue;
printf("%d ",i);
}while(i++<10);
return 0;
}
Output:
1 3 5 7 9
4. Nested Loop
#include<stdio.h>
int main()
{
int i,j;
for(j=0;j<3;j++) // outer loop
{
for(i=0;i<10;i++) //inner loop
{
if(i%2==0)
continue;
printf("%d ",i);
}
}
return 0;
}
Output:
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
5. Continue the outer loop:
#include<stdio.h>
int main()
{
int i,j;
Outer: for(j=0;j<3;j++)
{
for(i=0;i<10;i++)
{
if(i%2==0)
continue Outer;
printf("%d ",i);
}
}
return 0;
}
Note :
If increment is written at last that line never gets executed
Sample:
#include<stdio.h>
int main()
{
int i;
i=0;
do
{
if(i%2==0)
continue;
printf("%d ",i);
i++;
}while(i<10);
return 0;
}
It will never terminate the loop as increment will never happen.
No comments:
Post a Comment