(C++) 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 forms of flow control. According to computer systems theory, a good programming language should provide three forms of flow control.
 
1. Sequencing.
A sequence is a step-by-step algorithm in which operators are executed in a strict order. All our previous programs represented some sequence of operators.

2. Selection. The algorithm can change the sequence of steps depending on the logical condition.
If a condition is satisfied, 
    then do an action, 
otherwise do another action. 
 
3. Iteration. The algorithm repeats some sequence of steps a given number of times, or as long as a logical condition is met.
 As long as the condition is met
     do action.

The use of various forms of flow control allows programs to make decisions based on external factors, such as user input or data received from another part of the program.

Conditional Statement (if...else)

In programming, the conditional statement is responsible for the selection. The conditional statement allows you to change the execution of a program depending on the input data.


General view of the conditional operator

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

Rules of C++ Program Design (code style)

There is no uniform requirement for the placement of curly braces. The opening curly bracket can be placed on the same line as the operator. It is possible to form the operator as follows, which will not affect the result of program execution in any way.

if ( condition ) {
    ... 
} 
else {
    ... 
}
To improve the readability of the code, we will use the Allman's style, where the opening curly bracket is placed on a new line and the closing one also on a new line, so that it is below the opening one.
Operators within curly brackets {} are written in this case with a shift of 4 spaces to the right (2 spaces are better for large nesting) relative to the curly brackets to make it easier to read and understand the program code.

 

How the Conditional Statement Works (if...else)

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
- the code inside {} after else is skipped
If the logical condition after the if is false, then
- the code inside {} after if is skipped
- the code inside {} after else is executed
  
Remember!
1. if ... else -  it's one operator!  Therefore, no other operators can be between the bracket ending the "if" block ( } ) and the else.

2. There is never a condition after the word else. A condition is placed only after the word if. The else block is executed when the condition after the if is false.

3. If there is only one operator in the if block or in the else block, the curly brackets can be omitted.

4. A condition is an expression to which we can say whether it is true (i.e., satisfied) or false (i.e., not satisfied).

The condition uses signs of logical operators:
Operator Meaning Example
== Is Equal To 8 == 7 is false
!= Not Equal To 8 != 7 is true
> Greater Than 8 > 7 is true
< Less Than 8 < 7 is false
>= Greater Than or Equal To 8 >= 7 is true
<= Less Than or Equal To 8 <= 5 is false

5. In the programming C++, any number not equal to zero is a true condition, and zero is a false condition.

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