Home > Software engineering >  What does for(i=0;n;i ) mean? How does the 'condition' part of the FOR loop works here?
What does for(i=0;n;i ) mean? How does the 'condition' part of the FOR loop works here?

Time:12-12

I just stumbled upon a code with a FOR loop as : for(i=0; n; i ) I had read that the structure of FOR loop is for(declaration;condition;post-condition) How does the condition part work here??

CodePudding user response:

n is equivalent to n != 0 for conditions in C/C .

CodePudding user response:

Nicer: for (i=0; n != 0; i )

But I suggest other mode:

i = 0;
while (n != 0) {
    ...
    i  ;
}

CodePudding user response:

A "condition" is any expression that can be explicitly converted to bool.

Assuming n is some type of integer, it will convert to bool, with 0 converting to false and all other values converting to true.

Your loop will continue as long as n is not 0.

  • Related