Flow Control - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 7. Flow Control

This chapter will emphasize on a non-sequential method of Java programming. You will get acquainted with the if-then-else and different loop statements.

With our previous simple program examples, we were oriented that a Java class is executed in one direction – from the topmost line of code up to the bottom, or what we call sequential programming. However, there will be cases that you will be required to write codes in a non-sequential fashion, especially for those more complicated scenarios. This is accomplished using logical and looping statements, so you can control the flow of your program to perform more complex functions.

If-Then-Else

This is the most basic flow control statement that uses logical operators to determine whether or not a specific condition is fulfilled. Let us add an if-then-else statement to our first simple java program that will display whether the user is a minor or not based on the age that he has provided. By the way, in this example, one is considered a minor if the age is below 18 years old.

In this new set of programming codes, the condition tested is if the value of the user input MyVariableName provided is less than 18 or not. If it is, then the methods in the if block will be executed - displaying a message on the computer screen that the user is a minor (check the figure above). A block is also called a compound statement. Otherwise, the else block will be executed – notifying that the user is not a minor anymore (check the figure below). The else block is actually optional, meaning without its codes the program will just proceed to the rest of the class in case the condition is not met. Please take note that the parentheses around the condition and the braces surrounding the statements that are inside the if and else blocks, are all required. Also notice the indentation of the statements these symbols enclose.

Loops

This flow control statement allows methods to be executed over and over again as long as the specific conditions are met. In Java programming language, there are three kinds of loops that can perform repetitive tasks. We will also modify the simple program to do a countdown from 10 to 1 just to demonstrate the syntax for these loops. Again, do not forget the parentheses around the conditions, the braces and the indentations.

While loop – Considered as the simplest kind of “event driven” loop, this statement will execute the methods inside the while block as long as the specific conditions are met. Depending on the conditions set, this loop may never run even once.

In the program example above, the MyVariableName was first set to a value of 10. Next, the loop will check that while the variable is greater than 0, the program will display the value of the MyVariableName in every new line and then decrement it by 1. This will continue to run untilMyVariableName is equal to 0. If at the start of program execution, the value of MyVariableName is already equal or less than 0, then the while loop will be bypassed.

Do-while loop – The main difference between this and a while loop is that the methods inside the do-while loop is sure to run at least once before the condition is checked. In contrast with the while loop, the do loop’s condition is indicated at the bottom (this is the primary reason why the loop statement is executed at least one time)

In the program example above, again, the variable MyVariableName was first set to a value of 10. In the succeeding loop statement, no condition is checked yet. Instead, the do block will have to display the value of MyVariableName in every new line and then decrement it by 1 before the while condition is checked. This is what was discussed earlier that loop will definitely run once whether or not the conditions are met. Please take note of the semicolon at the right of the condition.

For loop – This statement is declared with specific parameters that control as to when or how many times the methods are to be executed. Variables that can be used to control the loop may be declared simultaneously within the loop itself. The form of the for loop statement is:

for (initialization; condition; iteration) statement;

The initialization part sets a loop control variable to an initial value. The condition is a Boolean expression that tests the loop control variable whether it is true or false. If the result of that test is true, then the execution of the for loop is continuously repeated. If it is false, then the loop stops.

In this final example, you will notice that all the parameters were set at the beginning, when the for loop statement was declared. So in the for loop declaration, MyVariableName was set to a value of 10, the condition whether it is greater than 0 is checked and then decrement its value by 1. After all these lines of code are performed or executed then the value of MyVariableName is displayed into the screen, each value on a separate line

In reality, there are usually a multitude of ways to accomplish a certain programming task. However, a good practice as a programmer is always to code your programs in an elegant way that entails choosing the most appropriate loop. One has the option to be creative and flexible, but this sometimes this leads to confusion.

Through this chapter, you have realized that Java programming is not only one-directional by integrating flow control through various looping statements. In the succeeding chapter, you will have a clearer image of what access modifiers are.