void get_out_last_10(thisWay_t *thisWay) {
int i;
for (i = (thisWay->gens - 9 < 1 ? 1 : thisWay->gens - 9); i <= thisWay->gens; i++) {
printOut(&thisWay->states[i], thisWay->height, thisWay->width);
}}
I have this function, I don't understand the sign '?' .
How to change those statements more 'Normally'.. other word make it logic same?
The operator ?: is used as follows:
condition ? value_if_true : value_if_false
The condition is evaluated true or false as a Boolean expression. On the basis of the evaluation of the Boolean condition, the entire expression returns value_if_true if condition is true, but value_if_false otherwise. Usually the two sub-expressions value_if_true and value_if_false must have the same type, which determines the type of the whole expression. The importance of this type-checking lies in the operator's most common use—in conditional assignment statements. In this usage it appears as an expression on the right side of an assignment statement, as follows:
variable = condition ? value_if_true : value_if_false .
It means if the condition before the ? is true the operation before the : colon is carried out else the one after is carried out and then the obtained value is assigned to i.So if you were to write it in an if else statement it would be :
if(thisWay->gens-9 < 1)
{
i=1;
}
else
{
i=thisWay->gens - 9;
}
i = (thisWay->gens - 9 < 1 ? 1 : thisWay->gens - 9);
If you want to convert it to if else it will be something like this
if(thisWay->gens - 9 < 1){
i = 1;
}
else{
i = thisWay->gens - 9;
}
In this statement
i = (thisWay->gens - 9 < 1 ? 1 : thisWay->gens - 9);
if the condition thisWay->gens - 9 < 1 is true it will return 1
else it will return thisWay->gens - 9
And that return value will be assigned to variable i.
Question mark (?) along with colon (:) is used to conditional operator. It works as follows for your case.
if(thisWay->gens - 9 < 1)
{
i=1;
}
else
{
i=thisWay->gens - 9;
}
Related
I currently have a problem to read this code.
Does anyone know what it does ?
if (c == a)
return q[a] ? 1 : 0;
The code inside if gets evaluated in order matching operator precedence.
So first a + 1 is evaluated and then b == (a + 1).
If b is equal to (a + 1) then if q[a] is true 1, is returned, otherwise 0.
See C# operators and expressions or Precedence and order of evaluation
(depending on the language, you want to google "language name" + operator precedence).
The statement
return q[a] ? 1 : 0;
is equivalent to
// Exact type of "zero" depends on the type of q[a]
if (q[a] != 0)
return 1;
else
return 0;
Or
return !!q[a];
If q[a] is non-zero it returns 1 otherwise 0.
If c==a and q[a] = true return 1 otherwise return 0.
Conditional operator in C is used like this:
condition ? value_if_true : value_if_false
What does 0 mean when it's used in the value_if_false?
I've seen some people using it like this, for example.
a == b ? i++ : 0
It seems like it does nothing. Does this work like return 0 in other functions?
In C language, ternary is shorter version of if statement and it requires both statements, if_true and if_false. It would be like this (in fact it can have multiple statements for one case, separated with comma):
Short:
condition ? if_true : if_false;
Long:
if (condition) {
if_true;
} else {
if_false;
}
You can also assign the value if you put something infront of condition.
Short:
result = condition ? if_true : if_false;
Long:
if (condition) {
result = if_true;
} else {
result = if_false;
}
Now here is the trick. In C language, writing 0; is a valid statement, so your ternary becomes in longer version same as code below:
if (a == b) {
i++;
} else {
0; /* This is valid C statement */
}
Or if you have assignment too, it would be:
if (a == b) {
result = i++;
} else {
result = 0;
}
You can also do this:
int a;
/* Code here ... */
condition ? a = 5: 0;
That is effectively the same as:
if (condition) {
a = 5;
} else {
/* DO not touch a */
}
The ?: operator is a ternary operator, but it is not called "ternary" as some answers and/or comments here suggest. It just is the arity of the operator, just as + is a binary operator or as & is unary. If it has a name at all, it is called "Conditional Expression"-operator
It is not quite equivalent to if/else, because it is a conditional value (with the consequence, that both expressions must have the same type) in the first place, not a conditional execution. Of course, both types can be cast to make them equal.
In the case of what the OP does, a better option (if if shall not be used) is in my opinion:
a == b && i++;
which resembles a bit more logical what happens. But of course it is a matter of style.
The reason why someone might want to write a == b ? i++ : 0; is that s/he probably wants to have an (Caution! You are now entering an opinion-based area) easier and faster alternative to if (a == b) i++; - although this is of course opinion-based and I personally not share the same opinion.
One thing I can think of as a "blocker" at the if statement is the requirement to write the parentheses () which can be omitted by using the conditional operator instead.
"But why the 0?"
The C syntax requires a third operand for the conditional operator. Else if you would want to compile for example:
a == b ? i++;
you will get an error from the compiler:
"error: expected ':' before ';' token"
Or respectively, doing so:
a == b ? i++ : ;
would raise:
"error: expected expression before ';' token"
So they use 0 as kind of "syntax satisfier" to be able to use the conditional operator as replacement for the if statement. You could use any other numeral value here as well, but 0 is the most readable value, which signifies that it has no use otherwise.
To showcase the use at an example:
#include <stdio.h>
int main (void)
{
int a, b, c = 4;
a = 2;
b = 2;
a == b ? c++ : 0;
printf("%d",c);
return 0;
}
The output for c will be 5, because a == b.
Note that a == b ? i++ : 0 is different when used f.e. inside of an assignment like f.e.:
int c = a == b ? i++ : 0;
Here c is either getting assigned by i or 0, dependent upon a == b is true or not. If a == b is true, c is assigned by i. If a == b is wrong, c is assigned by 0.
Side Notes:
To view it from a technical point, ?= is called the "conditional operator". The conditional operator is one of the group of ternary operators.
If you want to learn more about the conditional operator ?=, look at ISO:IEC 9899:2018 (C18), §6.5.15 - "Conditional operator" for more information.
There's nothing special about 0 one could write
a == b ? i++ : 1
And it would behave the same way.
Only difference is when you assign the expression to say another variable:
int c = a == b ? i++ : 1;
// a == b -> c will be i++
// a != b -> c will be 1
However it's much cleaner to write
if (a == b) i++;
It helps to think of the ternary operator as a shorthand way or writing an if-else statement.
If(a == b){
i++;
}else{
//if assigned, return 0. Else do nothing.
}
i have conditional operator's statement and i have no idea how its works.
there are two questions:
Question 1 : what will the following statement do :
quotient=(b==0)?0:(a/b) \\ here a,b,quotient is integer
Question 2 : Can preceding statement be written as follow ?
quotient=(b)?(a/b):0;
NOW MY QUESTION IS :
Question:1 :: we do not know b's value then how can we check this condition(b==0)
Question 2:: what (b) indicate ?
The conditional check in the C ternary conditional operator is an implicit comparison to not-zero.
In other words
quotient = b ? a / b: 0;
is the same as
quotient = b != 0 ? a / b : 0;
or the absurd
quotient = (b != 0) != 0 ? a / b : 0;
This is consistent throughout C, e.g. in an if, a for stopping condition, a while, &&, ||, &c.
If you try
int b = 0;
if (b) {
printf("Hello World");
}
Does not print anything while :
int b = 1;
if (b) {
printf("Hello World");
}
Prints Hello World. Why ? Because 0 is false and 1 is true.
If you do quotient=(b)?(a/b):0; it is interpreted to is b true ? or in other words is b evaluated to 1 (while, again, 1 is true and 0 is false)
C did not originally have a Boolean type. Conditionals are simply int values in C. 0 is false, and any other value is truthy. If the type of b is int, or it can implicitly convert to int, then (b) ? foo : bar does the same thing as (b == 0) ? bar : foo. (However, b==0 will evaluate to 1 or 0, whereas b by itself might have other nonzero values that if or ? consider truthy.)
So,I understand everything in code except this if statement - if(i-2) :
for (i = -1, j = 0; i < 4; i++, j++)
if (i - 2)
printf("%d\t", j * (i + 2));
Can anyone explain meaning of this if statement,does it have any influence on printf ? Thanks.
The statement under an if is executed if the controlling expression is true. In C, an expression that evaluates to 0 is considered false, while any other value is considered true.
So if (i-2) means the printf will get executed if i-2 is non-zero, i.e. when i is not equal to 2.
In C, an integer is treated like a boolean in an if statement:
0 is false
not-zero is true
So if (i-2) is equivalent to if (i != 2).
In c any value can be used in an if statement not just boolean expressions. This devolves into expression != 0. Often this is against style guides as it can be confusing and error prone.
The two following are equivalent
if (val) ...
if (val != 0) ...
So in you're case
if (i - 2)
will be true for every value of i except 2 when i-2 is 0
I was given this example in class and I am unsure what it does. I understand the colon adds a bit-field, but I am still uncertain about this question:
a = b > 0 ? 3:1;
?: operator is called the conditional operator.
If b value is > 0, the value 3 is assigned to a else the value 1 is assigned to a.
Take your Kernighan & Ritchie book 2nd edition, chapter 2.11 Conditional expressions, the behavior of the operator is explained.
? is a conditional operator:
a = b > 0 ? 3:1;
is equivalent to:
if(b > 0)
a = 3;
else
a = 1;
This is the conditional operator. It's equivalent to:
if (b > 0)
a = 3;
else
a = 1;
Read it as "a = if b > 0 then 3 else 1".
It's the conditional operator (generally called the ternary operator), which is used as a short way of writing if statements.
In general, it can be read:
condition ? value_if_true : value_if_false
So, in your case:
a = b > 0 ? 3:1;
Can be rewritten as:
if(b > 0) a = 3;
else a = 1;
The colon in this example doesn't mean anything related to bit fields - it's just the second part of the conditional.