Lesson 9.1: Conditional Statements – The Basics
In our last lesson we took a look at the basic types of arithmetic available in most programming languages. In this lesson, we’re going to take a look at using conditional statements which will allow us to give our programs something more than a straight-down list of number crunching.
What are Conditional Statements?
Conditional statements are statements which control the flow of code based on conditions. In less convoluted terms, it’s how you control your code based on what’s going on in your program. They are also sometimes called “control statements” or “control structures”.
All conditions used by conditional state are boolean (true/false) in nature, though this isn’t always apparent at first glance. It will evaluate the condition, and if it is true it will perform an action. If it is false, it will not perform the action (though it may continue down a list of conditional statements and perform another action based on the same condition).
There are two types of conditional statements: branching statements and loops.
Branching Statements
Branching statements are named so because they “branch” based on the condition. The types included in this category are if, if…else, and switch statements. All of these will look at the condition and perform a single action (which is sometimes nothing) based on that condition.
Loops
Loops will “loop” through the code (meaning it will perform a section of code, then loop back to the top of the code and perform it again) while a condition is true. The foundation of many programs that run constantly (which is basically every program other than simple web pages) is a loop. The common types of loops are for, while, and do…while. Many languages also implement foreach and do…until conditional statements as well.
In the next section we’ll take a look at actually utilizing each of the branching conditions. After that, the next lesson will be all about loops.
If you haven’t yet, be sure to take a look at the arithmetic lessons, especially Boolean Arithmetic. It will benefit you when we start talking about making use of the Boolean arithmetic in our conditional statements.
Next Lesson: Lesson 9.2: Conditional Statements – Branching Conditionals




