post increment in for loop in C - Page 1
Forum section: electronics / beginners

author: khatus
author_rank: Regular Contributor
timestamp: December 29, 2019, 05:55:40 am
content: 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 is1221What will be the equivalent pseudo-code for each or loops??which one is(i.e i or j) incremented first?

author: oPossum
author_rank: Super Contributor
timestamp: December 29, 2019, 06:18:39 am
content: Code:int i = 1;while(i <= 2) {int j = 1;while(j <= 2) {j++;}i++;}

author: khatus
author_rank: Regular Contributor
timestamp: December 29, 2019, 07:04:31 am
content: when j will be incremented ? is it before i or after i?

author: mariush
author_rank: Super Contributor
timestamp: December 29, 2019, 07:22:32 am
content: i is initialized with 1j is initialized with 1check if i == jif 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

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 29, 2019, 07:26:48 am
content: i cannot possibly increment before j because i is out of scope, the j loop completes first.The states are:i = 1, j = 1i = 1, j = 2i = 2, j = 1i = 2, j = 2This has nothing to do with the type of increment used. The return value of the increment statement is not read.Tim

author: khatus
author_rank: Regular Contributor
timestamp: December 29, 2019, 07:28:09 am
content: Thnaks

author: forrestc
author_rank: Unknown
timestamp: December 29, 2019, 07:34:35 am
content: Quote from: khatus on December 29, 2019, 07:04:31 amWalk through oPossum's code step by step. Technically, i will be incremented after the loop containing j exits.But even though i is incremented after j, one should note that in reality what happens is that i counts from 1 to 2, and for each of the values of i, j counts from 1 to 2.So your values inside the loop will be:i=1 j=1i=1 j=2i=2 j=1i=2 j=2when the entire code exits, i will actually be 3 and so will j, since j got incremented to 3 which exited the inner loop, and i incremented to 3 which exited the outer loop.One should note that this portion of your code:Code:if (i==j)continue;printf ("%d %d\n",i,j);is functionally equivalent to:Code:if (i!=j) printf ("%d %d\n",i,j);The second form is easier to understand is generally considered better form. Avoid using break and continue within a for loop since they can lead to unexpected bugs.

author: forrestc
author_rank: Unknown
timestamp: December 29, 2019, 07:49:13 am
content: Just a subtle terminology correction here, although what you meant came across fine:Quote from: T3sl4co1l on December 29, 2019, 07:26:48 amIn the case of the code shown, 'i' is not "out of scope". Out of scope effectively means 'hidden'. Because of the way that 'i' is declared, 'i' is in scope for the entire main() function (but not any other functions). A bug in main() could definitely increment 'i' in the wrong spot and cause a program error. For example, if one did something like:Code:int main(){int i,j;for (i=1;i<=2;i++){for(j=1;j<=2;i++){if (i!=j) printf ("%d %d\n",i,j);}}return(0);}(note the 'i' instead of a 'j' in the increment in the second for loop)You'd find that the code would enter an infinite loop since you are (assumedly) accidentally incrementing 'i' within the 'j' loop instead of 'j'. 'j' never gets incremented so the loop never exits. If 'i' was out of scope, the compiler would spit an error about not knowing about 'i'. But since 'i' is actually within scope, the program can definitely increment i if you tell it to do so.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 29, 2019, 07:54:44 am
content: Scope isn't the right word for it, but the inner loop isn't touching the outer loop is the point.

author: magic
author_rank: Super Contributor
timestamp: December 29, 2019, 09:01:38 am
content: single step it under debugger