+ 2

The Ternary Operator and IF / Else statement.

Is it better to use just Ternary Operator or IF/Else statements... OR I can use both of them and it is not gonna be less readable?? :) I am asking, in case I would like to use both of them but still keep a good visual site of the project. Thanks ;)

25th Jul 2018, 3:16 PM
Daniel Herrman
Daniel Herrman - avatar
3 Réponses
+ 2
Ternary Operator is commonly used for assigning values, while if/else is for executing functions and chaining multiple conditions. You can use both for readability. // Ternary ========= a = (b > c) ? 1 : 0 // if/else ========= if (isReady()) { onTextureLoaded(); } else if (isLoading()) { onLoading(); }
25th Jul 2018, 3:59 PM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 1
Mostly ternary operator are used for small purposes. Example : char equalTo[ ] = ( a == b ) ? "Equal" : "Not equal to"; AND if else statement are used for multipurpose. Example : See this https://code.sololearn.com/WDj7OlB8J2Qm/?ref=app Hope this helps ☺️☺️.
25th Jul 2018, 5:21 PM
Meet Mehta
Meet Mehta - avatar
+ 1
Thanks for the answers !! :)
25th Jul 2018, 5:43 PM
Daniel Herrman
Daniel Herrman - avatar