You are on page 1of 29

Control Flow

BY
GARIMA V.PANCHBHAI
Control flow
( ordering in program execution )
• Determines what shoulbe done first,what
second and so forth.
• Language Constructs can be arranged in 7 main
categories
• Sequencing
• Selection
• Iteration
• Procedural abstraction
• Recursion
• Concurrency
• Non determinacy
Expression Evaluation
• An expression generally consist of either a simple object or
function applied to collection of operands or arguments
each of which in turn is an expression .
• In general a language may specify that function call(operator
invocation) employ prefix,infix & postfix.
• Most languages use infix notation for built in operators.
• LISPS uses prefix noation for all functions
(+ 2 (/ 9 x)).
Precedence and Associativity:
Operator Precedence:
• specifies that certain operator, in the absence of parentheses,
group “more tightly” than other operators
Operator Associativity:
• Associativity rules specify that sequence of operators of equal
precedence group to the right or to the left.
• specifies the evaluation order of operators ofthe same
precedence
Left Associativity:
operators are evaluated left-to-right
Right Associativity:
operators are evaluated right-to-left
(Fortran power operator **, C assignment
operator = and unary minus)
No Associativity:
parentheses must be used to specify grouping
(ADA)
Assignments :
• In an imperative language
• Computations consists of an ordered series of changes to the
values of variables in memory.
• Each assignment takes pair of arguments a value and a reference.

• In Functional Language :

• Expression evaluation is dependent on the referencing


environment not on the time at which the evaluation occurs.
• Also known as referentially tranparent.
Initialization :
• For statically allocated variables an initial value
is specified in the context of the declaration.
• Java requires that value be definitely assigned to
variable before that variable is used in any
expression.
• Most common programming error is to use
variable in an expression without initialization.
Initialization

• Explicit initialization, e.g. definite assignment requirement in


Java
• Implicit initialization, e.g. global variables or arrays are
assigned 0’s in C ,Constructors, e.g., in most object-oriented
programming languages
Ordering within Expressions
• Precedence and associativity rules do not specify the order in
which operands of a given operator are evaluated.
• Ordering is important for two reasons
• Side effects
• Code improvement –
some languages allow compiler to rearrange expressions for
faster evaluation.
Short-circuit Evaluation:
• Short-circuit evaluation, minimal evaluation, or Mc-carthy
evaluation denotes the semantics of some Boolean operator
in some programming languages in which the second
argument is only executed or evaluated if the first argument
does not suffice to determine the value of the expression:
when the first argument of the AND function evaluates to
false, the overall value must be false; and when the first
argument of the OR function evaluates to true, the overall
value must be true.
• In some programming languages LISPthe usual Boolean
operators are short-circuit.
• In others (JAVA ADA), both short-circuit and standard Boolean
operators are available.
• For some Boolean operations, like XOR, it is not possible to
short-circuit, because both operands are always required to
determine the result.
• Opportunities for short-circuit evalution are apparent from the
truth tables examined in the previous lab exercise. When two
expressions are joined by OR (||), if the first one is true, the
answer will always be true. When two expressions are joined by
AND (&&), if the first one is false, the answer will aways be
false. It doesn't matter what the second expression is and
hence, it doesn't need to be evaluated.
|| true false
true true true
false true false

&& true false


true true false
false false false
Structured and Unstructured
flow:
• Control flow in assembly language is achieved
by means of conditional and unconditional
jumps.
• In HLL it is achieved by goto construct.
• In structured programming abandons the use of
goto.
• GoTo can be used for
• Mid-loop exit and continue
• Early returns from subroutines.
• For handling sErrors and exceptions
Sequencing :
When one statement occurs before another in the
program text the first statement executes before
the second.

Statement block
• groups multiple statements together
Examples
• { } in C, C++, and Java
• begin/end in Algol, Pascal, and Modula

Basic block
• block where the only control flow allowed is
sequencing
Selection :
• In most imperative languages, selection is
implemented using variant of if.. .Then….else…

• In Lisp, the equivalent Equivalent to :


(cond
((= a b) If a = b then ….
(….)) Elseif a = c then …..
((= a c) Elseif a = d then …
(…)) Else ………
((= a d) End.
(…))
(T
(….)))
Short-circuited conditions :
• The purpose of the boolean expression in a selection
statement is not to compute a value to be stored, but to
cause control to branch to various locations.
• Pascal does not support short circuited evaluation.
• Ada,C supports short circuited evaluations.
Advantages of shortcircuited
evaluations :
• Jump code is shorter.
• Faster execution than straight line code.
• In straight line code the value of every subexpression is
calculated.
• Value of the boolean condition is never explicitly placed into a
register.
• The value is implicit in the control flow.
Case/switch statements .

• Alternative syntax for nested if…then…else


• Different cases are called as labels.
• Action part is called as arms of case statements.
• Generate efficient target code.
• C supports default clause with switch
• Pascal does not support default clause, so it is
required to specify every value in label list.
• Alternative to nested if…then…else blocks
j := … (* potentially complicated expression *)
IF j = 1 THEN clause_A
ELSEIF j IN 2,7 THEN clause_B
ELSEIF j IN 3..5 THEN clause_C
ELSEIF (j = 10) THEN clause_D
ELSE clause_E
END
Iteration :
• Allow to perform set of operations repeatedly.
• Iterations takes the form of loops.
• Two main types of Loops are
• Enumeration-Controlled Loop
• Executed once for every value in a finite set

• Logically-Controlled Loop
• Execute until some boolean condition
• Depends on value altered in the loop
Enumeration controlled Loop:
• Is executed once for every value in a given finite
set.
• The number of iterations are known before the
execution of the loop.
• Eg. In Fortran
do 10 i=1, 10, 2
…..
10: continue
The number after do is a label, indicates the last
statement in the body of the loop.
Continue is like “no-op”.
Recursion :
• Requires no special syntax.
• Permits functions to call itself.
• Mostly used in functional languages.
• Decompose problem into smaller problems by
calling itself
• Some languages don’t permit recursion: Fortran
77
Tracing a Recursive Function

(define sum (lambda(n)


(if (= n 0)
0
(+ n (sum (- n 1))))))
Applicative and normal order
evaluation :
• Evaluation of arguments before passing them to
subroutine is called applicative-order evaluation.
• Evaluation of arguments only when the value is
actually needed is known as normal order
evaluation.
• Normal order evaluation occurs with macros.
• Normal order evaluation lead to faster code, but
may lead to run time errors.
(define double (lambda (x) (+ x x)))
(double (* 3 4))
>(double 12)
>(+ 12 12)
➢24
(double (* 3 4))
➢(+ (* 3 4) (* 3 4))
➢(+ 12 (* 3 4))
➢(+ 12 12)
➢24
Nondeterminacy:
• A nondeterministic construct is the one in which
choice between alternatives is not specified.
• Dijkstra proposed guarded command notation
for nondeterminacy.
• General form
if condition -> stmt_list
[ ] condition -> stmt_list
[ ] condition -> stmt_list
…..
fi
• Each of the condition is known as guard.
• The guard and the following statement , together are
called a guarded command.
• Non determinacy for logically controlled loops
do condition -> stmt_list
[ ] condition -> stmt_list
[ ] condition -> stmt_list
……
od

The loop terminates when none of the guards is true.

You might also like