Skip to content

Conditional Statements

What's the conditional statement?

A conditional statement is an instruction defined in the syntax of a specific programming language, allowing you to determine and change the order of execution of instructions contained in the source code. In Java there are several designs for conditional statements.

The IF statement

This is the simplest form of conditional statements. It checks the logical condition and if it is true, the instructions in its body (inside the block) are executed, otherwise they are omitted. The structure is as follows:

if (condition) {
    // Follow the instructions inside the block if the condition is true
}

A block diagram is shown below:

instruction if

Example:

float temperature = 38.5f;
if (temperature > 36.6f) {
    System.out.print("You have a fever/heat state!");
}

In the example above, the output will be displayed: You have a fever/heat state!

IF ELSE

In the if else instruction, the code in the else block is executed if and only if the logical condition declared in parentheses if(...) is not met. The diagram of this construction can be written as follows:

if (condition) {
    // {\pos(192,220)}take the code for the condition that you've met
} else {
    // ...execute a code for an unfulfilled condition...
}
A block diagram is shown below:

instruction if else

Example:

float temperature = 36.6f;
if (temperature > 36.6f) {
    System.out.print("You have a fever/heat state!");
} else{
    System.out.print("You're healthy/healthy!");
}
It is also possible to build a mixed construction of if... else if... else with any number of blocks else if, which gives you the possibility to branch the expression to multiple conditions. The diagram of this construction can be written as follows:

if (condition1) {
    // execute the code for condition condition1 fulfilled.
} else if (condition2) {
    // execute the code for condition 1 not fulfilled and condition 2 fulfilled
}
... // " another block else if
} else {
    // execute the code if none of the preceding conditions are met
}
Example:

float temperature = 36.4f;
if (temperature >= 37.0f) {
    System.out.print("You have a fever/heat state!");
} else if (temperature >= 36.6f && temperature < 37.0f) {
    System.out.print("You're healthy/healthy!");
} else {
    System.out.print("You are weakened!");
}

In the example above, the output will be displayed: You are weakened!.

SWITCH instructions

The last schema we discussed can be replaced by a more convenient method, which is at the same time more readable, the switch instruction. It is characterized by the fact that:

  • It covers multiple expressions of if-else
  • It consists of many conditions
  • It has a default default block if the other conditions are not met
  • From Java SE7 it is possible to use the variable type String.

The design is as follows:

switch(variable) {
    case number one:
        // execute the code in case the variable = value1
        break;
    case number two:
        // execute the code in case the variable = value2
        break;
        // other cases...
    default:
        // ...perform if and only if none of the above conditions are met...
}

Pay attention to the keyword break:

  • if the condition is met, the code block will be executed up to the break directive,
  • if it's missing, a code block will be executed from the next case.

If none of the cases of the case is fulfilled, the code block contained in the default will be executed. The limitation of the switch instruction is the type on which we can execute conditional instructions. At present, simple types and their equivalents representing objects can be used, i.e., the `witch:

  • int, Integer
  • Byte, Byte
  • Char, Char
  • short, short
  • String
  • any enum

A block diagram is shown below:

switch instruction

Note also that the keyword break in some cases is specifically omitted and the section default is not mandatory, but should be implemented in most cases, e.g:

switch (month) { // month is of enum java.time.Month
  case APRIL: // no break instructions
  JUNE case:
    System.out.println("Month has 30 days");
    break;
  JANUARY case: // no instructions break
  MARCH case:
    System.out.println("Month has 31 days");
    break;
   FEBRUARY case:
     System.out.println("Month has always less than 30 days");
}
Java 14 also introduces a new way of saving switch instructions. The character : for the condition case can be replaced by ->. The use of -> instead of : also relieves you of the need to write the break instruction because you always execute the code only at a particular block of the case. If you want to execute the same code for multiple values, we should separate them by commas. The examples below show a new, alternative syntax for the switch expression.

``java switch (number) { case 1 -> System.out.println("I'm one"); case 2 -> System.out.println("I'm two"); case 3 -> System.out.println("I'm three"); default -> System.out.println("I am a number other than 1, 2 and 3"); }

``java
switch (number) {
  case 1, 3, 5, 7, 9 -> System.out.println("I am the odd positive number");
  case 2, 4, 6, 8 -> System.out.println("I am an even positive number");
  default -> System.out.println("I'm not a positive odd number");
}