Skip to main content

Notifications

Community site session details

Community site session details

Session Id :

Ternary Operator

Hossein.K Profile Picture Hossein.K 6,648
This conditional statement behaves exactly like an if…else statement. The main
reason to use the ternary operator is convenience in coding. Its syntax is as
follows:
condition ? statement1 : statement2;
The condition is checked first and if true, statement1 is executed, if false,
statement2 is executed. However, the two expressions following the question
mark (?) must be of the same data type.
This example using the ternary operator, is equivalent in logic and results to the
previous example using the if...else statement.
int i = 12;
int j = 10;
int max;
max = i > j ? i : j;


Best Regards,
Hossein Karimi

Comments

*This post is locked for comments