Conditional jump operator if
Contents
- 1. Assignment of conditional jump operator if
- 2. What general view of the full form of the conditional jump operator?
- 3. What general form of the shortened form of the conditional jump operator?
- 4. Examples of using the conditional jump operator, which has the full form of representation
- 5. Examples of using a conditional jump operator, which has an abbreviated representation form
- 6. An example of using nested conditional jump operators
- Related topics
Search other websites:
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; else operator2;
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 (condition) operator;
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.
float x, y; float max; // maximum // x, y - are given ... if (x>y) max = x; else max = 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
float x, y, a; float f; // result // x, y, a - are given ... if ((5<=a)&&(a<=10)) f = (float)(Math.sin(x)+Math.cos(y)); else f = 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 statement int a, b, c, d; // given variables int max; // 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 month int month; int days; // Input of the value month = 1..12 ... days = 31; // initial assignment if ((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 if int a, b, c; int max; // a, b, c - are given ... if (a>b) { if (a>c) max = a; else max = c; } else { if (b>c) max = b; else max = c; }
Related topics
- The if-else-if statement
- The selection operator ‘switch’
- Operations. Assignment operation ‘=‘. Ternary operation ? : . Priority of operations