Module: Conditional operator


Problem

12/17

Incomplete conditional statement

Theory Click to read/hide

If in the "else" block you don’t have to do anything (for example: “if there is ice cream on sale, buy ice cream”, and if not ...), then the entire block “otherwise” you can omit and use an abbreviated (incomplete) form of the conditional operator:
if ( condition )
 {
   ... // what to do if the condition is true
 }
Let's consider an example of solving the problem of finding the maximum of two numbers using an incomplete form of the conditional operator.
 

Problem

You can format the solution to the problem of finding the maximum of two numbers using the incomplete form of the conditional operator.

You can use an additional variable Max by setting the initial value equal to the value of the variable a.
Next, we check if the value of the variable is greater than the value of the variable Max, then it replaces the value of the variable Max with the value of the variable b< /em>.

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