[Java] [Java] Conditional branch

less than 1 minute read

if statement

The if statement determines the evaluation (comparison) results such as numbers and character strings. If the evaluation result is “true”, the processing in the if block is executed. If the evaluation result is “false”, combine the else statements.

if (conditional statement) {
     // This is executed when the conditional statement is true
}
else {
     // This is executed when the conditional statement is false
}

ㆍIt is necessary to judge the condition to branch the processing such as if statement. Judgment is called “evaluation”.

relational operators

Relational operator.png

Logical operator

drow.io.png

switch statement

The switch statement can specify the code to be executed when the specified variable has a specific value. The variable is specified by switch, and the condition value is specified by case. Following the case value:, you can specify code to execute. The code block is executed up to break.

switch( variable) {
case value 1:
case value 2:
     // This is executed when the value is 1 or 2
     break;
case value 3:
     // this is executed when the value 3
     break;
default:
     // this is executed when it is not one of the values 1, 2 or 3
     break;
}