# Selection statements in the Java programming language

## Logical Expression- introduction.

The basic element of all programs is decision making. You need to be able to choose between different options, for example, "If I have couples in the account I will buy a new car, and if I do not have a buyer a monthly ticket for the bus." Program decision-making is realized by relational operators and corresponding logical expressions.

*Statement if*

Statement if it is an elementary logical expression. Its simplest form is:

if (expression) statement;

The expression can be any expression that gives the value true (true) or false. If the value of the expression is true, then the statement follows, otherwise it will not.

if (expression) statement;

The expression can be any expression that gives the value true (true) or false. If the value of the expression is true, then the statement follows, otherwise it will not.

*Example 1:* Correct Data Entry:

a) Load integers a and b and determine the quotient a / b;

b) Calculate the surface area for the radius of the radius of the circle

b) Calculate the surface area for the radius of the radius of the circle

*Example 1-solution*

a) Assume that a and b are entered via the input dialog. Let's also assume that the result will be placed in a variable that we will mark with c. If we want to calculate the quotient without conditions, we would write:

This is true for every integer except for b = 0;

In order to exclude the case when b = 0, we have to insert the command c = a / b into the body of an order where it will be executed under a certain condition:

The expression b! = 0 (b is different from zero) is a condition. It is in fact a logical expression that will return true if b is really different from zero or false if it is not.

The order executed in the body, if in this case, did not have to be covered by curly brackets as it is only one, whereas in the case that more than one order should be executed more than one curly brace would be required. So in this case it could have been written as follows:

**c = a / b;**This is true for every integer except for b = 0;

In order to exclude the case when b = 0, we have to insert the command c = a / b into the body of an order where it will be executed under a certain condition:

**if (b! = 0) {**

c = a / b;

}

c = a / b;

}

The expression b! = 0 (b is different from zero) is a condition. It is in fact a logical expression that will return true if b is really different from zero or false if it is not.

The order executed in the body, if in this case, did not have to be covered by curly brackets as it is only one, whereas in the case that more than one order should be executed more than one curly brace would be required. So in this case it could have been written as follows:

**if (b! = 0) c = a / b;****b)**Similarly to the example a), if the condition of the variable P representing the surface of the radius circle r without the conditions were used, the command would be:

**P = Math.PI * r;**

In order to verify that the radius of the radius r is a positive number, we will use if the command:

if (r> 0) P = Math.PI;

In these examples, commands are executed only if the condition in small brackets is true. If an incorrect command is simply not executed.

There is often a need for an alternative variant, i.e. that there is an order that will be executed if the condition is incorrect. For example. in the previously described case, if the condition r> 0 is not fulfilled, it would be possible to print the message that r is incorrectly entered. For alternatives, the clause else is used so that the command now looks like:

**if (r> 0)**

P = Math.PI;

P = Math.PI;

**else**

System.out.println ("The radius value must be positive");

System.out.println ("The radius value must be positive");

**Example**

**if (number% 2! = 0)**// check whether the number is odd

**++ number;**// if it is odd, let it become a paran

This statement can be written in some other way, but this form is recommended.

**if (number% 2! = 0) ++ number;**// this should not be, though it is possible

If it is necessary that, as a result of the statement, if several statements are made, and not just one as in the previous example, the braces can be used.

**if (expression) {statement1; iskaz2; ...}**

Example:

**if (number% 2! = 0) {**// check if the number is odd

**++ number;**// if it is odd, let it become a paran

**System.out.println ("The number is converted to a paran and its value is now" + number);**

If the expression is evaluated as true, all statements are executed between the curly braces, and if it is not, none is executed.

*Clause else*

The basic statement if can be expanded by the clause else.

The statement or statements that follow behind the clause else are executed only if the phrase in the clause if it is not correct. If it is necessary to make more statements in the clause else, the braces can also be used.

**if (expression) iskaz1;**

else iskaz2;else iskaz2;

The statement or statements that follow behind the clause else are executed only if the phrase in the clause if it is not correct. If it is necessary to make more statements in the clause else, the braces can also be used.

**if (expression) {**

iskaz1;

...

}

else {

iskaz2;

...

}iskaz1;

...

}

else {

iskaz2;

...

}

*Clause else if*

If there are more options when making the gutter, the else if can be used.

If expression 1 is evaluated as true, statement 1 is executed. If expression has no value true, the expression expression expression2 is checked. If its value is correct, the statement statement is executed2. After executing the statement statement2, the program continues with the code that follows the entire statement if. If the expression expression expression2 is not true, the expression expression expression3 is checked etc. If none of the expressions in the else if clauses are evaluated as true, statements are made behind the clause else, if one exists. Exiting the program is: Score = C;

**if (expression1) iskaz1;**

else if (expression2) iskaz2;

else if (expression3) iskaz3;

else iskaz4;else if (expression2) iskaz2;

else if (expression3) iskaz3;

else iskaz4;

If expression 1 is evaluated as true, statement 1 is executed. If expression has no value true, the expression expression expression2 is checked. If its value is correct, the statement statement is executed2. After executing the statement statement2, the program continues with the code that follows the entire statement if. If the expression expression expression2 is not true, the expression expression expression3 is checked etc. If none of the expressions in the else if clauses are evaluated as true, statements are made behind the clause else, if one exists. Exiting the program is: Score = C;

public class Test{ public static void main(String[]args) { int testResult=76; char grade; if(testResult >= 90) { grade='A'; } else if(testResult >= 80) { grade='B'; } else if(testResult >= 70) { grade='C'; } else if(testResult >= 60) { grade='D'; } else { grade='E'; } System.out.println("Grade="+grade); } } |
As you can see the value of a variable testResult can satisfy more than one condition, because 76 >= 70, and also the testResult >= 60. The score is still C, because only the first block of code belonging to the expression testResult >== 70. When this block is executed, program control goes to the code behind the statement if, in this case on the System.out statement ... The terms that follow behind the expression that was satisfied are not checked, and therefore the corresponding code is not executed.
The statements if they can be nested. In the context of one statement, one can find another, in the context of that second, third, etc. Here's what it looks like: if (expression1) {if (expression1-1) { if (expression1-1-1) iskaz1-1-1; } else iskaz1-1; } |