Conditional operator




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

Task
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.
C++
1
#include <iostream>    
2
using namespace std;  	    
3
main()        
4
{    	    
5
float A, B, Max;    	    
6
cin>>A>>B;      
7
Max = A;	    
8
if ( B > Max ) //  если значение B больше, чем значение Max,     
9
{    	    
10
  Max = B; //то заменяем значение Max на значение B     
11
}    	       
12
cout<<"Maximum "<<Max;    	    
13
}       
Your last submission is saved in the editor window.
     

Results:

All results: