0% found this document useful (0 votes)
9 views6 pages

Learning Outocme D1

This document introduces control structures in programming, defining them as structures that control the flow of a program through decision-making and repetition. It outlines two main types: selection control structures, which make decisions based on conditions, and iteration control structures, which execute instructions repeatedly based on conditions. The document also discusses relational and Boolean operators used to formulate conditions for these structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Learning Outocme D1

This document introduces control structures in programming, defining them as structures that control the flow of a program through decision-making and repetition. It outlines two main types: selection control structures, which make decisions based on conditions, and iteration control structures, which execute instructions repeatedly based on conditions. The document also discusses relational and Boolean operators used to formulate conditions for these structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Principles of Programming IA

(PPA115D/TRO115D)

Introduction to Control Structures

Compiled by
V. Booi
and
V. Memani
Learning outcome: D.1. Introduction to control structures
The purpose of this chapter is to introduce the student to control structures. We will
define control structures, look at the different types, and also talk about conditional
statements.

1.1 Control structures

A control structure is a programming structure that controls the flow of a program.


In life we are constantly faced with situations which require us to make decisions, and
based on the decisions, we perform certain tasks. Some of the tasks might have to be
repeated several times.

We make decisions on many things, we decide when to sleep, when and what to eat,
how long to watch TV, and so on. So life is about making decisions and repetition, and
so is programming.

In programming we write programs that make decisions and repeat the execution of
certain instructions. In order to do this we use control structures. Control structures
give our programs the ability to make decisions and repeat tasks. There are two kinds
of control structures used in programming, and they are:
 Selection control structures; and
 Iteration control structures.

We will discuss in detail each of the control structures in the next sections.
1.2 Selection control structures

Selection control structures are programming structures that are used to make
decisions or choices. They allow programs to make decisions as to which instructions
must be executed over others. The selection is based on the evaluation outcome of a
condition. A condition is tested and the next move is dependent on whether the
condition evaluates to a true or false. We mainly use relational operators to construct
conditions. If there are more than two conditions to be constructed, we use Boolean
operators to combine the conditions.

Selection control structures come in various types. The following statements are
examples of selection control structures:
 if statement;
 if…else statement;
 if …else if …else statement;
 Nested if statements; and the
 switch statement.

In chapter the next chapter we discuss in detail each of the statements. Let us have a
look at the generic representation of selection control structures using the flowchart.

1.2.1 Flowchart representation of selection control structures

Figure 1 below shows the generic flowchart representation of selection control


structures.

Figure 1: Flowchart representation of selection control structures


As can be seen from Figure 1, we have two conditional blocks of code that needs to
be executed. Block A statements are executed when the tested condition evaluates to
TRUE, and block B statements are executed only when the tested condition evaluates
to FALSE. So it’s to separate paths. It’s also important to note that the condition is
tested only once and thereafter the block of code that meets the condition gets
executed. There’s no repetition, the program continues executing after encountering
this selection control structure.

1.3 Iteration control structures

Iteration control structures are used to execute a set of instructions repeatedly


depending on the evaluation outcome of a condition. They are also known as loops.

We have three types of iteration control structures, and they are the:
 for loop;
 while loop; and
 do…while loop.

These specific types of iteration control structures will be studied in detail in chapter
eight. Let us look at the generic representation of iteration control structures using a
flowchart.

1.3.1 Flowchart representation of iteration control structures.

Figure 2 below shows the flowchart representation of iteration control structures.


Figure 2: Generic representation of iteration control structures

We can see from Figure 2 that the loop will continue working as long the condition
tests TRUE. Should the condition test FALSE, the loop will terminate or come to an
end. The statements that we want to execute repeatedly (block A) must be kept inside
the loop, that is, they must become body statements of the loop. Those we want to
execute after the loop has terminated (block B), we keep outside of the loop.

1.4 Conditions

For selection and iteration control structures we use conditions to determine what must
happen next. If the condition evaluates to TRUE we do something different compared
to when the outcome is FALSE.

We use relational operators to formulate conditions. Some of the relational operators


in use are:
 > (is greater than)
 < (is less than)
 >= (is greater than OR equals to)
 <= (is less than OR equals to)
 == (is equals to)
In case we have multiple conditions to test, we use Boolean operators to join them.
Below are the Boolean operators we use:
 && (AND)
 || (OR)

There’s also the unary Boolean operator, ! (NOT). It acts on a single operand. As said
in chapter 3, it returns the opposite of the operand.

1.5 Conclusion

In this chapter we managed to present the theoretical background of control structures.


We defined control structures, looked broadly at the different types of control
structures.

In the next two chapters we are going to look at the specific types of control structures.
Thank you very much for having taken time to go through this chapter. Enjoy the rest
of the day and God bless you.

You might also like