Module: Java. Conditional operator


Problem

12/17

Incomplete conditional statement

Theory Click to read/hide

If you don’t have to do anything in the “otherwise” block (for example: “if there is ice cream on sale, buy ice cream”, but if not ...), then the whole block “otherwise” can be omitted and the abbreviated (incomplete) form of the conditional statement can be used:
if ( condition )
 {
   ... // what to do if the condition is true
 }
Consider an example of solving the problem of finding the maximum of two numbers, using the incomplete form of the conditional statement

Problem

To solve the problem of finding the maximum of two numbers, you can use the incomplete form of the conditional operator.

Using the additional variable Max, the initial value of which is set equal to the value of variable A
Next, we check if the value of the variable B is greater than the value of the variable Max, then replaces the value of the variable Max with the value of the variable B.

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