int a = 14;int b = 23; if (a&& b) cout << a; else cout << b; can some body tell my what will be the output and explain pl | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

int a = 14;int b = 23; if (a&& b) cout << a; else cout << b; can some body tell my what will be the output and explain pl

27th Jan 2017, 2:49 PM
Sultan
Sultan - avatar
15 Answers
+ 4
ans=14 bcoz && operator says if both sides are true the value is true here in this example 14&&23 means true&&true bcoz compiler understand all digits true without 0(false) if u give value of variable a is 0 then the condition is false and else part is executed
27th Jan 2017, 4:25 PM
Avnish Tomar (CCS University)
Avnish Tomar (CCS University) - avatar
+ 3
If it's JavaScript, the output will be a because every number other than zero is true.
27th Jan 2017, 4:00 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 3
Maybe I'm wrong, but I think 1 is true and 0 is false. So if a and b both are one, then it will be true, and the if statement will execute. If not, the if statement will not execute.
27th Jan 2017, 4:06 PM
J.G.
J.G. - avatar
+ 3
@J.G. Any number other than zero is true. It's not just 1.
27th Jan 2017, 4:08 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 3
@Cheeze But before, you said in JavaScript. Is it just in js or in all languages?
27th Jan 2017, 4:10 PM
J.G.
J.G. - avatar
+ 3
@J.G. Since I only know JS, I never try it in any other languages. I believe it works the same as other languages but who knows I might be wrong.
27th Jan 2017, 4:11 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 3
@Cheeze I've never tried it either. I only know java and c++ Going to try it in those languages now...
27th Jan 2017, 4:12 PM
J.G.
J.G. - avatar
+ 3
Output is 14. The condition is true. "negative or positive. Anything that's not a 0 is a true value in if" source: http://stackoverflow.com/questions/14646718/if-statement-integer
27th Jan 2017, 4:18 PM
Jafca
Jafca - avatar
+ 3
@Jafca That makes sense, but I tried it, and if something other than 1 is put in, it doesn't work.
27th Jan 2017, 4:21 PM
J.G.
J.G. - avatar
+ 3
@J.G. You can enter two numbers here and try it https://code.sololearn.com/cERaMkI4gq7n/?ref=app
27th Jan 2017, 4:28 PM
Jafca
Jafca - avatar
+ 3
@Jafca I just realized I had a nested if statement that wasn't supposed to be. My logic in the program then gave me the wrong answer. That makes sense.
27th Jan 2017, 4:30 PM
J.G.
J.G. - avatar
+ 3
! @Sultan That's what we have been figuring out. It will be the same in c++ :)
27th Jan 2017, 4:32 PM
J.G.
J.G. - avatar
0
it is a c++ code guys not JavaScript
27th Jan 2017, 4:31 PM
Sultan
Sultan - avatar
- 1
its a c++ code!
27th Jan 2017, 4:12 PM
Jason Hoffman
Jason Hoffman - avatar
- 2
the outpur of this will be an error as you are asking an if without any parameters, you are just saying ( if a and b ) ?????? you need to supply some parameters for the program to check, etc if a and b larger than three (then) some code ( else ) some code.
27th Jan 2017, 3:57 PM
Jason Hoffman
Jason Hoffman - avatar