how continue statement works in this program?

Discussion in 'Programmer's Corner' started by engrrehmat, Feb 11, 2013.

  1. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    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();
    }
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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...
     
  3. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    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:

    Code ( (Unknown Language)):
    1.  
    2. int main()
    3. {
    4.     int i, j;
    5.  
    6.     for(i=1;i<=2;i++)
    7.     {
    8.         for(j=1;j<=2;j++)
    9.         {
    10.             if(i==j)
    11.                 continue;
    12.  
    13.             [COLOR="Red"]printf("%d\t%d\n", i, j);[/COLOR]
    14.         }
    15.     }
    16.  
    17.     getch();
    18. }
    19.  
    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: Feb 11, 2013
Loading...