When something like this is done in C:
is phase[i] a shorter way of saying: phase[i] != 0Code:int i; const static char phase[] = "urrrddd"; for (i = 0; phase[i]; i++) { /* some code */ }
or something else? Thanks
This is a discussion on Simple Explanation Needed within the C Programming forums, part of the General Programming Boards category; When something like this is done in C: Code: int i; const static char phase[] = "urrrddd"; for (i = ...
When something like this is done in C:
is phase[i] a shorter way of saying: phase[i] != 0Code:int i; const static char phase[] = "urrrddd"; for (i = 0; phase[i]; i++) { /* some code */ }
or something else? Thanks
Pretty much.
I used to be an adventurer like you... then I took an arrow to the knee.
When you don't specifiy a condition it evaluates to true if it is different than zero so phase[i] and phase[i]!=0 are the same thing when already compiled.
And that loop will run while the string isn't terminated.
Boolean expressions in C are numerical expressions as well, and they are resolved in the following way: Every number and expression that results to a non-zero value is true, while every expression that results to zero is false.
Perfect, thank you for all the explanations.
One more simple question that is similar.
For the code above, is there a long way of coding that last line...maybe with an if statement? I'm trying to get a better idea of how '&&' and '<' are being used to give the value of multi_tile. Thanks for the helpCode:int multi_tile, norec, opt_DM; /*some code*/ multi_tile = norec && opt_DM < 32;
Last edited by slowcoder; 07-10-2007 at 07:10 AM.
Last edited by Govalant; 07-10-2007 at 10:21 AM. Reason: clarification
So just to verify that I interpreted that properly, this would be the equivalant?
Thank you Govalant!Code:int multi_tile, norec, opt_DM; /*some code*/ if ((norec != 0) && (opt_DM < 32)) multi_tile = 1; else multi_tile = 0;
Exactly =)