If i use hte following codes , will the compiler optimize it like a switch structure , which uses a binary tree to search for values ?
if ( X == "aaaa" || X == "bbbb" || X == "cccc" || X == "dddd" )
{
}
else if ( X == "abcd" || X == "cdef" || X == "qqqq" )
{
}
It’s just an example , there’s no pattern of what’s inside the quote symbol
UPDATE
Ok , X is a string , but i don’t really think it matters here , i just want to know , when everything inside the if was all about single variable , will it be optimized.
The values WILL be compared one after the other as it is a requirement of the
||or, the short-circuit operator. So, here two things will happen:For example:
The output for the first statement will be:
as
a == world()will not be executed, and for the secondHello2 Hello, as the comparisons keep on happening till the first success.In case of the
&&operator, the comparisons keep on happening until the first failure (as that is enough to determine the outcome of the entire statement).