Programming Fundamentals: Conditions
The first article in this series of posts talked about how programs consist of instructions written in programming languages. When we write instructions, we are telling the computer what to do, and the computer executes these instructions in the order that we write them. Our programs will be rather basic though if we don’t allow for branching, the ability to execute certain instructions based on some condition.
A conditional statement is an instruction that allows a computer to decide what to do based on a condition. A condition is anything that can be resolved to either being true or false. Many programming languages include a data type known as Boolean value that is used to store a true or false value. Any data type can usually be resolved to be either true or false, with a value of 0 representing false, and anything else equating to true.
It is not just values that are evaluated, we can also use mathematical expressions as conditions, such as checking x > 5, a + b = 6. We are also not restricted to evaluating a single condition, with the use of Boolean logic we can create complex conditions using AND, OR, and use NOT to allow a condition to be checked against something not being true. AND and OR allow us to combine multiple conditions, using the examples previously described we could write a conditional statement that states that x > 5 AND a + b = 6, which means that both x must be greater than 5 and the variables a and b must equal 6 for the condition to be true. OR on the other hand requires that only one of the conditions, x > 5, OR a + b = 6 equate to true for the whole condition to be considered true.
A programming language can be looked at in the same way as any spoken language, we must learn the words and rules that govern the language, the syntax, to use it. Each programming language will have a set of reserved words and define a structure we must follow for the computer to be able to understand the instructions we write. A conditional statement is an important concept, and is thus implemented into all languages, otherwise there would be no branching. Most languages implement a conditional statement using the word if. A conditional statement will look something like the following:
If (condition) then
Instructions to do something
In the above case, we would test a condition and then proceed to execute some code. Additionally, another reserved word else, is associated with conditional statements.
If (condition) then
Instructions to do something
Else
Instructions to do something else
The inclusion of the word else means that we have the option to branch out and execute instructions dependent on the evaluation of the condition. In the first example, instructions would be executed, if and only if the condition evaluated to true. In this second example, the computer gains the ability to execute instructions if the condition does not evaluate to true. A third example shown below, makes use of the words else if.
If (condition) then
Instructions to do something
Else if (condition) then
Instructions to do something else
The key difference between the 2nd and 3rd examples is that instructions in the 3rd example are dependent again upon some condition evaluating to true. In the 2nd example the instructions after the else word are executed every time the if condition evaluates to false. In the 3rd example though, there is chance that both instructions in the if and else if sections aren’t executed if both conditions evaluate to false. Also, there are no restrictions upon the amount of else if conditions you can use. By this I mean we could have code that looked like the following:
If (condition) then
Instructions to do something
Else if (condition) then
Instructions to do something else
Else if (condition) then
Instructions to do something else
Else if (condition) then
Instructions to do something else
…
Although we can use as many else if statements as we want, we cannot use else repeatedly, as the computer would be unable to determine which else’s section of instructions to execute.
Therefore, conditions give us incredible opportunities to write more complex code by allowing us to execute instructions based on conditions defined by us whether that’s checking the value of a variable, or evaluating a mathematical expression. We can control the flow of execution of our program, and ensure instructions are only executed when we want them to be. Conditions are not strictly limited to use in conditional statements, such as if statements, and play a big role in the ability to repeat the execution of an instruction, or set of instructions. The next article will expand the use of conditions, by describing their use in relation to repetition.