Module: Conditional operator


Problem

2/17

Conditional operator - IF

Theory Click to read/hide

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

 

Problem

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

1. In the 6th line, in parentheses, write the condition that you will check.
2. On the 7th line, write an output statement that will be executed if the condition is true (is true).
3. On the 10th line, write an output statement that will be executed if the condition is false (not true).