Skip to content

Latest commit

 

History

History
37 lines (25 loc) · 992 Bytes

TernaryOperators.md

File metadata and controls

37 lines (25 loc) · 992 Bytes

Ternary Operators in C++ (Conditional Assignment)

Video

<condition> ? <true-case-code> : <false-case-code>;

The ternary operator allows you to execute different code depending on the value of a condition, and the result of the expression is the result of the executed code. For example:

int five_divided_by_x = ( x != 0 ? 5 / x : 0 );

This operator is a syntactic sugar for a if-else statement

static int s_Level = 1;
static int s_Speed = 2;

int main()
{
    if (s_Level > 5)
        s_Speed = 10;
    else
        s_Speed = 5;

    // it's the same as
    s_Speed = s_Level > 5 ? 10 : 5;

    std::string rank = s_Level > 10 ? "Master" : "Begginer";
}
// nesting can become a little hard to read
// so you should consider avoiding
s_Speed = s_Level > 5 ? s_Level > 10 ? 10 : 5 : 5;