Control Statements Overview
Control Statements Overview
1h 42m

This course explores the fundamental concepts and syntax associated with Control Flow and showcases these with some real-life projects.

Learning Objectives

  • Learn about the three categories of control statements: sequential, selection, and repetition
  • Manipulate control statements using the continue and break statements
  • Learn about pseudo-random numbers and the Random class 

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java


This is a beginner-level course and can be taken by anyone with an interest in learning about Java.



Control statements are statements involved with the flow of control or simply control flow of a program. The control flow just refers to the order in which instructions are executed within a program. We can use such statements to produce incredibly exciting and sophisticated applications. Before diving deep into control statements in subsequent lectures, we're going to take a brief bird's-eye view of the control statements.

In this lecture, we will look at syntax that might look a little bit foreign to you at the moment, but it's for the purpose of exposing you to some of the constructs and seeing them in action. I'll explain these in much more detail later. So, don't worry if you don't understand what's going on yet. Let's first create a Section3-Projects project inside of IntelliJ. So, I will click 'New Project' and 'Next', 'Next', and make sure it's in a good location that I like and it looks okay to me. Section3-Projects. There we go, and hit 'Finish'. All ready. Then I'll expand this and in this source directory right here, we're going to create a file called ControlStatementsIntro. Right click 'New', 'Java Class', and then ControlStatementsIntro. Notice there's no hyphens, no spaces, no underscores, none of that. Okay, here we go.

So, before we do some coding let me go over something briefly. There are three primary categories of control statements in Java. They are sequential or sequence control statements, selection control statements, also sometimes called decision statements, and repetition control statements, sometimes just referred to as loops. Sequential or sequence control statements are the default. These statements are executed in the order that they're written one right after the other. This is what we've been using since the beginning. For example, if we declare an age variable and set it to 15 and then print it out, that is done in sequence. So, let me write public static void main. There we go. 

And let's do this int age = 15 and then I can do this, System.out.println(age). Pretty cool. So, let's run this, right click, 'Run'. And of course, it prints out 15 as we expected. So, that's pretty straightforward and predictable, isn't it? Now, what if we wanted to make a decision based on the age? Say if the users at least 16, they can drive here in the United States. So, let's see what that might look like. So, we're going to write a test to see if age is greater than 16 and then print out either you can drive or you can't drive yet. So, I'm going to get rid of that statement. I am going to put, this is a technique I use here, I'm going to put end main there, you don't have to but that's just a little comment there to help me line up the curly braces again. So, if (age>=16), there's our relational operators. I put a set of curly braces, else's, if that other thing isn't true, then this will get executed, and up in here I will print out System.out.println. I'll put "You can drive". Okay. Or you can get your license or to be more correct. Well we'll just leave it as is. "You cannot drive yet" or you can't get your license yet. 

That would be another thing you could put. So again, do you remember the relational operators from the last section? The >= operator is one of them. Before we were just putting out Boolean values that resulted from the relational operations, but now we're using them to make decisions. This if statement expects in between parentheses to have some sort of Boolean value here. And ultimately this resolves to a Boolean value because it's a relational operation. So, we're all set with that. So, let's run this and see what happens. I'm going to run, actually run that, right there. And currently it says "You cannot drive yet" because we have age set to 15 that does not satisfy this condition. So, it goes to the else section down here. Now, what about we'll say 19? Rerun it and there you go. You can drive. Pretty awesome, right? So, it chose only one of the options and ignored the other. 

So, see how even though the code for being 16 or older is in one block of code that that block can be skipped entirely, breaking the normal or sequential sequence flow of control. As a final quick look at the last category, remember there's sequence or sequential, selection, and then repetition. We're going to look at the repetition control statements or the loops. I'm just going to print out the numbers one up to and including the age with happy birthday in front of each of them. So, after this little test right here, I'm going to put a regular old println just to add some new line space. And then we're going to put, let me see here. We'll do that. So we'll say int i = 1 because you don't really, we're not really starting at zero here. And all right. Then we will put that and sometimes I do something like this and even up here, I might even do end if-else, but you again don't have to do this. Most developers would say don't do that. It's just one of the things I do, especially for teaching, but I found it actually helps me when I write my own code too because then I can very easily keep track of where the curly braces are no matter which environment I'm in. So, let's put System.out.println and we will say "Happy Birthday", and we're going to actually put a space here and can concatenate it with the number right there from one and then it goes up to and including the age that was typed in up here.

So, let's run this and see what happens. Run it... and there we go, right there. So, this repetition control statement here is called a 'for loop' and it repeats from one up to including the age, printing the same statement essentially over and over again but with a different age, the integer each time. So, every time it goes through this, it starts with the one and then it increments or goes up by one each loop or each iteration and it keeps going until this is no longer satisfied, the loop continuation condition. Pretty cool. So, this is all very exciting. However, it's just a brief taste of what's possible. Before we move on to the next lecture, I want to challenge you to simply say out loud the three categories of control statements again to make sure it stays fresh in your brain. So, don't look back just yet, pause the video, and see if you can remember all three. You don't have to fully remember what they do, just remember their names for now. Did you remember the three primary categories of control statements?

Here they are in case you need a little reminder. Sequential or sequence, selection, and repetition. Awesome work. Let's move on to the next lecture in which we will dive a little deeper into decision-making with selection-control statements. I'll see you there.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics