0
C Conditionals
I am not understanding the concept of Conditionals in C, would you guys please help me?
3 Respostas
+ 1
a>5 && a<10 first a>5 then a<10 evaluated then &&.
In a compound statements consisting of more than 1 operators, then statements evaluated depending on the precedence level.. Highest precedence operators evaluated first, and it preceded to least one.. If any operator have equal precedence then it evaluated according to its Associativity (left to right or right to left)..
In the above your example,, >, < have highest precedence than && so those evaluated first but also <, > equla precedence but Associativity is left to right so a>5 is evaluated first then a>10, and next &&.
Note: if a>5 is false then for && statement, a<10 not evaluated at all.. Because it's results to false any way.. 0&&1=>0, 0&&0=>0.
Edit : see this for more clarity...
https://www.tutorialspoint.com/questions/index.php
+ 2
Mathematical expressions, e.g., (5*x+1), evaluate to some arbitrary number value.
Conditional expressions, e.g., (x>=11), determine whether the condition is true or false and evaluate to only 1 or 0.
Sometimes you will see something like:
if (5*x+1) {
// code...
}
This still works because first the mathematical expression gets evaluated, and then the resulting value gets evaluated implicitly as a conditional. If the mathematical result is 0, then the conditional evaluates to 0 -- false. If the mathematical result is non-zero, then the conditional evaluates to 1 -- true. Explicitly, the statement is equivalent to this:
if (5*x+1 != 0) {
// code...
}
+ 1
Conditionals (like if, else) evaluated on conditions..
A conditions results either true or false..
if( a>b)
printf("a greater than b");
else
printf("b greater than or equal to a");
Here, for ex : a =2, b = 1, then if part get executed
if for ex : a =1, b=2 then else part get executed...
Mention the details about which one is actually not understanding with adding link or "phrase" or code sample, if you about a specific one... Hope this helps..