Diesen Artikel auf Deutsch lesen

Programming Terminology: Control structure

What is a control structure and how can it be used when you are writing programs? Read on to find out.

Control structures are specific commands in imperative programming languages which are used to control the logical flow of the program during its execution. These commands control which part of the program the computer will execute next.

There are two main types of control structures:

Both kinds of control structures help to avoid the classic jump commands like "GOTO" which were common in earlier programming languages. These often produced programs which were difficult to understand and to debug.

Branching control structures

With branching control structures code contained inside the control structure is either executed or not, based on the calculation of a condition.

There are two common types of branching control structures:

if-then-else branching control structures

In if-then-else control structures, code is split up into two parts, unless the "else" part is not necessary for the program. The condition term usually follows the "if" statement and is calculated only once, right at the start of the control structure.

The syntax varies in different programming languages, but the main principle is the same:

  1. "IF"-command: in the "if" statement, the condition directly after the statement is checked and its boolean value is calculated. If the value of the condition equals "true", then the following lines of code are executed, up to the start of the "ELSE"-branch or the "END"-statement.
  2. "ELSE"-branch: in most programming languages, the "ELSE" branch for an "IF" command is optional. If it is provided in the program code, the command lines after "ELSE" and up to the "END" of the "IF" command are executed only if the boolean value of the condition after the initial "IF" command is calculated to be "false".
  3. "END" statement: the "IF" control command structure is ended by an "END" command statement or an equivalent closing statement. After that, the program is executed sequentially, command line after command line.

As if-then-else structures can be part of the "if" or "else" branch of a surrounding if-then-else control structure in most programming languages, this can be used to create a complex logical structure. Due to the basic binary "true"/"false"-structure of these commands, the logic is still easy enough to follow.

Here is a short example for an "if" control structure in the C programming language:

 CODE: Select all lines of code for copying

if (x < 20)
  CalculateY();
else
  CalculateZ();


In this example, the function "CalculateY" is executed if the value of the variable "X" is below 20. If "X" is equal or greater than 20, the function "CalculateZ" is executed instead.

Case control structures

Case control structures also use conditions in order to decide which part of the program has to be executed next, similar to if-then-else structures. The main difference lies in when and where the boolean value of the condition is calculated.

In addition to this difference, case structures allow for the execution of different parts of code based on several possible values of a variable or a calculation.

The common syntax for case control structures looks similar to this in most programming languagues:


CASE (variable or calculation)
  WHEN value_1:
    command 1
    command 2
    ...
  WHEN value_2:
    command 3
    ...
  WHEN OTHERS:
    ...
ENDCASE


The variable or calculation after the initial "CASE" command provides a value that is used as a condition inside the case control structure. The value can be numeric, boolean, a single character or a string.

Inside the case structure, individual possible values are listed and act as references. If the value of the initial variable or calculation is equal to one of the reference values, the comparison in the "WHEN" statements equals "true", causing the computer to execute the code that is listed as part of the specific "WHEN" branch.

Here is a short example for a case control structure in the C programming language:

 CODE: Select all lines of code for copying

switch (cval) {
   case 0: v = 1; break; 
   case 1: v = 2; break; 
   case 2: v = 5; break;
   default: v = 10;      
}

In this example, the value of the variable "v" is set to one of the values 1, 2, 5 or 10 depending on the value of the selection variable "cval". If "cval" has a value other than 0, 1 or 2 (no matter which), the value of "v" is set to 10 as default.

Loop control structures

While code in a branch control structure can be left out of execution during runtime if the condition is false, code in a loop control structure is usually executed one or several times over.

Most programming languages know three variations of loop control structures:

While most programming languages also contain special loop exit commands which can force the program to exit from the loop before the default exit condition is met, such exits/breaks are usually avoided if possible, because the program is more difficult to analyze when exits are used.

For-loops

"for"-loops or counter loops use a counter variable and a condition in order to determine how often the code inside the loop has to be executed.

The counter variable is usually an integer variable that is counted up or down in increments or decrements of one until the end condition is true

Here is an example of a "for"-loop in the BASIC programming language

 CODE: Select all lines of code for copying

FOR i=2 TO 18 STEP 2
  ' do something
  ' do more things
  ' ...
NEXT i

In the above example, the variable "i" is an integer value which is increased in the "for"-loop from a starting value of 2 up to a maximum value of 18. The additional "STEP" statement and the value after it increase the counting steps from 1 to 2, so "i" is increased by 2 after each run through the loop: 2, 4, 6, 8 etc. up to 18.

All lines of code inside the loop are executed again and again until the defined maximum value of the counter is reached. After that, the execution of the program continues for the code after the end of the loop.

In some programming languages like C, C++ or Java the exit condition for the loop can be a more complex condition. But the mechanism is the same as described above: the code inside the loop is executed line after line one or more times, until the condition changes from a boolean value of "true" to "false".

There is also a variation of the "for"-loop in some languages, the "foreach"-loop. Instead of using an integer counter to count the repetitions of the loop, this uses a defined set of values. These values can be numbers, characters, strings or even whole data sets where the set is effectively a small internal table made up of several different values. Instead of counting up or down, the loop sequentially runs through all values in the set until the last one has been reached.

Here is an example from the Perl language:

 CODE: Select all lines of code for copying

foreach $name ("Anne", "Tom", "Peter") {
  print("Hello, $name.\n");
}

In this example, the string variable "$name" acts as a counter. It is given the values listed in the set (from "Anne" to "Peter"), starting with the first one, until the end of the set has been reached. Inside the loop, the print command prints out a greeting for the current name value.

While-loops

Similar to "for"-loops, the code inside "while"-loops is executed again and again until a certain condition changes from true to false.

Unlike "for"-loops, the condition is not dependent on an (integer) count value. It can be any kind of condition containing variables, function values etc. which can change during the execution of the code inside the loop.

Here is a short example (using a classic version of the BASIC language):

 CODE: Select all lines of code for copying

X = 2
WHILE ( X*X < 100 )
  PRINT X, X*X
  X = X + 3
WEND

In this example, the variable "X" starts at the value of 2. In each run though the while loop, the current value as well as the calculated value of "X" times "X" is printed and "X" is increased by 3. If the value of "X" times "X" gets bigger than or equal to 100, the loop is not executed any longer.

Repeat-loops

"repeat"-loops or "do"-/"do-while"-loops are similar to "while"-loops. The difference is that the evaluation of the exit condition happens at the end of the loop, after the first execution of the code inside the loop, whereas in while loops the condition is checked at the start of the loop, before the first run through the loop.

Let's take the example from the while loop above and change it into a repeat loop:

 CODE: Select all lines of code for copying

X = 2
REPEAT
  PRINT X, X*X
  X = X + 3
UNTIL ( X*X < 100 )

At first glance, the code looks similar to the while loop. This repeat loop also stops to execute the code inside the loop when "X" times "X" is no longer smaller than 100.

There is one difference however: the code for increasing X by 3 is executed once before the condition "X*X < 100" is checked the first time. In the while loop example, the condition check occurs before the value of X is increased.

In the examples used here, the condition effectively produces the same loop behavior. But because of the different points within the flow of the program where the condition is checked, the number of times that each loop is executed is different. The "repeat"-loop is not executed as often as the "while"-loop.

Everything under control

Control structures make up an important part of programming. In combination with functions, objects, variables etc. they are essential to create the logic flow of a program.

This effectively lends some intelligent behavior to the program (if done right) and helps the computer to make decisions.


Enjoy this page? Please pay it forward. Here's how...

Would you prefer to share this page with others by linking to it?

  1. Click on the HTML link code below.
  2. Copy and paste it, adding a note of your own, into your blog, a Web page, forums, a blog comment, your Facebook account, or anywhere that someone would find this page valuable.