Module: (C++) Conditional operator


Problem

12/17

Incomplete Conditional Statement (if)

Theory Click to read/hide

Incomplete Conditional Statement (if)

If you don’t have to do anything in the else block (for example: “if there is ice cream on sale, buy ice cream”, but if not?), then the block else can be omitted and the incomplete form of the conditional statement can be used.


General view of the incomplete conditional operator

if ( condition ) // header if...else statement
{
    ...  // block "if" - operators that are executed, 
         // if the condition in the header is true
}
 

How the Incomplete Conditional Statement Works (if)

If the condition is true If the condition is false
If the logical condition after the if is true, then
- the code inside {} after if is executed
If the logical condition after the if is false, then
- the code inside {} after if is skipped
   

Problem

To find the largest of two numbers, you can use an incomplete conditional operator. To do this, create an additional variable (mx), in which we initially save the value of the first variable. 
Then compare the value of the second variable with mx and, if the value of the second variable is greater than the value of mx, replace the value of mx with the value of the second variable.
As a result the variable mx will contain the maximum value of the two variables. Its value will be outputed on the screen as the answer.

With this scheme it is easy to find the maximum value of three or more numbers. 

Complete the program so that it finds the maximum value of two variables according to the algorithm.