Menu Close


Java: Control Flow

In this article I will cover one of the essential feature of any imperative programming language. This is flow control. It is a group of statements that control to logic of the program like the flow of a river.


A decision statement allow to perform one operation or another depending on certain conditions. If those conditions are met then will perform the first course of action, if they are false perhaps they will perform a different course of action.


Here we create the first conditions using relational operators ( >, <, >=, <=, ==). The code in between squiggly brackets will only execute when the first condition evaluates as true. In this situation age is greater than 21 so we will write out on the screen: “You can legally drink alcohol.”

Notice we evaluate the expression at the very top 1st and then skip the rest of the evaluations. Even though age is greater than 18, that line of code will not execute because its bellow the first condition we checked.

Second block: else if – will be executed when the first condition is not true but second condition is true. Last block: else – will be executed if none of the above conditions are true. So if age is any number bellow 18 then the system will print out “You are considered a minor.” even if that number is -1. This doesn’t make sense from a real world perspective but this is what we told our program to do.

Switch Statement

Switch statements are a more advanced way of handling decisions in code. Code that can be written using a switch can also be written using if/else conditions. Usually is preferable to use a switch when there is a variable that can have different values that can lead the execution path.

Can you guess what season it will be based on our code? We again start at the very first decision statement, we see if the number we entered is the same as the case statement. We compare 1 and 3, are they the same number? No, so we go to the next case. We compare 2 and 3, are they the same number? No, so we go to the next case. We compare 3 and 3, are they the same number? Yes, so we execute our code and then reach the “break” statement. This optional line of code tells us that we don’t have to check any other cases and we can simply exit the “switch” statement. If none of the cases we’re executed we will reach the very bottom or “default” case.


The next important control flow mechanism you need to know are the repetitive statements also known as “Loops”. There are three types of loop statements in Java:

  1. while loop
  2. for loop
  3. do… while


A while loop is the most flexible loop you can use and the most straightforward. The downside however is that while loops are easy to use but difficult to master. If you don’t think out how your code increments beforehand you will likely end up with an off-by one or index out of bounds error. In layman’s terms they are more prone to human error. Lets suppose I’m throwing eggs at someones house, but I want to throw a specific number of eggs at their house so I will count to make sure I throw the right number of eggs (let’s say my favorite number is 11).

This code will print out that I threw 11 eggs at my neighbors house. Notice the three important parts of a loop:

  1. Initializing a number I will use to count during the execution of my loop
  2. Checking for a condition (like eggsThrown being less than 12)
  3. Changing a number (counting UP each time I egg my neighbors house)

Now imagine you had to write out these parts over and over again… It would get tedious very quickly especially if you had a career in programming. So programmers invented a more time efficient way of doing everything in one easy to write statement.


Lets rewrite our code:

Notice how much cleaner we can write our code this way! And you can take my word for it, its a good deal more efficient as well.

Do While

The final loop we will discuss is the do-while loop. You will find this to be a somewhat situational loop. Most of the times you will write your loops using while or for because they are what people most commonly use. There are times when you first want to execute a piece of code and then repeat it later if a certain condition is met. This may not even be something you will use until you begin to see nuances in how code is written and executed. But that doesn’t mean you might not run across it in the wild, so pay attention!

And there you have it! These are the basics for flow control and I hope this helps you if you’re struggling with grasping some of the basics. Don’t worry! It takes time and comes with practice.