



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
CS 403, Class 10 Slide # 2
CS 403, Class 10 Slide # 3
CS 403, Class 10 Slide # 4
The cartoon that was on this page was taken from http://www.borg.com/~rjgtoons/toon.html
CS 403, Class 10 Slide # 5
CS 403, Class 10 Slide # 6
CS 403, Class 10 Slide # 7
Levels of Control Flow
Evolution
CS 403, Class 10 Slide # 8
Control Structures
CS 403, Class 10 Slide # 9
Selection Statements
CS 403, Class 10 Slide # 10
Selection Statements
Single-Way Examples
FORTRAN IF : IF (boolean_expr) statement
Problem: can select only a single statement; to select more, a goto must be used, as in the following example FORTRAN example:
IF (.NOT. condition ) GOTO 20 ... ... 20 CONTINUE
CS 403, Class 10 Slide # 11
Two-way Selector Examples
CS 403, Class 10 Slide # 12
e.g. (Pascal) if ... then if ... then ... else ...
Which then gets the else? Pascal's rule: else goes with the nearest then
Nested Selectors
CS 403, Class 10 Slide # 19
Counter-Controlled Loops
FORTRAN 90’s Other DO
Syntax: [name:] DO variable = initial, terminal [, stepsize] … END DO [name]
Loop var must be an INTEGER
CS 403, Class 10 Slide # 20
Counter-Controlled Loops
Syntax: for var := <list_of_stuff> do statement where <list_of_stuff> can have: list of expressions expression step expression until expression expression while boolean_expression for index := 1 step 2 until 50, 60, 70, 80, index + 1 until 100 do
( index = 1, 3, 5, 7, ..., 49, 60, 70, 80,81, 82, ..., 100)
CS 403, Class 10 Slide # 21
Counter-Controlled Loops
Control expression can be int or real ; its scope is whatever it is declared to be Control var has its last assigned value after loop termination The loop var cannot be changed in the loop, but the parameters can, and when they are, it affects loop control Parameters are evaluated with every iteration, making it very complex and difficult to read
CS 403, Class 10 Slide # 22
Counter-Controlled Loops
CS 403, Class 10 Slide # 23
Counter-Controlled Loops
end loop
CS 403, Class 10 Slide # 24
Counter-Controlled Loops
CS 403, Class 10 Slide # 25
Counter-Controlled Loops
Syntax: 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 e.g., for (i = 0, j = 10; j == i; i++) ... If the second expression is absent, it is an infinite loop. CS 403, Class 10 Slide # 26
Counter-Controlled Loops
other two are evaluated with each iteration.
This loop statement is the most flexible.
CS 403, Class 10 Slide # 27
Counter-Controlled Loops
Differs from C in two ways:
definitions (scope is from the definition to the end of the function in which it is defined)
CS 403, Class 10 Slide # 28
Counter-Controlled Loops
Differs from C++ in two ways:
expression is only the loop body
CS 403, Class 10 Slide # 29
Exercise: Page 325 #
k = (j + 13) / 27 loop: if k > 10 then goto out k := k + 1 i := 3 * k – 1 goto loop out:
CS 403, Class 10 Slide # 30
Logically-Controlled Loops
Design Issues:
CS 403, Class 10 Slide # 37
Unconditional Branching
Problem: readability Some languages do not have them:e.g., Modula- and Java Label forms:
Restrictions on Pascal's gotos:
A statement group is either a compound statement or the body of a repeat-until The target of a goto cannot be a statement in a statement group that is not active Means the target can never be in a statement group that is at the same level or is nested more deeply than the one with the goto An important remaining problem: the target can be in any enclosing subprogram scope, as long as the statement is not in a statement group This means that a goto can terminate any number of subprograms