As we know, a program consists of lists of instructions. Control structures are programming block that can change the path we take through those instructions or not. It is a syntactic form in a programming language to express flow of control.
In java and also in every other programming language there are three basic control structures:
- Sequence Structure
- Repetition Structure
- Selection Structure
This involves the sequential execution of java statements one after the other in the order in which they are written inside the source code — much like reading a recipe for preparing a food or a book. They can carry out a series of read or write operations, arithmetic operations or assignments to variables. This is the simplest of the three basic java control structure. Below is a sample code that illustrates this type of control structure.
Looking at the above code, we can easily see the order or sequence of the operations as listed below.
- We created a class named age and prompted the user to enter their age.
- We read the integer entered by the user and stored it in an integer variable named age.
- We displayed on the console “see you”.
As we can see, from the programs output the various instructions in the code were sequentially executed. This same order will be followed if we were to run this same program for any number of time we want to run it.
In java, Selection statements are also known as decision making statements or branching statements or conditional control statements. They are used to select a part of the program to be executed based on a condition. They allow choosing between two or more paths for execution of any particular program. There are three types of selection statement used in java;
- Single selection
- Double selection or two way selector
- Multiple selection or n-way selector
- Single Selection: if statement is an example of this type of selection statement. It selects or ignores a single action or single group of actions, that is if the given condition is true, it carries out or executes a given action(s) but if false it ignores it and continues with the next statement or line of code immediately after the ignored if statement block.
- Double selection: if …else statement is an example of this type. This type of selection statement performs a set if action(s) if a particular condition is true and another set of action(s) if false.
- Multiple Selection: An example of this type is a switch case statement. Unlike if and if..else which are single and double selection respectively, switch statement can have multiple execution paths, out of which only one whose condition is true is executed.
Looking at the various selection statement structure, one could see that execution is not sequential rather depends on the condition of any of the selection types used in the program. If the condition is true the block of code contained in the selection type used is executed but if false the block of code is jumped or skipped and then execution continues with the next line of code after the selection statement block.
Using the program already written let us modify it such that when a person enters his or her age, the program can decide whether he/she can be granted access to a seminar venue -assuming the age range of qualification for one to be granted access to the seminar is 18 or more.
Making use of the above code and its output to illustrate the selection structure, we could see that starting from the import statement (line 1) the program execution was sequential but on getting to the start of the if…else statement block( line 8), the program checks the age inputted by the user to know if it satisfied the condition ( age greater than or equal to 18), if true the block of code within the if statement is executed, that is “Access granted” is displayed on the console but if the condition is false the ‘if’ block is jumped or skipped and the else block gets executed, that is “Access denied” and “you aren’t up to age” is displayed on the console as shown below.
From the foregoing one could see that this program does not follow a sequential structure and the selection statements help us to achieve this in java.
3. Repetition Structure
Repetition statements are also called loops and it is a block of code to be executed for a fixed number of times or until a certain specified condition is met. It encircles the flow for a while. This structure is similar to the selection structure in the sense that once the said or specified condition or conditions is/are met or true, the repetition statement block is executed but differs that the execution of this block is carried out continuously (looping) until the condition becomes false then this block is exited and flow of control continues from the next line of code or statement immediately after the repetition statement. Java provides three repetition statements or looping statements that enable programmers control the flow of execution and they are;
- While Repetition Statement
- Do…While Repetition Statement
- For Repetition Statement
Before we look into them one by one, we must have to know that there is a common structure for all types of loop or repetition statements, such as:
- There is a control variable, called the loop counter which must be initialized( i.e given an initial value when declared).
- The loop condition that determines whether the looping should continue or the program should break from it.
- The increment/decrement of the control variable, which is modified each time the iteration of the loops occurs.
- While Repetition Statement: Here, the condition is evaluated first and if it returns true then the statements inside the while loop execute, which includes the three structures listed above. When this condition returns false, the control comes out of the loop and jumps to the next statement after the while loop.
- Do …While Repetition Statement: This is similar to the While loop, except that a do …while loop is guaranteed to execute at least one time. The boolean expression or condition to test appears at the end of the loop, so the statements in the loop execute once before the condition is tested. If the condition returns true, the control jumps back up to the statement and the statement in the loop execute again. This continues until the loop condition becomes false. It also contains or is made up of the three structures prior listed.
- For Repetition Statement: The structure of this loop is also similar to the while and do…while loops. It is recommended for use when the number of iterations is fixed. Also the control variable declaration and initialization, the loop condition and the increment or decrement of the control variable are all stated in a single line of code, and if any of it is omitted or not followed in the order as written above will cause a syntax error.
As we have seen, the repetition structure execution is not sequential rather the control flow loops around the repetition statement provided the specified condition is true and exits when false.
We have seen that the default execution structure of a java program is the sequence structure but the programmer can decided to take control of the execution flow and change it to his/her desired flow by making use of the selection or repetition structure statements a the case maybe to achieve whatever goal or program the programmer desires to achieve. Furthermore, this three control structure discussed in this article forms the basis of any java program,with these three java control structures or combination of it programs of any kind can be written in java successfully.