Reputation: 21
I have this expression which I know is an if statement...but what does it translate to? flag = flag?0:1; Is it : if (flag==flag) flag=0 else flag =1 if this is the case then flag will become 1 once when timer reaches 12500 but it will never become 0 again. right?
int flag = 0;
while(1)
{
if (timer == 12500)
{
flag = flag?0:1;
timer=0;
}
if(flag == 1)
p4_0=0; //turn on LED0
else
p4_0=1; //turn off LED0
timer++;
}
Thanks!
Upvotes: 0
Views: 1035
Reputation: 195
(flag = flag?0:1) means that if(flag) then flag=0 otherwise flag=1.To be more precise,if the value of flag is 0 then flag will become 1 otherwise for all non-zero value flag will become 0. Its use in your code is that as soon as the timer reaches 12500 the flag will become 1 and the LED will be turned on and it will remain ON henceforth.
Upvotes: 1
Reputation: 74
flag = flag?0:1; Means if flag is 0 then it is zero, if flag >0, then flag becomes 1.
Upvotes: 0
Reputation: 14565
that is a ternary expression
var = var ? expression1 : expression2
means if var is truthy, assign expression1 to var, else assign expression2 to var. basically this.
if (var)
var = expression1
else
var = expression2
so in the code you have above, when the timer == 12500, the flag will be reset and turn off LED0.
Upvotes: 1