Ternary Operator Vs If Else

The java ternary operator or conditional operator is supported in languages such as java, javascript, python, c / c++, c# etc. The java ternary operator or conditional operator takes three operands, one condition followed by a question mark (?), then an expression to be executed if the condition is true followed by a colon (:), and the expression to be executed if the condition is false. It is similar to “if else” statement.

For beginners, the difference between ternary or conditional operator and if else statement is a bit difficult to understand. This page explains the difference between the ternary or conditional operator and the if-elseif-else statement.

Data based decision making is very important in programming languages. Conditional operators such as if and ternary operators will help to make decisions.



What is Ternary Operator?

Ternary Operator is a three operand conditional operator. It’s a simplified if-else statement format. The ternary operator’s first operand is a condition, should return a boolean value. The second and third operands are expressions that are executed based on a condition basis. If the condition of the first operand is met and returns true, the second operand expression is executed. Otherwise, the third operand expression is called. The ternary operator or conditional operator is supported in languages such as java, javascript, python, c / c++, c# etc.

Syntax of Java Ternary Operator

result = condition ? expressionIfTrue : expressionIfFalse;

condition
A conditional expression, returns a boolean value

expressionIfTrue
An expression to be executed if the condition is true

expressionIfFalse
An expression to be executed if the condition is false

Java Ternary Operator example

int max = (a>b) ? a : b;

If the value in a variable is greater than b value, then the max is assigned with a value. Otherwise the b value is assigned in max variable.



What is If statement?

If statement consists a condition followed by block of statements. If the condition evaluates to true, the block of statement inside the if statement is executed. If not, block of statement inside the else part is executed. The if else statement is supported in most of the languages examples like java, javascript, python, c / c++, c# etc.

Syntax of Java IF statement

 if(condition) {
     statement 1;
     …
     statement n;
 } else {
     statement 1;
     …
     statement n;
 }

Example of Java IF statement

 if(a>b) {
     int c = add(a,b);
     calculate(a,c);
     print("success");
 } else {
     int c = add(a,b);
     calculate(b,c);
     print("failed");
 }


Difference between Ternary Operator and If Else

Ternary Operator is a programming statement. “If-Else” is a programming block. The statement is, of course, faster than Block.

In the initialization of variables, the Ternary Operator can be used where as if conditions can not be used. For example, variable “max” is assigned with value either a or b based on condition.

Ternary Operator is more readable than if conditions. Ternary Operators are considered more expert and professional way of writing codes compare to using if else statements

The Ternary Operator is not suitable for executing a function or several statements. In this case, if else is more appropriate.

If condition is preferred in case if program requires executing only on true block. In this case, it is necessary to work around to use Ternary Operator.

Nested Ternary Operator is not readable and can not be debugged easily. If else is more readable and easier to debug in case of issue.



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *