Session 6

Control Structure Statements

October 28, 2017

in this session we going to learn about control structures. so what is control structures ?
A control structure is a control statement and the statements whose execution it control.

control structure consist of 2 statements :

I. Selection Statements
A selection statement provides the means of choosing between two or more paths of execution
Two general categories:

–Two-way selectors

–Multiple-way selectors

1. Two-Way Selection Statements

  • General form:

if control_expression

then clause

else clause

  • Design Issues:

–What is the form and type of the control expression?

–How are the then and else clauses specified?

–How should the meaning of nested selectors be specified?

 

The Control Expression

  • If the then reserved word or some other syntactic marker is not used to introduce the then clause, the control expression is placed in parentheses
  • In C89, C99, Python, and C++, the control expression can be arithmetic
  • In most other languages, the control expression must be Boolean

Clause Form

  • In many contemporary languages, the then and else clauses can be single statements or compound statements
  • In Perl, all clauses must be delimited by braces (they must be compound)
  • In Fortran 95, Ada, Python, and Ruby, clauses are statement sequences
  • Python uses indentation to define clauses

if x > y :

x = y

print ” x was greater than y”

 

Nesting Selectors

  • Java example

if (sum == 0)

if (count == 0)

result = 0;

else result = 1;

  • Which if gets the else?
  • Java’s static semantics rule: else matches with the nearest previous if
  • To force an alternative semantics, compound statements may be used:

if (sum == 0) {

if (count == 0)

result = 0;

}

else result = 1;

  • The above solution is used in C, C++, and C#

2. Multiple-Way Selection Statements

  • Allow the selection of one of any number of statements or statement groups
  • Design Issues:

What is the form and type of the control expression?

How are the selectable segments specified?

Is execution flow through the structure restricted to include just a single selectable segment?

How are case values specified?

What is done about unrepresented expression values?

 

  • C, C++, Java, and JavaScript

  switch (expression) {

case const_expr1: stmt1;

case const_exprn: stmtn;

[default: stmtn+1]

}

  • Multiple Selectors can appear as direct extensions to two-way selectors, using else-if clauses, for example in Python:

if count < 10 :

bag1 = True

elif count < 100 :

bag2 = True

elif count < 1000 :

bag3 = True

 

II. Iterative Statements

  • The repeated execution of a statement or compound statement is accomplished either by iteration or recursion
  • General design issues for iteration control statements:
  1. How is iteration controlled?
  2. Where is the control mechanism in the loop?

Counter-Controlled Loops

  • A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize valuesExample :

1.Ada

  for var in [reverse] discrete_range loop               …

end loop

  • Design choices:
  • Type of the loop variable is that of the discrete range (A discrete range is a sub-range of an integer or enumeration type).
  • Loop variable does not exist outside the loop
  • The loop variable cannot be changed in the loop, but the discrete range can; it does not affect loop control
  • The discrete range is evaluated just once
  • Cannot branch into the loop body

2. C-based languages

for ([expr_1] ; [expr_2] ; [expr_3]) statement

– The expressions can be whole statements, or even statement sequences, with the statements separated by commas

–The value of a multiple-statement expression is the value of the last statement in the expression

–If the second expression is absent, it is an infinite loop

  • Design choices:

-There is no explicit loop variable

-Everything can be changed in the loop

-The first expression is evaluated once, but the other two

are evaluated with each iteration

– It is legal to branch into the body of a for loop in C

 

Logically-Controlled Loops

  • Repetition control is based on a Boolean expression
  • Design issues:

–Pretest or posttest?

–Should the logically controlled loop be a special case of the counting loop statement  or a separate statement?

Example :

  • C and C++ have both pretest and posttest forms, in which the control expression can be arithmetic:

while (control_expr)  do

loop body    loop body

while (control_expr)

– In both C and C++ it is legal to branch into the body

of a logically-controlled loop

  • Java is like C and C++, except the control expression must be Boolean (and the body can only be entered at the beginning — Java has no goto

User-Located Loop Control Mechanisms

  • Sometimes it is convenient for the programmers to decide a location for loop control (other than top or bottom of the loop)
  • Simple design for single loops (e.g., break)
  • Design issues for nested loops

Iteration Based on Data Structures

  • The number of elements in a data structure controls loop iteration
  • Control mechanism is a call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminate
  • C’s for can be used to build a user-defined iterator:

  for (p=root; p==NULL; traverse(p)){

}

 

This entry was posted in Session Summary. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *