- 1
whats happening here?
#include <iostream> using namespace std; int main() { if(!3){ cout<<"tricky\n"; } cout<<"yes"; return 0; }
5 Respostas
+ 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
+ 4
ohh.. I got it.
THANKS.
+ 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..
:)
+ 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.
0
what's happening in the if statement.