EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: khatus on December 29, 2019, 05:55:40 am

Title: post increment in for loop in C
Post by: khatus on December 29, 2019, 05:55:40 am
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?

Title: Re: post increment in for loop in C
Post by: oPossum on December 29, 2019, 06:18:39 am
Code: [Select]
int i = 1;
while(i <= 2) {
  int j = 1;
  while(j <= 2) {
    j++;
  }
  i++;
}
Title: Re: post increment in for loop in C
Post by: khatus on December 29, 2019, 07:04:31 am
when j will be incremented ? is it before i or after i?
Title: Re: post increment in for loop in C
Post by: mariush on December 29, 2019, 07:22:32 am
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
Title: Re: post increment in for loop in C
Post by: T3sl4co1l on December 29, 2019, 07:26:48 am
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
Title: Re: post increment in for loop in C
Post by: khatus on December 29, 2019, 07:28:09 am
Thnaks :)
Title: Re: post increment in for loop in C
Post by: forrestc on December 29, 2019, 07:34:35 am
when j will be incremented ? is it before i or after i?

Walk 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=1
i=1 j=2
i=2 j=1
i=2 j=2

when 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: [Select]
   if (i==j)
        continue;
       
    printf ("%d %d\n",i,j);
is functionally equivalent to:
Code: [Select]
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. 
Title: Re: post increment in for loop in C
Post by: forrestc on December 29, 2019, 07:49:13 am
Just a subtle terminology correction here, although what you meant came across fine:

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

In 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: [Select]
 
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.
Title: Re: post increment in for loop in C
Post by: T3sl4co1l on December 29, 2019, 07:54:44 am
Scope isn't the right word for it, but the inner loop isn't touching the outer loop is the point.
Title: Re: post increment in for loop in C
Post by: magic on December 29, 2019, 09:01:38 am
single step it under debugger :P