Skip to content

Latest commit

 

History

History
71 lines (43 loc) · 1.29 KB

File metadata and controls

71 lines (43 loc) · 1.29 KB
title categories subCategories
ternary
Structure
Control Structure

?: Ternary Operator

Description

It takes three arguments rather than the typical one or two that most operators use. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. It is an alternative to shorten an if-else block. It can help increase the readability and reduce the number of lines in your code.

Syntax

expression_1 ? expression_2 : expression_3; // if expression_1 evaluates to true then expression_2 is evaluated else expression_3

Parameters

expression. An expression is any legal combination of symbols that represents a value.

Example Code

int x = 10, y = 20, z; // declared and defined three variables x, y and z
z = (x < y) ? x : y;
// the statement checks the conditional expression and assigns z the value of x, the expression x<y is true

See also