1.03 Conditions and loops

Aim: To look at some types of conditions and loops and how they can be used.

Topics covered:

  • What is a condition and what is it for?
  • What is a loop?
  • What kind of loops are there?
  • How can they be used?

Code flow

Sometimes we are happy with a simple flow from the top of a script to the bottom and the machine will execute each statement in turn. However, this is often not good enough for anything but the most basic of scripts.

We can change the flow using conditions (e.g. if and switch-case statements), loops (e.g. while and for loops) and functions. There are a few other ways to alter the flow of execution, (e.g. Exceptions, events, and statements such as break, continue and return) but these will be discussed further in future articles.

Conditions define blocks of code that will only be executed if certain conditions are met.

Loops define a block of code that will be executed repeatedly while a condition is being met.

Functions will be discussed in the next article.

 The ‘If’ statement

‘If’ statements are sometimes called ‘If-Then’ or ‘If-Then-Else’ statements. They generally look something like this…

Informal pseudo-code

If condition Then
    // Perform main code block
Else
    // Perform else code block
End If

The condition is used to determine whether or not to go into the main code block.

The main code block is entered and executed if the machine considers the condition to be true.

The (optional) else code block is entered and executed if the machine considers the condition to be false.

If multiple conditions need to be checked then a lot of languages have optional ElseIf blocks…

Informal pseudo-code

If condition Then
    // Perform main code block
ElseIf conditionB Then
    // Perform conditionB code block
ElseIf conditionC Then
    // Perform conditionC code block
Else
    // Perform else code block
End If

For languages that do not support ElseIf, another ‘If’ statement could be placed in the else code block to provide the same functionality. In Java for example, this is done by including a space between the ‘else’ and the ‘if’.

Most languages also provide an inline ‘If’ expression, but we will not discuss that at this point.

The ‘Switch/Select Case’ statement

When we want to execute one of a number of different code blocks depending on the value of a variable, we can use a switch-case statement…

Informal pseudo-code

Switch var
    Case 1:
        // perform code block 1
        Break
    Case 2:
        // perform code block 2
        Break
    Case 3:
        // perform code block 3
        Break
    Default:
        // perform default code block
End Switch

Here we are checking the variable var. If it contains the value 1 then code block 1 will be executed. If it contains the value 2 then code block 2 will be executed. If it contains the value 3 then code block 3 will be executed. If it contains a value that isn’t 1, 2 or 3 then the default code block will be executed.

This could be done with an if statement that looks like the following…

Informal pseudo-code

If var == 1 Then
    // perform code block 1
ElseIf var == 2 Then
    // perform code block 2
ElseIf var == 3 Then
    // perform code block 3
Else
    // perform default code block
End If

… however the switch-case is often easier to read.

A point worth mentioning is that many languages have different versions of the switch-case statement.

For example instead of ‘Switch’ Visual Basic (including VBA and VB.net) uses the word ‘Select’.

Another example is that some languages require the word ‘Break’ (or similar) at the end of each case block to prevent execution falling through to the next case block. Whereas other languages assume this implicitly

The ‘While’ loop

When the same code block needs to be executed multiple times we use a loop. The ‘While’ and ‘For’ loops are the most common of these.

The ‘While’ loop executes the same block of code repeatedly until its condition is no longer met. The condition is checked at the end of each iteration (repeat) of the code block…

Informal pseudo-code

While condition
    // main code block
While End

While loops can be dangerous if thought is not put into the condition. There is potential for it to continue executing “forever” if the result of the condition does not change.

In the standard ‘While’ loop its code block will not be executed at all if its condition is not met the first time it is encountered. If we always want to run the code block at least once, then we can use the Do-While loop. Most languages have a version of this…

Informal pseudo-code

Do
    // Main code block
While condition

The ‘For’ loop

If we want to repeat the same block of code a specific number of times, then we can use the ‘For’ loop…

Informal pseudo-code

For i = 1 to 10 step 1
    // perform main code block
End For

The ‘For’ loop generally has an initial assignment, a condition and a value modification clause. In the pseudo-code example these are as follows:

  • Initial assignment: i = 1
  • Condition: i cannot be greater than 10
  • Value modification: i will increase by 1 on each iteration (its incremental step is 1)

The same effect could be achieved with a ‘While’ loop with the same condition, and using extra statements for the initial assignment and the value modification.

One reason to use a for loop might be to execute a code block for each entry in a list. Most languages have a variation on the ‘For’ loop to make this easier called a ‘For Each’ loop…

Informal pseudo-code

For Each entry In list
    // perform main code block
End For Each

In this example ‘list’ is a variable that contains list of objects, and ‘entry’ is a variable that contains the object that we are currently up to.

For example we could use this to print a list of names to the screen as follows…

Informal pseudo-code

For Each name In nameList
    print name
End For Each

Summary of conditions and loops

We have got through a lot in this article so to summarise I will repeat the definitions from the start:

  • Conditions define blocks of code that will only be executed if certain conditions are met.
  • Loops define a block of code that will be executed repeatedly while a condition is being met.

Feedback on 1.03 Conditions and loops

If you have any questions or suggestions relating to this article or any other then please fill in our feedback form and let us know what you think.