FLOW CONTROL

INTRODUCTION

java-logo

Based on what we have learned so far, we know that the execution of a Java program occurs sequentially. In any software we are going to write there is a need to alter the normal flow of the program based on a number of conditions that occur. In this regard Java like all other programming languages provides us with some flow control instructions. A program always has its own execution path as we see in the figure.

This is the simplest case, the program executes instructions one after the other in a top-down sequence, eventually switching from one method to another; however, in most cases it is necessary to alter this flow during program execution based on conditions that arise.

Path di esecuzione

We also know cha Java has code blocks, that is, instructions delimited by a pair of curly brackets.

Code Block

MODIFY THE NORMAL EXECUTION FLOW

The instructions enclosed in curly brackets are part of a single block of code whose existence we have so far overlooked. It is precisely at this point that control flow or control flow comes into the picture, which allows us to move away from the simple logic of the predetermined flow to add an even very high level of flexibility by allowing us to write even very complex programs from the standpoint of application logic.

ALTERATION OF FLOW CONTROL, CATEGORIES

Flow control can be altered through three categories of instructions. The first is the Conditional Branching which allows code to take different branches based on conditions that occur. The second category is the Iterative Looping, that is, iteratively executing a block of code until a certain condition is met. A conditional test is used to cause the execution of a set of instructions (code block) as long as the outcome of this test remains true. The third category is the Jumping or unconditional jumping, which allows us to immediately stop the flow of the program to literally jump to execute some code that is in another part of the program.

Control Flow

THE IF/ELSE STATEMENT

This is the first set of instructions devoted to Branching; we will see how to make decisions within a program based on Boolean conditions. We declare an integer variable temperatura and assign to it the value 20.

if/else

The first boolean test checks whether the temperature is greater than 25 degrees if it is the block code is executed if statement
else the condition is not verified and the branch code is executed the statement else. We can also introduce more clauses else if that actually complete the if/else statement.

else if

Only one of these clauses will be chosen based on the Boolean condition contained within it. Any number of blocks can be used else if.
If none of the blocks if, else if is satisfied by the boolean condition, then the code associated with the branch else is executed. If there is a single instruction in a block or blocks as in this case the curly brackets can be omitted.

THE STATEMENT SWITCH

The statement switch is another instruction that is part of Branching. It is also a conditional test. This instruction is very useful when we have to make decisions based on a single variable.

If the value of the variable is equal to the value specified in a certain case, then the code associated with that case. The break
immediately stops the flow of code in the switch and moves execution to the first instruction that is after the switch. break is the first example of the unconditional jumps. If we omit the break the program continues with the examination of the next case. If none of the previous cases were successful you enter the branch default and that code is executed.

switch

CONSTANT LIST IN THE CASE (JDK14)

Let’s take the example from the previous version and rewrite it as shown in the image.

switch

We have the possibility of stacking a number of different houses on top of each other, since the evaluation of the houses moves from top to bottom if myVar is worth 10, 20, or 30 we enter the block of code marked “First Stack.” We can switch from case stacking to case constant list as shown in the figure.

switch

SWITCH EXPRESSION (JDK14)

It is definitely the most important innovation introduced in JDK14. So far we have seen the switch as a statement, since version 14 we can use expressions. A switch expression is now able to return a value. Let us start with a traditional example and compare the two types.

switch

In a case like this it is much better to turn the switch statement into a switch expression.

switch expression

We moved the declaration of the string variable name, which is the variable assigned by the switch that returns a value. We can do everything you do with expressions, such as return it from a method, pass it to a method as an argument, and so on. Within the switch the breaks have been eliminated, yield means return, and is a new keyword that immediately breaks the switch and produces a return value. Such a keyword assigns a value to the variable name. One important thing to say is that the code block must be closed with a semicolon. The switch expression must be exhaustive this means that the cases we are going to insert must cover all possible cases, so always use a default to make it exhaustive.

ARROW CASE (JDK14)

It is a new form of switch usage for both statement switches and expression switches. In all cases we used the cases ending in a colon. Starting with version 14 we can use an arrow instead of the colon (Arrow case).

Gone is the yield statement, however in the presence of an Arrow case the code returns exactly the values as with the yield statement, in the case in figure the switch expression returns twenty, which is then assigned to the variable name.

switch expression

One important thing to know is that an Arrow case allows a block of code to be placed in cases. Since there is a block of code we have to make the return from the case explicit with the yield instruction which should be used only with blocks of code i.e. when we have two or more instructions. If the case block has a single instruction, yield can be omitted.

Arrow case statement switch

Let us see the case of a statement switch.

switch

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

        java -jar –enable-preview CourseJava.jar

  • Or run the main found in the file CorsoJava.java.