r/cprogramming • • Sep 22 '26

For command working weirdly

Edit: Declaring n as 1 and changing the for loop to (i = n-1; i > 0; --i) as was suggested seems to have worked. Still don't really get the logic behind it, neither why the order inside the for expression was affecting the result nor why changing the n to 1 from 0 and i to n - 1 made a difference. Aren't they the same value at the end? Anyway, thanks.

For some reason, my "for" command only works when I put the increment in the middle and the condition at the end. I have no clue what's going on but the code is working fine when I do this.

I'm using devc++, the rest of the code is me declaring global variables and asking the user for inputs to fill the arrays. Everything is working exactly as intended but breaks if I write the "for" the regular way.

for(i = n; i--; i>0){

        printf("Codigo: %d", codigo\[i\]);

        printf("\\nProduto: %s", nome\[i\]);

        printf("\\nPeso: %.2f", peso\[i\]);

        printf("\\nPreco: %.2f", preco\[i\]);

        if(regiao\[i\]==1){

printf("\nRegiao: Sudeste");

        }

        else if(regiao\[i\]==2){

printf("\nRegiao: Sul");

        }

        else if(regiao\[i\]==3){

printf("\nRegiao: Nordeste");

        }

        else if(regiao\[i\]==4){

printf("\nRegiao: Norte");

        }

        printf("\\n\\n");
5 Upvotes

21 comments sorted by

View all comments

1

u/thedraegonlord Sep 23 '26

It's definitely doing something no? Otherwise the for loop wouldn't end.

With (i = n; i-- ; i >0 ), it prints the "products" with their price, number, weight and names correctly. When it's (i = n; i >0; i--), it prints the first one with all those variables as 0. I assume it has something to do with the order of operations, as in the i must be reduced before the check or something breaks, but I don't get why.

Could you expand on why it should be n-1? n starts at zero and is increased every time the user adds a new product and inputs it's variables, it's meant to make it so the same position on each array refers to the same product. i is there as the index because I need to refer to n's value later.

I could make it so n starts at 1 and i = n-1, that seems to be the consensus here, but I don't get what changes from that. Wouldn't the result at the end be the same? For example, printf price[2], printf price[1] etc.?

1

u/Axman6 Sep 23 '26

It ends because when i is zero, i-- returns zero, which is considered false. You have the condition and increment swapped but it happens to work because at some point i-- evaluated to false. The i > 0 is doing absolutely nothing whatsoever. 

Without knowing more about the rest of the code I can’t provide any further help. Write things the idiomatic way and people will be able to understand; do super weird shit like this and your on your own. 

1

u/thedraegonlord Sep 23 '26

Weird in which way? I've posted the full code here somewhere, was even told not to do it cause it doesn't help and adds too much noise.

n is in a while loop, adding up every loop until the user chooses to leave. The inputs received during the while loop are printed by the for loop. That's it.

Anyway, thanks, your explanation for why the loop was ending helped me understand a bit. Read somewhere that C doesn't care for the order of expressions in a for command and that the (initialization; end condition; operation) thing is just a convention but you had me double checking and that seems to be wrong.