+ 1
Why does this basic if-else statement refuse to post anything other than one? The integer is a=2.
#include <iostream> using namespace std; int main() { int a=2; if (a=1) { cout<<1<<endl; } else { cout<<2<<endl; } return 0; }
7 Réponses
0
Because assignment inside an if will return true until and unless 0 is not assigned. Try assigning 0 to 'a' inside if and it should execute the else part.
+ 2
You need to use == operator to compare variable values....
You have declared a=2 then in if condition you should specify
if(a==1)
+ 1
Output says: 1
+ 1
You are assigning a=1 inside the if and then printing it. Why do you expect your else to be executed?
0
Shouldn't the else statement be executed if the if statement is false?
0
Thx. This worked. Now one other question. If you do not mind telling me the truth for the 0. Is this considerable to think there must be 0 in the first, or only "if" statement?
0
It will be something like if(0) which returns false and the else part is executed.
Sorry I meant, until and unless 0 is NOT assigned. I have changed it.