When should I use an IF statement? When should I use ternary operators? Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When should I use an IF statement? When should I use ternary operators? Why?

7th Jul 2016, 11:59 AM
Arturo Arena
Arturo Arena - avatar
2 Answers
+ 2
suppose if u want to print a value based on few conditions like print 'a' when it is positive print 'a+1' when equal to 0 print 'a-1' when it is negative then u should use if - else if. however u can also use ternary on it but using if-else if increases readibility of code use ternary when things are small
12th Jul 2016, 11:04 AM
Rishabh Abhani
Rishabh Abhani - avatar
0
just remember one thing performance wise both are same but multiple ternary operator sometime create confusion. otherwise they are same as per apple documentation
2nd Sep 2016, 10:34 AM
Inder Mishra
Inder Mishra - avatar