Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Statement-Level Control Structures
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ISBN Chapter 8 Statement-Level Control Structures.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
Chapter 8 . Sequence Control
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
ISBN Lecture 08 Statement-Level Control Structures.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Statement-Level Control Structures
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Loops and Iteration for Statements, while Statements and do-while Statements.
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 8: Statement-Level Control Structures
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Control Structures sequence of execution of high-level statements.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
W E E K F I V E Statement-Level Control Structures.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
Def: A control structure is a control statement and
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
JavaScript: Control Statements.
Chapter 8: Control Structures
Statement-Level Control Structures
Control Structures In Text: Chapter 8.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Statement-Level Control Structures
CSC215 Lecture Control Flow.
Chapter 8: Statement Level Control Structures
Presentation transcript:

Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution of the program. while (i<100) Control Statement { fun(); Control Structure k++; }

Chapter 8 (Control Structure) Slide 2 Statement Level Control Composition: Statements are placed in a textual sequence, and they are executed in order. Alternation/Selection: Two or more sequence of statements form alternatives, so that one of the sequences is executed. Iteration/Loop: A sequence of statements may be executed repeatedly Jump/Branch: Control is transferred from one statement to another, which need not necessarily be placed in a textual sequence.

Chapter 8 (Control Structure) Slide 3 Two Way Selection Statements The “if” statement: if (expr) {... then clause } else {... else clause (Optional) } In Java, “expr” has to be either a relational expression or a Boolean expression.

Chapter 8 (Control Structure) Slide 4 Dangling Else Problem if (expr1) if (expr2) {... } else {... } The solution to the dangling else problem is to pair an else to the most recent unpaired if in the current block.

Chapter 8 (Control Structure) Slide 5 Multiple Selection Statements (1) If-elseif statementSwitch statement if (expr) {... then clause } elseif (expr) {... elseif clause } else {... else clause } switch (expr) { case v1:... case clause case v2:... case vn: default:... default clause }

Chapter 8 (Control Structure) Slide 6 Multiple Selection Statements (2) The if –elseif statement can be represented by a nested if-else statement. A switch statement can be represented by an if-elseif statement. The expression in a ‘switch’ must evaluate to an integer, and the value in a case clause must be a constant.

Chapter 8 (Control Structure) Slide 7 Iteration Statements An iterative statement causes a collection of statements to be executed zero, one, or more times. Pretest: if the condition for loop termination is tested at the top of the loop (before the statements). Posttest loop: the condition for exiting the loop is tested after the statements in the loop.

Chapter 8 (Control Structure) Slide 8 Counter-controlled Loop  Has a variable of a numeric type, called the loop variable in which the count value is maintained.  Has loop parameters specifying the initial and terminal values and optionally a step size, of the loop variable. These values determine the number of iterations performed.  The loop variable is not allowed to be modified inside the loop.

Chapter 8 (Control Structure) Slide 9 Counter controlled loop (Egs.) PASCAL: for k:= 1 to 10 do begin i:=i-1; p:=i*5; end FORTRAN: DO I = INIT, TERM, STEP K=K+1 P=K*2 END DO

Chapter 8 (Control Structure) Slide 10 C for-loop C uses the “for-loop” to emulate a counter controlled loop. It is not strictly a counted iteration - it does not have any of the characteristics of a counter controlled loop. for (k=1; k<=10; k++) {... } “k” is not a loop variable, since we can use any expression in its place, and the variable “k” is allowed to be modified inside the loop body.

Chapter 8 (Control Structure) Slide 11 Logically Controlled Loop Logically controlled loops are much simpler than counter controlled loops, and are based on the value of a logical expression. PretestPosttest while(expr) {... } do {... } while(expr); The posttest loop body will always be executed at least once.

Chapter 8 (Control Structure) Slide 12 C “for” loop for (expr1; expr2; expr3) {... } is equivalent to expr1; while(expr2) {... expr3; } All expressions above are optional. An expression can contain multiple statements separated by commas.

Chapter 8 (Control Structure) Slide 13 “for” loop in C++, Java C++ and Java allow variable declaration in the first expression. In C++, the scope of a variable declared in the first expression is from its definition to the end of the function in which it is defined. In Java, the scope of such a variable is that of the loop body. Java also has the restriction that the second expression must be either a relational or a Boolean expression.

Chapter 8 (Control Structure) Slide 14 Branching (Jump) “Go to” Statements must be avoided.  The statement level control provided by ‘Go to’ is unstructured.  ‘Go to’ statements severely affect readability of the code.  ‘Go to’ statements make the code more prone to errors, and hampers code development.

Chapter 8 (Control Structure) Slide 15 “Go to” (Example in C) for(...){... if(i==0) goto error: }... error:......

Chapter 8 (Control Structure) Slide 16 Advantages of “goto”  Direct hardware support for efficient execution.  Simple and completely general-purpose - can be used to implement any other control form.  Useful in abortive exits in iterations (eg. required to conditionally abandon processing in some deeply nested structure).  Sometimes useful in control branching in large program codes.  The break statement is a restricted form of unlabeled branch statement, which is used to leave the innermost block.