I just started learning C, and I have some problems with the ? :
operator.
How can I change
x = c ? a : b;
into an if else statement?
if() { x=a; } else { x=b;}
Is it correct like this? I don't know what the condition should be.
I just started learning C, and I have some problems with the ? :
operator.
How can I change
x = c ? a : b;
into an if else statement?
if() { x=a; } else { x=b;}
Is it correct like this? I don't know what the condition should be.
A turnary statement is in the form:
result = (booleanValue ? valueA : valueB);
This converts to:
if (booleanValue) {
result = valueA;
} else {
result = valueB;
}
In your case "booleanValue" is "c", "valueA" is "a", "valueB" is "b". Hope that helps!
Here is the equivalent if
statement:
if (c)
{
x = a;
}
else
{
x = b;
}
The conditional operator ?:
is described in chapter 2.11 of your C book, "The C Programming Language", 2nd edition, by Kernighan and Ritchie.