if - else | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

if - else

x=3; y=8; if(!(x>2) || (y<0)) - this should be readed as IF x NOT greater than 2 is TRUE. Since the x=3 the condition returns FALSE.

24th Dec 2018, 1:45 PM
Predrag
Predrag - avatar
20 Réponses
+ 17
1) x>2 is true, so applying ! on it, will make it false 2) since, 1st condition is false, so there will be need to check 2nd condition to determine final result [bcz of || operator] Note : if 1st condition will be true, then 2nd condition will not be evaulated as result will be true only(independent of 2nd condition) [this is also known as short-circuiting (for || operator)]
24th Dec 2018, 3:19 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
No brother. It will check whole condition and if one of these both will be true then it will return true. You better check it on code playground by making a code.
24th Dec 2018, 1:57 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
This whole condition will return true. The first condition is !(x > 2) Will be false, because x is greater then 2 but the ! will make the answer reverse so it is false. And second condition is y less than 100 which is true. "||" This sign is or sign which means if one of both condition is true, it will return true.
24th Dec 2018, 1:52 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
No, that is not the explanation. First condition you have 3 > 2 evaluated to Boolean "true", but before that condition is logical"not" => first condition becomes "false". The second condition is true 8<100. Then logical "OR" first condition false , second true, so the condition is true. Hope it helps you.
24th Dec 2018, 1:58 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Then it's false 😐
24th Dec 2018, 2:16 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
No brother. You are wrong in that case. I have learnt c, c++ and Java. It will always check all conditions and then it will return true or false.
24th Dec 2018, 2:22 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
Run this code. Here first condition is false and second it true. #include <stdio.h> int main() { int a = 10, b = 20; if(a > 10 || b==20) { printf("Test"); } return 0; }
24th Dec 2018, 2:24 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
I think you write something completely different. It's the last question in "logical operators", but logical "NOT" is applied to the result of the conditions in the brackets. So the result is false.
24th Dec 2018, 2:15 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
No, logical "NOT" is applied after evaluation of both of the conditions in the brackets.
24th Dec 2018, 2:23 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Diego Acero You mean he is wrong in putting parentheses?
24th Dec 2018, 2:31 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
As TheWhiteCat💡 mentioned, the logical “NOT” is applied after evaluating “(x > 2 || y < 0)”. (x > 2 || y < 0) evaluates to true because the first condition is true. Then, the logical “NOT” makes the whole expression false. Thus, we have: if False: printf("true"); else: printf("false"); Which clearly prints “false”.
24th Dec 2018, 2:44 PM
Diego
Diego - avatar
0
it will return FALSE because || operator chek the first TRUE statment and that is x is > than 2. It wont check second condition.
24th Dec 2018, 1:55 PM
Predrag
Predrag - avatar
0
It's in the C tutorial (conditions) , this task, and the correct answer is False.
24th Dec 2018, 1:59 PM
Predrag
Predrag - avatar
0
A little correction. I was wrongly typed "100" instead of "0". Sry
24th Dec 2018, 2:05 PM
Predrag
Predrag - avatar
0
But the truth is that in C, || operator checks the first true condition if he founds the true in first condition it wont check second one. In this case the truth is x > 2 and condition looks for !x>2.
24th Dec 2018, 2:20 PM
Predrag
Predrag - avatar
0
Predrag The question you’re talking about is the following (which is different from the one you posted). x = 3; y = 8; if(!(x > 2 || y < 0)) printf("true"); else printf("false");
24th Dec 2018, 2:28 PM
Diego
Diego - avatar
0
This is the complete code from C tutorial. x = 3; y = 8; int main(){ if(!(x>2) || (y<0)) printf("True"); else printf("False"); }
24th Dec 2018, 2:29 PM
Predrag
Predrag - avatar
0
It will evaluate the same way
24th Dec 2018, 2:35 PM
Predrag
Predrag - avatar
0
Diego Acero i think that applays to my post that condition should be "read" as " if x NOT > than 2 - than is TRUE".
24th Dec 2018, 2:44 PM
Predrag
Predrag - avatar
0
Thats correct.
24th Dec 2018, 2:45 PM
Predrag
Predrag - avatar