Monitoring System

Statement Level Control Structures
Chapter 8

Overview
• • • • Selection Statements Iterative Statements Unconditional Branching Guarded Commands

2

Statement Level Control Structures?
• Constructs that allow the program to select from alternative control flow paths (if else construct) • Allow repeated execution of statements (loops)

3

Control Statements
• Research on control statements during the 1960s and 1970s
• ‘goto’ statements are sufficient • A language without goto statements really only needs a few other controls • All algorithms that can be expressed by flowcharts can be coded using just two control structures

4

What Do They Look Like?
• Number of control statements used expanded by writability
• • • • While For If – else Switch

• Restricted by readability • On the other hand, too few control statements can be harmful to readability

5

Selection Statements
• Means of choosing between two or more execution paths • Two categories
• Two-way • n-way (multiple selection)

6

Two-way Selection
• What we know as if-else constructs • Variations in their evolution can be seen in their diverse syntax in different languages Basic construct is: if control_expression then clause else clause
7

The Control Expression
• In C-based languages – usually in parentheses • If the language has a then reserved word, then parentheses not necessary (Ada) • Usually a Boolean expression if 5 == 7 then print “Your compiler is broken” else print “Your compiler works!”
8

The Clause
• In modern languages the then clause and else clause can be single or compound • Perl forces all clauses to be compound • In Fortran 95 and Ada , the clauses are just statement sequences – must be ended with keyword end

9

Nesting Selectors
• Problems with Nesting Selectors Java: if (sum == 0) if (count == 0) result = 0; else result = 1; Python: if (sum == 0): if (count == 0): result = 0 else: result = 1

10

Nested Selectors Continued...