Is it recommended to use Ternary operator?

Adam Lee

In C, we could use ternary like

( a == 4) ? a = b: a = 5;

Someone told me better not using it, how do you think?

Kerrek SB

The conditional operators is useful for forming expressions. Use it when you need an expression which depends, well, on a condition. Don't use it to make statements. For example, your code could be a statement:

if (a == 4) { a = b; }
else        { a = 5; }

Or you could write it with a conditional expression, but idiomatically like this:

a = (a == 4 ? b : 5);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

use of ternary operator in javascript

From Dev

c++ ternary operator use

From Dev

How to use Ternary Operator in JSP

From Dev

NullPointerException throws when I use ternary operator

From Dev

Can ngClass use ternary operator in Angular 2?

From Java

How to use the ternary operator inside an interpolated string?

From Dev

How to use ternary operator in SQL Server 2008?

From Dev

AWK|BASH, use double FS and ternary operator

From Dev

How to use continue keyword in a ternary operator in php

From Dev

How to use AND in if statement using ternary operator

From Dev

Is it ok to use ternary operator in C++ streams?

From Dev

How to use php ternary operator in the parameter of a function?

From Dev

How to use continue keyword in a ternary operator in php

From Dev

How to use AND in if statement using ternary operator

From Dev

Use ternary operator for flow control in Java

From Dev

Is it ok to use ternary operator in C++ streams?

From Dev

Ruby - Is it possible to use a ternary operator in a proc?

From Dev

Unable to use ternary operator within object data

From Dev

use ternary operator to solve multiple conditions

From Dev

How can I use a operator ternary in a string?

From Dev

Can we use ternary operator within an if statement?

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

From Dev

Ternary operator and increment operator

From Dev

Ternary Operator(Elvis Operator) ?:

From Dev

Ternary operator with "OR" operator not working?

From Dev

Can I use a ternary operator with only one result?

From Java

Java - Use ternary operator on char input to get boolean

From Dev

How to use ternary operator to change value of ng-model in AngularJS?

Related Related

HotTag

Archive