Ternary Numeric Result in C++

Level 266 keystrokes

Uses the ternary operator to store the larger of a (5) and b (10) in max, and prints 10.

C++ code

// Declares two ints in one statement, separated by a comma: a = 5, b = 10
1int a = 5, b = 10;
// If a > b, max gets a, otherwise b
// 5 > 10 is false, so max becomes b, which is 10
2int max = (a > b) ? a : b;
// Prints the larger value, 10
3cout << max << endl;

Output

10
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 4
  • ;× 3
  • =× 3
  • ,× 1
  • :× 1
  • ?× 1
  • (× 1
  • )× 1
  • >× 1

More C++ examples at level 2

← C++ typing practice