ControlFlow

ControlFlow - COP4020 Programming Languages Control Flow...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Control Flow Prof. Robert van Engelen
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 4/7/11 Overview ! Structured and unstructured flow " Goto's " Sequencing " Selection " Iteration and iterators " Recursion " Applicative- and normal-order evaluation ! Expressions evaluation " Evaluation order " Assignments
Background image of page 2
COP4020 Spring 2011 3 4/7/11 Control Flow: Ordering the Execution of a Program ! Constructs for specifying the execution order: 1. Sequencing : the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text 2. Selection (or alternation): a run-time condition determines the choice among two or more statements or expressions 3. Iteration : a statement is repeated a number of times or until a run-time condition is met 4. Procedural abstraction : subroutines encapsulate collections of statements and subroutine calls can be treated as single statements 5. Recursion : subroutines which call themselves directly or indirectly to solve a problem, where the problem is typically defined in terms of simpler versions of itself 6. Concurrency : two or more program fragments executed in parallel, either on separate processors or interleaved on a single processor 7. Nondeterminacy : the execution order among alternative constructs is deliberately left unspecified, indicating that any alternative will lead to a correct result
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 4 4/7/11 Structured and Unstructuted Flow ! Unstructured flow : the use of goto statements and statement labels to implement control flow " Merit or evil? " Generally considered bad, but sometimes useful for jumping out of nested loops and for coding the flow of exceptions (when a language does not support exception handling) " Java has no goto statement (supports labeled loops and breaks) ! Structured flow : " Statement sequencing " Selection with “if-then-else” statements and “switch” statements " Iteration with “for” and “while” loop statements " Subroutine calls (including recursion) " All of which promotes “structured programming”
Background image of page 4
COP4020 Spring 2011 5 4/7/11 Sequencing ! A list of statements in a program text is executed in top- down order ! A compound statement is a delimited list of statements " A compund statement is called a block when it includes variable declarations " C, C++, and Java use { and } to delimit a block " Pascal and Modula use begin . .. end " Ada uses declare . .. begin . .. end ! Special cases: in C, C++, and Java expressions can be inserted as statements ! In pure functional languages sequencing is impossible (and not desired!)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 6 4/7/11 Selection ! If-then-else selection statements in C and C++: " if (<expr>) <stmt> [ else <stmt>] " Condition is a bool, integer, or pointer " Grouping with { and } is required for statement sequences in the then clause and else clause " Syntax ambiguity is resolved with “ an else matches the closest if ” rule !
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.

Page1 / 38

ControlFlow - COP4020 Programming Languages Control Flow...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online