Understanding Control Structures in Java

Chidume Kenneth
Dec 9, 2020 · 6 min read
Image by 8926 from Pixabay

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

Sequence 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.

  1. We created a class named age and prompted the user to enter their age.
  2. We read the integer entered by the user and stored it in an integer variable named age.
  3. 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.

Selection Structure

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
  1. 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.
  2. 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.
  3. 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.

Here the user entered 18 as the age, hence the output

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.

Here the user entered 12 as the age, hence the output

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:

  1. There is a control variable, called the loop counter which must be initialized( i.e given an initial value when declared).
  2. The loop condition that determines whether the looping should continue or the program should break from it.
  3. The increment/decrement of the control variable, which is modified each time the iteration of the loops occurs.
  4. 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.
  5. 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.
  6. 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.
Sample code and output using the for loop to print the first five numbers

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.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store