What is an if statement

Java-Tutorial.org

if statements

Another simple control structure is the simple alternative, also known as the if statement. The if-Instruction is used for the conditional execution of instructions. The if statement has the following structure:

/ * If the boolean expression is true, then execute statement1, otherwise do nothing * / if (boolean expression) statement1;

The if statement starts with the keyword if, followed by a Boolean expression in parentheses. The round brackets are a must. The Boolean expression is the condition for the execution of instruction1. Is the condition true (true), the instruction is executed. The above spelling of a if-Instruction is only possible if there is only one instruction after the condition. If several statements are to be executed under one condition, the if-Condition an instruction block follow. We recommend that you use statement blocks even if you only have one statement. This has two advantages: On the one hand, the source code is clearer and, on the other hand, possible errors are reduced because you might want to add further instructions later, but quickly forget to add the brackets afterwards.

With the statement block, the ifStatement looks something like this:

/ * If the boolean expression is true, then execute instruction1 through instructionN, otherwise do nothing. * / if (boolean expression) {statement1; Instruction2; ... instructionN;}

The if-Instruction can still be used with the so-called else-Branch to be expanded. The statements in the else branch are then executed if the Boolean expression is false (false) is. It also applies to the else branch that a single statement does not require a statement block, but several must be grouped in curly brackets. However, we only want to work with statement blocks here.

/ * If the condition is true, then execute statement1 and statement2 * / if (condition) {statement1; Statement2;} / * otherwise execute statement3 and statement4. * / else {statement3; Statement4;}

Formulated the above example means:
"If the condition is true, then execute instruction1 and instruction2, otherwise execute instruction3 and instruction4."

The if-Instruction is also referred to in German as an if-then-instruction.

You can also do several if- Nest instructions. One then speaks of a multiple alternative:

// If the condition is true, then execute instruction1.if (condition1) {instruction1;} // Otherwise execute instruction2 under condition2.elseif (condition2) {instruction2;} / * If condition1 and condition2 are not true are execute statement3. * / else {statement3;}

Here would also have the second if-Instruction can be in a separate statement block, which increases the readability of the source text:

/ * If the condition is true, then execute statement1. * / if (condition1) {statement1;} / * otherwise execute the else branch. * / else {if (condition2) {statement2;} / * If condition1 and condition2 are not true, execute statement3. * / else {statement3;}}


This notation makes it immediately clear that it is a nested ifInstructions and that instruction3 can only be executed under the precondition that the first and second conditions are false.

Let us now turn to some examples for that ifInstruction. We would like you to consider which part of the instructions is being carried out and why. For each example there is also a button with which you can find out the solution.

Example 1:

int a = 5, b = 6; if (a
Example 2:

int a = 5, b = 6, c = 7; if (a
Example 3:

int a = 5, b = 6, c = 7; if (a> b) {a = b;} else {if (a At this point we would like to encourage you to experiment a little with the if statements. Create a starting class, take the examples or create your own, and modify the variables and conditions to become more familiar with conditional statements.