Intro to programming: If statements

Published on: July 24, 2015

Tags: programming and intro-programming


Although computers live in a very predictable world of 1s and 0s they need to be able to handle uncertainties.

If a customer has enough money in his account then the ATM should give money out.

If a shirt is on sale then the shirt’s price must be updated

You deal with uncertainties by using the same kind of if statements as a computer every day.

If it is sunny then we will have a picnic.

You first determine that it’s sunny, then you know you will have a picnic.

If you are over 4ft tall then you can ride this roller coaster.

Once you know you know you are over 4ft tall you also know you are allowed to ride this roller coaster.

Each if statement will occur only under a certain condition (it’s sunny or you are over 4ft tall in the above examples). This condition will be a True or False statement. When the condition is True, the “then” section will occur. When the condition is False, the “then” section is ignored.

We might string many of these ifs together:

  • If it is sunny then we will have a picnic.
  • If it is raining then I will bring an umbrella.
  • If it is cold then I will wear a coat.

Let’s assume that it’s both sunny and cold. Using your “human” brain, reread the above.

  • Will you go on a picnic? Yes.
  • Will you bring an umbrella? No.
  • Will you wear a coat? Yes.

Lovely! You've got a picnic wearing a light jacket to look forward to!

Now look at the conditions in the examples above with your “computer” brain. Notice anything? These conditions are always a statement that is True or False. “It is sunny”, “it is raining”, and “it is cold” are all boolean expressions. So the computer can read these examples as:

  • If True then we will have a picnic.
  • If False (this is ignored since the condition evaluated to False)
  • If True then I will wear a coat.

Computers use these if statements to determine what should happen next. If statements control the flow of the program, some things happen (like having a picnic), while others are skipped over (like bringing an umbrella).


Ok, now it’s your turn. Answer the following questions for you at the current moment.

Think about your every day life, and come up with at least five more examples of when you use if statements to decide what to do.

Thanks for submitting your examples!

comments powered by Disqus