Java. Conditional operator


Want to learn how to create powerful, "intelligent", versatile and useful programs?

Then you need to learn the three main forms of process control. According to the theory of computing systems, a good programming language should provide the implementation of three forms of control over the process of program execution:
1 - SEQUENCE:  Executing sequential statements - we’ve already seen this in earlier courses. All our previous programs were a sequence of statements
2 - ВЫБОР:
         if such and such a case,
               then do it
         else do that
3 - REITERATION:
          WHILE (as long as such and such cases - do it)

The program is becoming more “intelligent,” and also greatly increases the efficiency of computers in the second form - the CHOICE between different methods of action.

Let's start with a simple example.
It is necessary to enter two real numbers from the keyboard and determine the largest of them.
Parse this example.

In the problem of finding the maximum number of two, we met a new operator that began with the word IF
This statement is called - CONDITIONAL
If the condition after the word if is true (true), then all commands (statements) that follow the condition in curly braces {} are executed. If the condition is false (false), then the commands in braces after the word else are executed.

GENERAL TYPE OF THE CONDITIONAL OPERATOR:
if (condition) // header 
{
   ... // block “if” - statements that are executed,
       // if the condition in the header is true
}
else
{
   ... // block “otherwise” - statements that are executed,
        // if the condition in brackets is false
}
REMEMBER:
1. IF - ELSE  -  THIS IS ONE STATEMENTS!
   Therefore, between the bracket ending the block "if" (}) and the word else can not be other statements
2. after the word else NEVER A CONDITION IS PERFORMED.  (If you want to check another condition, then write another if immediately after the word else)
    The “otherwise” block is satisfied when the main condition indicated after the word IF is false, i.e. not performed
3. If, in the block “if” or in the block “otherwise” there is only one operator, then curly brackets can be omitted
4. CONDITION is an expression with respect to which one can say whether it is true (that is, fulfilled) or false (that is, not fulfilled)
  In the condition, you can use signs of logical relations
   > , <                more less
  >=, <=             more or equal, less or equal
  ==                    equally
  !=                     not equal

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

 

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 (give it the name Max)

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