Module: Java. Conditional operator


Problem

2/17

Conditional statement - IF

Theory Click to read/hide

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

 

Problem

Complete the program that outputs the “-” (minus) sign if the number entered from the keyboard is negative, and the “+” (plus) sign if the number is positive (do not take into account that zero can be entered from the keyboard)

1. In the 6th line in brackets write the condition that you will check
2. In line 8, write the output statement that will be executed if the condition is TRUE (fulfilled)
3. In line 12, write the output statement that will be executed if the condition is FALSE (not satisfied)