how continue statement works in this program?

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
int main()
{
int i,j;

for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
}
printf("%d\t%d\n");
}




getch();
}
 

tshuck

Joined Oct 18, 2012
3,534
perhaps you should look up a C tutorial so you can learn this stuff without having to ask very basic C questions...

It causes the loop to end its current iteration and goes to the beginning of the loop...

In this case, if i == j, then j++ and evaluate whether j<=2...
 

CVMichael

Joined Aug 3, 2007
419
continue goes to the end of current loop block, and since there is no code after continue, it does not matter if it's there or not...

Also, you are not passing the variables to the printf, and also, I think the printf should be inside the smaller for loop.

So, your code should be:

Rich (BB code):
int main()
{
    int i, j;

    for(i=1;i<=2;i++)
    { 
        for(j=1;j<=2;j++)
        {
            if(i==j)
                continue;

            printf("%d\t%d\n", i, j);
        } 
    }

    getch();
}
This way, the continue would actually make a difference. The printf will not execute when the values "i" and "j" are equal.

Your outpost should be:
1 2
2 1
 
Last edited:
Top