Select Page

Welcome to “Java Diaries” a series in which I document my self-teaching of Java as both a record of what I’ve done and a reference for me to check back on later – hopefully aiding the information I’ve been consuming to sink in. Click here to view all posts in this series.

Control Flow Statements

After the last post in which we covered Method Overloading, this time it was on to the first of Control Flow Statements which are, as the name suggests, statements which control the flow of the code. These statements are essential tools which allow you to be able to decide and control where the program goes. They are as follows:

  • Switch statement
  • For statement
  • While statement
  • Do-While statement

The first one of these to be covered on the Udemy course was the Switch statement.

Switch Statement

The Switch Statement can help reduce the complexity of If statements but this doesn’t automatically mean you will just swap to Switch statements. Both if and switch statements have their positives and negatives and if it is possible to use both which one you use can be a matter of preference and style.

For example, if statements can be more flexible as you test various variables each time whereas with the switch statement you can only test one variable at a time. If you’re only testing one variable then great but if not then you will have to use the if statement.

int myInt;
int mySecondInt;

if (myInt >= 10 && mySecondInt <=20) {
    System.out.prinln("Values are above 10 and below 20");
} else {
   System.out.println("Values are NOT above 10 and below 20");

As an example, in the above code we have two ints “myInt” and “mySecondInt” and then in the if statement we check whether the value of myInt is greater than or equal to 10 AND the value of mySecondInt is less than or equal to 20. If it matches both of these rules for both of these variables then a line will be printed.

We have used two variables here and even checked both of them at the same time. This would not be possible with the switch statement.

int switchNumber = 3;

        switch(switchNumber) {
            case 1:
                System.out.println("Value was 1");

            case 2:
                System.out.println("Value was 2");

            case 3: case 4: case 5:
                System.out.println("Value was 3, 4, or 5.");

                System.out.println("Was not 5 or below");

The example we used when learning how switch statements function on the Udemy course is above. We have defined a variable named switchNumber which has been assigned the value of “3”.

We then use the switch statement in much the same way we might use the if statement, we write “switch” followed by a pair of (brackets) inside of which we put the name of the variable we want to test – remembering we can only use one variable at a time here. We then start the code block with curly braces { }.

At this point on we use the case keyword followed by the value we want to test for. So we have case 1 which is testing whether switchNumber is equal to one. If it is it will run whichever code you have set inside the case, in the example above it would print “Value was 1” and then break; which is a new statement that works with the switch statement. It helps to think of it as breaking free of the code – more on this in a second.

Continuing with the example above the value of swichNumber was set to 3 so in our switch test we know the value being 1 isn’t the case so we move on to see whether it is 2 instead, this is not the case, so we then check to see if it 3, 4, or 5 – which of course is the case so we now print the line Value was 3, 4 or 5. And then we break; 

As mentioned previously, break; is a statement that stops the current code, and makes the program jump ahead, to the end of the code block, saving the program from running the lines following the true value. So in our example after the code realises that the value of switchNumber is 3 it breaks free of the switch statement and will continue running the next part of the program – whatever that may be.

There are a few more quirks you may have noticed with the switch statement. When listing the cases, Case 1 and Case 2, are on seperate lines but then we have:

case 3: case 4: case 5:

Here, what is happening is we are checking whether switchNumber is 3, 4 or 5. It’s shorthand for having to write each case and the System.out.println code multiple times.

The final piece of the switch statement above is the ending, default, this is the same as how you end an if statement with else. In this case, we are saying that if switchNumber doesn’t meet any of the cases we have defined, then run this default piece of code.

Tip: You technically don’t need the break; line in the default case as the code would have moved on at this point anyway, but it’s good practice to writer break; still regardless.

So there we have the basics of switch statements, there are other quirks of switch statements that were mentioned but we didn’t go into too deeply, such as how in Java 7 the ability to use Strings with the switch statement was introduced – with a few caveats, such as them being case sensitive. There are built in methods that help solve this issue though such as .LowerCase() which converts the String variable to all lowercase and then you can write your cases to check the string using lowercase.

As you would expect, the official Java documentation has a great description of the Switch statement and how it works. You can see this by visiting the official Java documentation here.

This was just a brief introduction to Switch statements and I’m sure as the Udemy course goes on I will learn more about them and how to utilise them properly.

Last Post Button