9 - Control Structures in C
SECOND TERM: PROGRAMMING
Learning Outcome
(a) explain types of control structures: sequential, selection and
repetition;
(b) use suitable algorithm representations in constructing algorithms to
solve problems using control structures;
(c) explain the general format for each control structure;
(d) convert algorithms which use sequential control structure into C
statements;
(e) convert algorithms which use selection control structures: if,
if...else, nested if and switch...case into C statements;
Learning Outcome
(f) convert algorithms which use repetition control structures: for,
while and do...while into C statements;
(g) use break and continue statements to alter the flow of control;
(h) write a C program that uses control structures.
CONTROL STRUCTURES IN C
C programs are written in the linear fashion.
The linear programs have almost no logical comparison.
A realistic C program may require that a logical test is carried out at
some particular point within the program.
One of the several possible actions will then be carried out, depending
on the outcome of the logical test.
The outcome obtained by making comparisons and moving further
calculations is known as branching.
Logical statements are tested from several available groups of
statements within the program.
The program which forms repetitions for a certain number of time is
known as looping.
All these branching, selection, and looping are the fundamental
necessities of the program without which one cannot make decisions
and the program remains incomplete.
Structures use different kinds of signs and symbols for constructing the
control flow within the program.
The logical expressions, relational operations, assignment operators are
extensively used in the different program.
The common relational operators are: <, <=, >, >=, the equality
operators are: = = and !=.
The logical connectives are logical operators such as && (And), ||
(OR)
The conditional operator ? : is also used in the expression in the
program.
There are three different kinds of statements in C; expression
statements, compound statements and control statements.
An expression statement consists of an expression followed by the
semicolon. A compound statement consists of a sequence of two or
more consecutive statements enclosed in braces ({ and }).
The enclosed statements can be expression statements, other compound
statements or control statements.
Most control statements contain expression statements or compound
statements including embedded compound statements.
The three basic types of control structures are:
sequential,
selection, and
repetition.
They can be combined in any way to solve a specified problem.
Sequential
Sequential is the default control structure, statements are executed
line by line in the order in which they appear.
The selection structure is used to test a condition.
A sequence of statements is executed depending on whether or not the
condition it true or false.
This means the program chooses between two or more alternative
paths
Condition refers to any expression or value that returns a Boolean
value, meaning true or false.
Selection
The three main types of selection statements are if, if… else and
switch statements.
The most basic and common is the if statement. The if and if…
else statements can be nested.
Switch statements are ideally used when there are multiple cases to
choose from.
Repetition
The repetition or iteration structure repeatedly executes a series of
statements as long as the condition is true.
The condition may be predefined or open-ended. while, do…
while and for loop are the three types of repetition statements.
A loop can either be event controlled or counter controlled.
An event-controlled loop executes a sequence of statements till and
event occurs while a counter-controlled loop executes the statements a
predetermined number of times.