Selection statements in the Java programming language
In simple examples in the C programming language, commands were executed in one flow, ie. one after the other, until the very end of the program. Sometimes in the program some commands are executed under a certain condition.
For example. consider the following problem: For the entered numbers a and b, calculate the value of the fraction a / b.
After the declaration of variables and entries a and b by the user, we come to the part where it is necessary to calculate the fraction. Is it possible to simply introduce the variable c and calculate c = a / b?
It is known that division with zero is not allowed. This means that before writing the formula written above, it must first check whether the condition that b is different from zero is met. If so, then you can write a formula and print the result. Otherwise, something alternative can be done, e.g. to write a message that division with zero is not allowed, or to do nothing.
We conclude that there are two variants of program execution, or only one, which depends on the condition of whether b is different from zero or not. This means that branching occurs in the program and that branching is done on the basis of logical data or expressions.
For example. consider the following problem: For the entered numbers a and b, calculate the value of the fraction a / b.
After the declaration of variables and entries a and b by the user, we come to the part where it is necessary to calculate the fraction. Is it possible to simply introduce the variable c and calculate c = a / b?
It is known that division with zero is not allowed. This means that before writing the formula written above, it must first check whether the condition that b is different from zero is met. If so, then you can write a formula and print the result. Otherwise, something alternative can be done, e.g. to write a message that division with zero is not allowed, or to do nothing.
We conclude that there are two variants of program execution, or only one, which depends on the condition of whether b is different from zero or not. This means that branching occurs in the program and that branching is done on the basis of logical data or expressions.
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:
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;
}
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;
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;
}
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;
else
System.out.println ("The radius value must be positive");
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;
else
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.
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.
if (expression) iskaz1;
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;
...
}
if (expression) iskaz1;
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;
...
}
Clause else if
If there are more options when making the gutter, the else if can be used.
if (expression1) iskaz1;
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;
if (expression1) iskaz1;
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; } |
Previous
|< Data in Java programming |
Next
Loops in Java >| |