Hi everyone,

This is post #2 in a series of 5 posts about the 5 basic concepts of any programming language.  Here's a breakdown again of those concepts:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax
  5. Tools

We've already discussed what a variable is, so now let's talk about control structures.  What on earth is a control structure!?  Wiki describes it as follows:

A control structure is a block of programming that analyzes variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control “flows”). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters.

H'okay, so, that definition is obviously a bunch of technical terms that no beginner to programming would understand.  So let me try to describe it in more human terms.  When a program is running, the code is being read by the computer line by line (from top to bottom, and for the most part left to right), just like you would read a book.  This is known as the “code flow“, now as the code is being read from top to bottom, it may hit a point where it needs to make a decision, this decision could make the code jump to a completely different part of the program, or it could make it re-run a certain piece again, or just plain skip a bunch of code.  You could think of this process like if you were to read a choose your own adventure book, you get to page 4 of the book, and it says “if you want to do X, turn to page 14, if you want to do Y, turn to page 5″.  That decision that must be made by the reader is the same decision that the computer program must make, only the computer program has a strict set of rules to decide which direction to go (whereas if you were reading a book, it would be a subjective choice based on whomever is reading the book).  So, this decision that must be made, that will in turn effect the flow of code, is known as a control structure!

Okay, that doesn't seem to be such a hard concept… a control structure is just a decision that the computer makes.  So then that begs the question, what is it using to base that decision on?  Well, it's simply basing its decision on the variables that you give it!  Let me show you a simple example, here's a piece of Java code:

if (yourAge < 20 && yourAge > 12)
  // you are a teenager
  // you are NOT a teenager

So, you can see above that we have a variable, and its name is yourAge, and we are comparing yourAge to 20 and 12, if you're less than 20 AND you're more than 12, then you must be a teenager (because you are between 13 and 19 years of age).  What will happen inside of this control structure, is that if the value assigned to the yourAge variable is between 13 and 19, then the code will do whatever is inside of the first segment (between those first two curly braces { } ), and it will skip whatever is inside of the second code segment (the second set of curly braces { } ).  And if you are NOT a teenager, then it will skip the first segment of code and it will execute whatever is inside of the second segment of code.

Let's not worry too much about what the code looks like for the moment, as I'll touch on how to write the code out properly in section #4 syntax.  The only concept you need to try and wrap your head around right now, is that there is a way in programming to ‘choose' which lines of code to execute, and which lines of code to skip, and that will all depend on the state of the variables inside of your control structure.  When I say state of a variable, I just mean what value that variable has at any given moment, so if yourAge = 15, then the state of that variable is currently 15 (and thus, you're a teenager).

You've now seen one control structure and I've tried to explain it as best I could.  This control structure is known as an if...else structure.  This is a very common control structure in programming, let me hit you with some other examples.  Here's a while loop control structure:

while (yourAge < 18)
  // you are not an adult
  // so keep on growing up!

This while loop control structure is also very handy, its purpose is to execute code between those curly braces { } over and over and over until the condition becomes false.  Okay, so what's the condition?  Well, the condition is between the round brackets ( ), and in this example it checks yourAge to see if you are less than 18.  So if you are less than 18, it will continuously execute the code inside the curly braces { }.  Now, if you were 18 or older before the while loop control structure is reached by the code flow, then it won't execute ANY of the code inside of the curly braces { }.  It will just skip that code and continue on executing code below the while loop control structure.

The Best 7 Java Programming Resources

Alright, so if you're read this far down the article, then you're clearly interested in learning how to code. We actually have a free guide that outlines the best 7 resources for Java programmers.

When you sign up you'll receive the guide immediately and you'll also receive a few emails a week that will help you speed up your Java learning process so you can get up and running as fast as possible.

Go ahead an click the button below to get started!

CAREFUL! Don't Push This Button If You're Not Ready To FINALLY Learn How to Code

There are a few other examples of control structures in Java, but I don't want to overwhelm you with them right now.  So instead I'll sum up what we've learned today.

We've learned that code flows from top to bottom and for the most part left to right, just like a book.  We've learned that we can skip over certain code or execute certain parts of code over and over again, and this is all achieved by using different control structures.  These control structures are immensely important to programming, as they make the programs function properly.  For example, you wouldn't want people to be able to login to your Facebook account if they enter the wrong password right?  Well, that's why we use the if...else control structure, if the passwords match, then login, else show a "password is incorrect" screen.  So, without control structures, your program's code would only flow in one way, and it would essentially only do one thing over and over again, which wouldn't be very helpful.  Changing what the code does based on a variable is what makes programs useful to us all!

I hope I've cleared the mystery behind the term control structure, and I look forward to talking about our next subject, data structures!

Plenty of Ways to Learn

  • COMMUNITY - Learning with a community is the best way to ensure accountability and support when you need help. Coders' Campus is a brand new community that is dedicated to new programmers like you!
  • BLOGS - If you feel like you enjoy the way this information is laid out in blog format, then I invite you to click on the "next" button below.
  • BOOKS - If you feel like you are better at learning from a book, then I invite you to check out my ebook via http://javapdf.org. This eBook is organized, and packed full of easy to follow tutorials and videos.
  • PODCASTS - Finally, if you prefer to learn by just listening, then there's also a podcast available on iTunes via How to Program with Java Podcast.

Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.