The 5 Basic Concepts of any Programming Language – Concept #2

by Trevor Page on July 1, 2012

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
}
else
{
  // 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.

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.
  • VIDEOS – If you feel like you prefer learning by watching Videos, then check out the Java Video Tutorials website. It’s full of fun and easy to follow video tutorials. So far the students enrolled in this video course have rated it 9.5/10!
  • 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.




{ 24 comments… read them below or add one }

Tatia July 10, 2012 at 3:31 pm

So far very interesting and easy to understand.

Reply

Rene Besana July 13, 2012 at 1:28 am

while it is true that the computer reads your code top to bottom fashion, it is not true that it always reads it left to right. when it comes to an assignment statement it reads from right to left, i.e., right to left of the equal sign (assignment operator) because it has first to determine the rvalue which it assigns to the lvalue.

Reply

Trevor Page July 13, 2012 at 1:31 am

You are absolutely correct Rene, I will update my post, thank you for the clarification :)

Reply

Jeffery September 29, 2012 at 11:03 am

When I initially left a comment I appear to have clicked the -Notify
me when new comments are added- checkbox and from now on each
time a comment is added I get four emails with the same comment.
There has to be a means you are able to remove me from that service?

Kudos!

Reply

Trevor Page September 29, 2012 at 6:57 pm
joe December 18, 2012 at 9:11 pm

True programming beginner right here! And I’d say that this is one of the easiest to understand tutorials out there. Keep it up man.

Reply

Trevor Page December 18, 2012 at 11:13 pm

Thanks Joe

Reply

Ashish January 4, 2013 at 2:50 am

Nice work buddy Ur posts are very helpfull….

Thanks…Tc

Reply

Trevor Page January 4, 2013 at 9:54 am

I aim to please :)

Reply

yatendra January 6, 2013 at 1:42 am

really helpful …thnx bro!!

Reply

Henry Ratemo January 18, 2013 at 3:02 am

Hi, i am so grateful for your effort to enlighten me on the fields that i had little knowledge on kudos and together we can go far……….more lessons and more especially new adventures in the programming field with JAVA

Reply

Durim January 23, 2013 at 5:21 am

Thanks Travor!

You are so cool and your explanation is GREAT and VERY CLEAN!!

Reply

Kabiru February 18, 2013 at 11:43 am

Hi, this is very interesting i now start learning java very easy pls., continues with nice work
thanks

Reply

Isis April 16, 2013 at 5:43 pm

Hi Trevor,

thanks a lot for your information and hard work. In your opinion, do you think a person could work as a programmer if they only know Java? Should an individual learn, for example, 5 languages before trying to work as a computer programmer or software/network developer? Thanks for your input!!

Reply

Trevor Page April 17, 2013 at 10:20 am

I wouldn’t say that you would need to know 5 different languages, but if you’re relying on mostly Java knowledge, then I would also recommend learning SQL for use with databases. If you want to go one step further, then understanding the “front end” technologies will give you a leg up as well… these “front end” technologies would include HTML/CSS, JavaScript/jQuery and some sort of MVC framework for Java (like Spring or Struts).

Reply

Jonathan October 19, 2013 at 9:39 pm

New to your site, and finding it very helpful so far!
Will you cover integrating Java with these other technologies?
Thanks!

Reply

mohamed January 13, 2014 at 6:32 pm

this is what I’m looking 4

Reply

vamsi February 11, 2014 at 10:14 pm

I lyk da way u explain things

Reply

Raed May 29, 2014 at 3:42 pm

Hello Trevor,
i am a java programer from Iraq and i hear your Podcast and i like
the way how do you explain things about java and sql wonderful excellent
and wish the best for you take care.

best Regards
Raed

Reply

Trevor Page May 30, 2014 at 10:05 am

Thanks Raed, I really appreciate you taking the time to leave that comment

Reply

Raed June 6, 2014 at 8:40 am

Hello Trevor,
i think i found a mistake about SQL Inner Join the Query

select * from author
inner join book on
author.author_id = book.book_id; <—– mistake

shoul be

select * from author
inner join book on author.author_id = book.author_id;
because the FK is author_id in Book-Table

best Regards

Raed

Reply

Trevor Page June 6, 2014 at 11:33 am

DOH!

Nice catch Raed… this is why I should copy/paste my SQL code directly from my TOAD for MySQL. When I write it out manually, there’s room for human error. In any case, I’ve updated the code in the post. Thanks for finding that :)

Reply

Raed June 6, 2014 at 2:39 pm

Hi Trevor,

thanks alot you are willcome, it’s happen.
accept my best wishes

Reply

Evan July 1, 2014 at 8:47 am

so far so good! i went to a camp to learn java and got absolutely nothing from it. here, i can actually understand what your saying, its been very helpful. thanks so much!

Reply

Leave a Comment

Powered by sweet Captcha

{ 1 trackback }

Previous post:

Next post: