Please enable JavaScript.
Coggle requires JavaScript to display documents.
Control Flow, Evolution of control statements
machine level -changing…
Control Flow
control structure
- A control statement and a collection of statements whose execution it controls
Control statement
- Basic mechanisms for controlling the execution of individual statements within a program
- Order of execution
1.Flow of control within expressions.
- operator precedence and associativity
2.Flow of control among program units
- Function call and concurrency.
3.Flow of control among statements
- Control statements and control structures
compound statements
- a language feature , helps make control statement design easier
- collection of statements to be abstracted into
a single statement.
- FORTRAN - no , introduced-ALGOL60
- statements are written in the order in which they
are to be executed.
- Adding data declarations -Block
- Different languages use different syntactic to encode
syntactic
- python -indentation
- MYSQL - Begin-End , If- End If , Loop-End Loop
Design Issues
- Multiple entry points
-Multiple exits
forms of statement-level sequence control
- Composition(textual order)
- Alternation
- Iteration(execute repeatedly)
Explicit sequence control
- goto statement -most powerful statement for controlling the flow of execution
- Improper usage may result in highly unreadable and difficult to
maintain programs – Spaghetti code.
- disadvantages - more than advantages..goto not use in some new languages
- Java not support to goto
-
-
Break Statement
- Causes control to move forward in the program to an explicit point at the end of a given control structure such as while (loop is completely stop)
- java -break --> labeld or unlabeled
Continue Statement
- Causes control to move forward to the end of the current loop body(stop and start next iteration of loop)
Selection statements
- Provide the means of choosing between two or more execution paths in a program
Single-way selection
- FORTRAN ,BASIC
If(.NOT.Condition)
Continue
- ALGOL 60
-
N-way (multiple) selection
- providing a rule
- providing a syntactic form
- selection closure
Semantics
- The expression is evaluated and the value is compared with the constants in the constant lists
- If a match is found control transfers to the statement attached to the matched constant list
- When statement execution is completed, control transfers to the first statement following the case structure
Implementation
- use a jump table
- Vector stored sequentially in memory, each of whose components is an unconditional jump instruction
Iteration statements
- a statement or a collection of statements to be executed zero or more times
- Head -controls the number of times that the body will be executed
- Body - a (compound) statement that provides the action of the statement
- Simple repetition
- Counter-controlled loops
- Logically Controlled Loops
- User-Located Loop Control Mechanism
- Iteration based on data structures
- User-Defined Iteration Control
Logically Controlled loops
- Repetition control is based on a boolean expression
- Every counter controlled loop can be built with a logically controlled loop, but not the converse
User-located loop controls
- no iteration control mechanism; they are infinite loops unless controls are added by the programmer.
exit - FORTRAN 90 ,JAVA
break - C , C++
Counter-controlled loops
- has a loop variable
- step-size
- Loop parameters
Iteration based on data structures
- a data structure is used to control the iteration (. Number of iterations is controlled by the number of elements in the data structure.)
Simple repetition
- Statement head specifies that the body is to be executed some fixed number of times.
User-Defined Iteration Control
- The iterator is called at the beginning of each iteration, and each time it is called, the iterator returns an element from a particular data structure in some specific order.
- Iterator must be history sensitive : must remember which item is processed last.
- a user-defined iteration statement terminates when the iterator fails to find more elements
-
Evolution of control statements
- machine level -changing the value of program counter
- CPU -conditional or unconditional jumps(Gotos)
- FORTRAN - control statements directly -IBM704
- Structured Programming Theorem
all algorithms that can be expressed by flowcharts can be coded in a programming language with with only two-way selection and pretest logical loops
- Turing complete
can compute anything that is computable
- control statement
- selection
- pretest logical loop
- readability
- writability
Labels
- an explicit name or number assign to a fixed position within source code
- referenced by control flow statements
label forms
- Identifiers -Algol 60 ,C
- Unsigned integers - FORTRAN ,Pascal
- variables - PL/I
- label are attached to statements by colons
-
-