Answers for "short for if"

1

short for if

#include <iostream>

//Another variation:
    a == 1 ? std::cout << "1" : (a == 2 ? std::cout << "2"  : a = 3);

//This is the same as:
if(a == 1)
{
    std::cout << "1";    
}
else if (a == 2)
{
    std::cout << "2";
}
else
{
    a = 3;
}
Posted by: Guest on February-09-2021
0

c++ ternary statement

x = condition ? expression1 : expression2

// Example:
double x = 1 > 0 ? 10 : 20; // put any value
Posted by: Guest on December-09-2020

Browse Popular Code Answers by Language