- 1

whats happening here?

#include <iostream> using namespace std; int main() { if(!3){ cout<<"tricky\n"; } cout<<"yes"; return 0; }

26th Oct 2017, 4:24 AM
harsh.I.M.
harsh.I.M. - avatar
5 Answers
+ 4
Actually, @Cool Codin already gave you the explanation about what the "if" part did, basically, any statement or code block following an "if" will be executed if the expression passed to "if" (within the parentheses) evaluates to true. In C or C++ language, any number that is nonzero is logically assumed as true, but since you used the not (!) operator the evaluation result is inverted to false, explanation as follows: 3 evaluates to true, because it is nonzero, !3 evaluates to false, inverted from actual result, that's really saying !true (read: not true) which gives false. What's happening in the if statement here is the code prints the sentence "tricky!" to the console. Hope this can help you to understand. Hth, cmiiw
26th Oct 2017, 1:56 PM
Ipang
+ 4
ohh.. I got it. THANKS.
27th Oct 2017, 12:37 PM
harsh.I.M.
harsh.I.M. - avatar
+ 3
Yes, this is because any number other than 0 evaluates to true. In this case, you put the "not" (!) operator in front of 3(true), which evaluates it to false and runs the cout << "yes"; statement.. :)
26th Oct 2017, 4:31 AM
Cool Codin
Cool Codin - avatar
+ 3
If you don't wrap the cout<<"yes"; statement in an else block it will be executed either way, regardless of the evaluation result of the "if(..)" statement. if(!3){ cout << "tricky!"; } else { cout << "yes!!"; } Hope I understand your question correctly.
26th Oct 2017, 8:44 AM
Ipang
0
what's happening in the if statement.
26th Oct 2017, 10:40 AM
harsh.I.M.
harsh.I.M. - avatar