+ 1

Can I Use This Code For If Else Statements Alternatively ?

#include <iostream> using namespace std; int main() { int x,a=5,b=4; x = 5>4?x=a:(x=b); /* can i replace "if else" code with this code ?? working is same so.. */ cout<< x; return 0; } Basically I'm new in programming i started with c++ n i think this question can clear my lots of doubts so help will be appreciated.. nd my english is not very good if i do any mistake in typing please clear it also..

8th May 2018, 9:33 AM
Happy
Happy - avatar
15 Respuestas
+ 1
(condition) ? (if_true) : (if_false) This is how it should be, so I would remove that x = at the beggining of your line, and the rest can be left as is. a>b?(x=a):(x=b);
8th May 2018, 9:54 AM
Paul
+ 1
also for more information search for Ternary operator
8th May 2018, 10:11 AM
Paul
+ 1
yes, it is exacly liek if else statement, just shortened. You can even create nested ternaries for multiple If else statements! And that is cool! but looks like mess sometimes
8th May 2018, 11:30 AM
Paul
+ 1
oh, you mention someone by putting @ and then writing his name, like that: Happy Also, please do not call me sir, I guess I may be same age as You are.When taking input from someone by using CIN, take one input line by line or by separating them: cin>>a; cin>>b; or, if I remember correctly (I never do it that way tho): cin>>a>>b; You should be cool by doing thinga this way
8th May 2018, 12:03 PM
Paul
+ 1
int main() { int x =0, a=0, b=0; cin>>a; cin>>b; a>b?(x=a):(x=b); cout<<x; return 0; }
8th May 2018, 12:14 PM
Paul
+ 1
this one works great for me rho. when using code playground here and when you want to put two input values you have to send then in that first line, each one separeted by a new line
8th May 2018, 12:15 PM
Paul
+ 1
yeah, just mention me and i will get a notification, will try to answer asap
8th May 2018, 12:26 PM
Paul
0
Ty Paul Sir. can i use it instead of if else statements ?
8th May 2018, 11:13 AM
Happy
Happy - avatar
0
okey sir can i mention you when i need some help ? if yes then also tell me how to mention bcz i don't know how to mention any person on any post ty
8th May 2018, 11:35 AM
Happy
Happy - avatar
0
#include <iostream> using namespace std; int main() { int x,a,b; cin>> a,b; a>b?(x=a):(x=b); cout << x ; } now i set user input for values working is same is that correct sir ? sorry for disturbing you again and again..
8th May 2018, 11:48 AM
Happy
Happy - avatar
0
ofc, you can mention me any time and I am happy to help. To clear some thinh up too, it is good practice to initialize variables with 0 for example, to not meet any miatakes of wrong values on the way, so: int x =0, a =0, b=0; even when you know that they will change soon
8th May 2018, 12:05 PM
Paul
0
okey bro.. :p nycc too meet you n i tried using cin >>a; cin >>b; but it's not working always show 5 no matter a value is 5 or b value is 5 always 5 so i tried this way and it's works Paul
8th May 2018, 12:07 PM
Happy
Happy - avatar
0
are you trying this one on a pc or on the playground here??
8th May 2018, 12:12 PM
Paul
0
I tried using playground and also in another compiler //g++ 6.3.0
8th May 2018, 12:13 PM
Happy
Happy - avatar
0
okey will try it tc c u soon bro tysm😊 for helping me.. now you're my broh😜 so i can disturb you anytime any time I'm right na😋😎
8th May 2018, 12:18 PM
Happy
Happy - avatar