0

Output finding

n=5; if(n=0) cout<<"zero"; else cout<<"non zero "; cout<<endl; cout<<n; what should be the output?

3rd Dec 2016, 4:58 PM
Dharm Vashisth
Dharm Vashisth - avatar
18 Respostas
+ 2
Mistake is in your if condition if(n=0) is wrong if(n==0) is right. You have missed single = sign in condition. Based on your condition n=0 is always right because you're just assigning and it always be true. Hope you understand this
3rd Dec 2016, 5:07 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
I don't know what you mean. Here, let me explain it like this... int n = 5; if (n = 0) cout << "is 0\n"; else cout << "not 0\n"; cout << n; /* output is: is 0 0 */ int n = 5; if (n == 0) cout << "is 0\n"; else cout << "not 0\n"; cout << n; /* output is: not 0 5 */
3rd Dec 2016, 5:46 PM
Cohen Creber
Cohen Creber - avatar
+ 2
The way an if statement works, is that it will run if a condition evaluates to true. Similarly, the else statement will run if a statement evaluates to false. If you wasn't already aware, 0 represents FALSE, and any other number reresents TRUE. Therefore... if (1) { } is perfectly valid, and will run 100% of the time, because 1 is a constant which will evaluate to TRUE. When you use... if (n = 0) { } you are setting n to 0, which is FALSE, therefore the else block will run. When you use... if (n == 0) { } you are checking whether or not n is equal to zero. If it is, it's true. If it's not, it's false.
3rd Dec 2016, 6:07 PM
Cohen Creber
Cohen Creber - avatar
+ 2
You are setting n to 6. 6 is true as it is a non-zero value, therefore the if statement runs.
3rd Dec 2016, 6:18 PM
Cohen Creber
Cohen Creber - avatar
+ 1
Yes. I know what the output is, and I know why it is outputting that. What is your question?
3rd Dec 2016, 5:50 PM
Cohen Creber
Cohen Creber - avatar
+ 1
No. Inside the if statement you use the conditional operator (==). If you use the assignment operator (=), you are changing the value of n, which I'm assuming is what you don't want.
3rd Dec 2016, 5:54 PM
Cohen Creber
Cohen Creber - avatar
+ 1
if we use = operator instead of == operator then will we not face any type of error? if no error occured then why do we say that if is a conditional statement?
3rd Dec 2016, 5:56 PM
Dharm Vashisth
Dharm Vashisth - avatar
+ 1
Whether you use the assignment operator or the conditional operator inside the if statement, no compile errors will come up, as they are both valid. The implimentation is what changes. = is assigning the value of n to something else. == is checking the value of n against something else.
3rd Dec 2016, 6:01 PM
Cohen Creber
Cohen Creber - avatar
+ 1
if we can use assignment statement statement in if statement then why do we say if as conditional statement?
3rd Dec 2016, 6:04 PM
Dharm Vashisth
Dharm Vashisth - avatar
+ 1
#include <iostream> using namespace std; int main() { int n=5; if(n=6) cout<<"six "; else cout<<"zero "; cout<<endl<<n; return 0; } run then you will see if n=6 then also same problem exists.why?
3rd Dec 2016, 6:13 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
output should be: non zero 0 but how?
3rd Dec 2016, 5:00 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
You are using the assignment operator (=). Use the conditional operator (==). What you're doing, is assigning n to 0 in the if statement, which is false, therefore the else blocks runs. After that, the value of n is printed, which is 0.
3rd Dec 2016, 5:06 PM
Cohen Creber
Cohen Creber - avatar
0
this (=)sign is in question and there is no error occured also. you can also try this.
3rd Dec 2016, 5:21 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
No. It's not an error, it does work like I explained. It's not working for what you want though. You don't want me be ASSIGNING n to 0 in the if statement, you want to be checking it's CONDITION. Hence the conditional operator (==).
3rd Dec 2016, 5:23 PM
Cohen Creber
Cohen Creber - avatar
0
but in qestion output is based on given conditions not on our condition . you can also run it and can check the output.
3rd Dec 2016, 5:41 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
output of this question result in the below output as you can also check it by running code. Bro output: non zero 0
3rd Dec 2016, 5:49 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
my question is that according to this output. shall we use assigning operator in 'if' statement condition part?
3rd Dec 2016, 5:53 PM
Dharm Vashisth
Dharm Vashisth - avatar
0
oooh thanks for clearing my doubt.☺👌👌💐
3rd Dec 2016, 6:23 PM
Dharm Vashisth
Dharm Vashisth - avatar