The Concept of this Practice Program

I have just completed Chp. 4 Sec. 15 of Alternate Version of Starting ourt with C++ Third. A checkpoint question is asking me what the program will display, but I don't understand what it is supposed to be doing in the first place. I get the code. I am just haveing a little problem deciphering it. Can someone help?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
useing namespace std

int main ()
{
  const int upper = 8, lower = 2;
  int num1, num2, num3 = 12, num4 = 3;

  num1 = num3 < num4 ? upper : lower;
  num2 = num4 > upper ? mun3 : lower;
. cout << num1 << " " << num2 <<end1 ;
. return 0;
}
Lines 9 and 10 are using the Conditional Ternary Operator: http://www.cplusplus.com/doc/tutorial/operators/#conditional

Format: condition ? result1 : result2

Line 9:
num1 = num3 < num4 ? upper : lower;
This line will assign a value to num1 depending on how the rest of the code evaluates

num3 < num4 ? upper : lower;:
if num3 is less than num4, return upper(which equals 8)
otherwise, return lower (which equals 2)

The conditional operator is a handy way to shorten an if/else statement. The same line could also be written as:
1
2
3
4
if (num3 < num4)
    num1 = upper;
else
    num1 = lower;
Last edited on
I don't think the program is supposed to do anything useful. It's just trying to test your understanding of how the conditional operator (also referred to as the ternary operator) works.

https://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm
Topic archived. No new replies allowed.