Electronics > Beginners

post increment in for loop in C

(1/2) > >>

khatus:
Here is a very simple C program:
#include <stdio.h>

    int main()
    {
    int i,j;
    for (i=1;i<=2;i++)
    {
     for(j=1;j<=2;j++)
     {
        if (i==j)
        continue;
        
    printf ("%d %d\n",i,j);
        
        }   
    }   
    return(0);   
    }

The result is

    12
    21

What will be the equivalent pseudo-code for each or loops??
which one is(i.e i or j) incremented first?

oPossum:

--- Code: ---int i = 1;
while(i <= 2) {
  int j = 1;
  while(j <= 2) {
    j++;
  }
  i++;
}

--- End code ---

khatus:
when j will be incremented ? is it before i or after i?

mariush:
i is initialized with 1
j is initialized with 1
   check if i == j
   if yes, continue means get out of for, so it's like jumping over all instructions between { }  and going back to the for (j ) part and the expressions are re-evaluated and j is incremented (the j++ at the end of for )
when done with j =1, j=2 ,   i is incremented and j is initialized again with 1 value

T3sl4co1l:
i cannot possibly increment before j because i is out of scope, the j loop completes first.

The states are:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2

This has nothing to do with the type of increment used.  The return value of the increment statement is not read.

Tim

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod