Conditional operator


Algorithmic constructions
Do you want to learn how to create powerful, "intelligent", versatile and useful programs?
Then you need to learn the three main forms of program execution control. According to the theory of computer systems, a good programming language should provide the implementation of three forms of control over the process of program execution:

1. Sequence:  execution of sequential statements - we have already met with this in earlier courses. All our previous programs were some sequence of statements.

2. Selection:
if such and such a case,
    then do it
otherwise do that

3. Repeat:
while (condition is true)
     do it).


The second form,  choosing between different courses of action, greatly increases the efficiency of computers.

A simple example. 
It is necessary to enter two real numbers from the keyboard and determine the largest of them.

Conditional statement (if)
In the problem of finding the maximum number of two, we met a new operator that began with the word if.
This operator is called conditional.
The word if is translated from English as "if", and the word else  as "else ". After the word if , a logical condition is written, and if it is true (true), then all commands (operators) that appear after the condition in curly brackets {} will be executed. If condition is false (false), then the commands in curly braces after the word else.
are executed  
General view of the conditional operator
if (boolean_condition ) // header with condition { ... // "if" block — statements that are executed // if condition in header is true } else { ... // "otherwise" block — statements that are executed // if condition in brackets is false }
Need to remember!
1. if- else -  is a single statement. Therefore, between the parenthesis that ends the if  (}) and the word else cannot contain other operators.
2. Never put a condition after the word else .  The "else" is executed when the main condition specified after the word if  - is false, that is, it is not fulfilled.
3. If, in the block "if" or in the "else" there is only one operator, then the curly braces can be omitted.
4. A Boolean condition is an expression that can be used to say whether it is true (meaning it is true) or false (meaning it is not true).

A logical condition is written using the signs of logical relations
>, < greater than less
>=, <= greater than or equal, less than or equal to
== equals
!= not equal

 

Let's consider the second solution to the problem of finding the maximum of two numbers. 
In the second program, we will first write the maximum value to an additional variable (let's name it Max).

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.