Author Topic: post increment in for loop in C  (Read 1025 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 154
  • Country: gl
post increment in for loop in C
« 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?

 

Offline oPossum

  • Super Contributor
  • ***
  • Posts: 1472
  • Country: us
  • Very dangerous - may attack at any time
Re: post increment in for loop in C
« Reply #1 on: December 29, 2019, 06:18:39 am »
Code: [Select]
int i = 1;
while(i <= 2) {
  int j = 1;
  while(j <= 2) {
    j++;
  }
  i++;
}
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 154
  • Country: gl
Re: post increment in for loop in C
« Reply #2 on: December 29, 2019, 07:04:31 am »
when j will be incremented ? is it before i or after i?
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: ro
  • .
Re: post increment in for loop in C
« Reply #3 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
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22436
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: post increment in for loop in C
« Reply #4 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
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: Brumby

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 154
  • Country: gl
Re: post increment in for loop in C
« Reply #5 on: December 29, 2019, 07:28:09 am »
Thnaks :)
 

Online forrestc

  • Supporter
  • ****
  • Posts: 720
  • Country: us
Re: post increment in for loop in C
« Reply #6 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. 
« Last Edit: December 29, 2019, 07:49:38 am by forrestc »
 

Online forrestc

  • Supporter
  • ****
  • Posts: 720
  • Country: us
Re: post increment in for loop in C
« Reply #7 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.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22436
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: post increment in for loop in C
« Reply #8 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.
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline magic

  • Super Contributor
  • ***
  • Posts: 7454
  • Country: pl
Re: post increment in for loop in C
« Reply #9 on: December 29, 2019, 09:01:38 am »
single step it under debugger :P
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf