Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

IT2009

Statement-Level Control Structures


Basics
 Control statements allow selection among alternative control flow paths and repeated execution of
statements or sequences of statements.
 A control structure is a control statement and the collection of statement whose execution it controls.
 Categories of control statements: selection, multiple selection, iterative, and unconditional branching.

Selection Statements
 A selection statement provides the means of choosing between two (2) or more execution paths in
a program.
 Two (2) categories of selection statements: two-way and n-way (multiple selection)
 The general form of a two-way selector is:
if control_expression
then clause
else clause
 C-based languages use braces (curly brackets) to form compound statements.
if (sum == 0) {
if (count == 0)
result = 0;
}
else
result = 1;
 The multiple selection statement allows the selection of one of any number of statements or
statement groups.
 The general form of a switch statement in C, C++, Java, and JavaScript is:
switch (expression) {
case constant_expression_1: statement_1;
...
case constant_expression_n: statement_n;
[default: statement_n+1]
}
 The default clause is for unrepresented values; if there is no default, the whole statement
does nothing.
 The switch statement rule in C# disallows the implicit execution of more than one (1) segment.
The rule is that every selectable segment must end with an explicit unconditional branch statement,
either a break, which transfers control out of the switch construct, or a goto, which can transfer
control to one of the selectable segments.
 Example of a switch statement in C#:
switch (value) {
case -1:
negativeNum++;
break;
case 0:
zeros;
goto case 1;
case 1:
positiveNum++;
default:
Console.WriteLine(“Error in switch \n”);
}

07 Handout *Property of
STI
IT2009

Iterative Statements
 An iterative statement is one that causes a statement or collection of statements to be executed
zero, one, or more time(s). It is often called a loop.
 The body of a loop is the collection of statements whose execution is controlled by the iteration
statement. When the test for loop completion occurs before the loop body is executed, it is referred
to as pretest. If it occurs after the loop body is executed, it is called posttest.
 A counting iterative control statement has a variable called the loop variable, in which the count
value is maintained. It also includes some means of specifying the initial and terminal values of the
loop variable, and the difference between sequential loop variables, called the stepsize.
 The initial, terminal, and stepsize specifications of a loop are called the loop parameters.
 The general form of the for statement in Ada is:
for variable in [reverse] discrete_range loop
...
end loop;
 The general form of the for statement in C is:
for (expression_1; expression_2; expression_3)
loop body
 Example of a for statement in C:
for (count = 1; count <= 10; count++)
...
 The general form of the for statement in Python is:
for loop_variable in object:
- loop body
[else:
- else clause]
 The repetition control of a logically controlled loop is based on a Boolean expression.
 The C-based programming languages include both pretest and posttest logically controlled loops.
 The pretest logical loop has the following form:
while (control_expression)
loop body
 The posttest logical loop has the following form:
do
loop body
while (control_expression);
 Example of the pretest logical loop in C#:
int n = 1;
while (n < 6)
{
Console.WriteLine(“Current value of n is {0}”, n);
n++;
}
 Example of the posttest logical loop in C#:
int x = 0;
do
{
Console.WriteLine(x);
x++;
}
while (x < 5);
 User-located control loops allow programmers to choose a location for loop control other than the
top or bottom of the loop body.
 C, C++, C#, Python, and Ruby have unconditional unlabeled exits (break). Java and Perl have
unconditional labeled exits (break in Java, last in Perl).

07 Handout *Property of
STI
IT2009

 Example of nested loops in Java:


outerLoop:
for (row = 0; row < numRows; row++)
for (col = 0; col < numCols; col++) {
sum += mat[row][col];
if (sum > 1000.0)
break outerLoop;
}
 C, C++, and Python include an unlabeled control statement, continue, that is used to skip the rest
of the loop statements on the current iteration without terminating the loop structure.
 Example of the use of continue in loop:
while (sum < 1000) {
getnext(value);
if (value < 0) continue;
sum += value;
}
 The data-based iterators are loop statements for processing data structures such as linked lists,
hashes, and trees. The C-based languages use the for statement to allow to user to create iterators
for user-defined data. C# and Perl use the foreach statement as a predefined iterator for standard
data structures.
 Example of the use of foreach in C#:
List<String> names = new List<String>();
names.Add(“Alecks”);
names.Add(“Arvin”);
names.Add(“Gab”);
...
foreach (String name in names)
Console.WriteLine(name);

Unconditional Branching
 An unconditional branch statement transfers execution control to a specified location in the program.
 Example of the use of the goto statement in C#:
switch (n)
{
case 1:
cost += 10;
break;
case 2:
cost += 10;
goto case 1;
case 3:
cost += 20;
goto case 1;
default:
Console.WriteLine(“Invalid selection.”);
break;
}

References:

Ben-Ari, M. (2006). Understanding programming languages. Chichester: John Wiley & Sons, Inc.

Sebesta, R. (2015). Concepts of programming languages. (11th ed.). USA: Pearson Education, Inc.

Tucker, A. and Noonan, R. (2002). Programming languages: Principles and paradigms. (2nd ed). New York: Mc-
Graw Hill

07 Handout *Property of
STI

You might also like