## Conditional jump operator if

##### Content

**Assignment of conditional jump operator if****What general view of the full form of the conditional jump operator?****What general form of the shortened form of the conditional jump operator?****Examples of using the conditional jump operator, which has the full form of representation****Examples of using a conditional jump operator, which has an abbreviated representation form****An example of using nested conditional jump operators**

##### 1. Assignment of conditional jump operator if

Often, when writing programs, it is necessary that the course of the solution of the problem is performed depending on a certain condition. To do this, Java (and other programming languages) uses the if conditional jump operator.

The conditional jump operator allows organizing branching of the execution process in the program.

The conditional branch operator can be composite, that is, contain other operators. This may be operators who implement sequential execution, cyclical process and conditional jump.

Conditional jump operator has two forms of presentation:

- full form if … else;
- shortened form if … .

##### 2. What general view of the full form of the conditional jump operator?

General view of the conditional jump operator in full form:

if(condition) operator1;elseoperator2;

First, the *condition *value is checked. If it is true, then the *operator1* is executed. Otherwise, the *operator2* is executed.

If you need to execute several statements, then they are taken in the brackets { }.

##### 3. What general form of the shortened form of the conditional jump operator?

General form of the conditional jump operator in shortened form:

if() operator;condition

The operator in shortened form is executed as follows. First, the *condition* value is checked. If it is true then the *operator* is executed. If the *condition *value is not satisfied (false), then the next operator is executed (nothing is done).

If you need to execute several statements, they are taken in the bracket { }.

if(condition) { // several operators ... }

##### 4. Examples of using the conditional jump operator, which has the full form of representation

**Example 1**. Fragment of the program code, which contains the maximum value between two numbers.

floatx, y;floatmax; // maximum // x, y - are given ...if(x>y) max = x;elsemax = y;

**Пример 2.** Determine the value of the variable f, depending on the entered value of the variables a, x, y. Using logical expressions to calculate the value.

A fragment of the program code that solves this expression

floatx, y, a;floatf; // result // x, y, a - are given ...if((5<=a)&&(a<=10)) f = (float)(Math.sin(x)+Math.cos(y));elsef = 2*x + (y*y)/(x*x+3);

##### 5. Examples of using a conditional jump operator, which has an abbreviated representation form

**Example 1.** There are four variables a, b, c, d in which some values are written. From the set of variables to find the maximum.

// a shortened form of the if statementinta, b, c, d; // given variablesintmax; // maximum - result // a, b, c, d - are given ... max = a;if(max<b) max = b;if(max<c) max = c;if(max<d) max = d;

**Example 2.** A fragment of the program, which, depending on the month number, records the number of days in this month in the variable “days“. The “month” number montn is specified by an integer from 1 to 12.

// shortened form of the if statement // determine the number of days in a monthintmonth;intdays; // Input of the value month = 1..12 ... days = 31; // initial assignmentif((month==4)||(month==6)||(month==9)||(month==11)) days = 30;if(month==2) days = 28;

##### 6. An example of using nested conditional jump operators

Conditional jump operators can have an unlimited number of nesting levels. The following example demonstrates the use of nested conditional jump operators.

**Example.** Searching for the maximum value between three numbers a, b, c.

// nested operators ifinta, b, c;intmax; // a, b, c - are given ...if(a>b) {if(a>c) max = a;elsemax = c; }else{if(b>c) max = b;elsemax = c; }

##### Related topics

- The if-else-if statement
- The selection
**operator ‘switch’** **Operations.**Assignment operation ‘=‘. Ternary operation ? : . Priority of operations