Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

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.
Statement-Level Control Structures
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
CS0004: Introduction to Programming Repetition – Do Loops.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Program Design and Development
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
Python quick start guide
Ranga Rodrigo. Class is central to object oriented programming.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Visual Basic: An Object Oriented Approach 5: Structured Programming.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Fundamentals of C and C++ Programming Control Structures and Functions.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
CPS120: Introduction to Computer Science Decision Making in Programs.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
I Power Higher Computing Software Development High Level Language Constructs.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Fortran: Control Structures Session Three ICoCSIS.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Unsur-unsur Algoritma
‘C’ Programming Khalid Jamal.
VBA - Excel VBA is Visual Basic for Applications
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
Introduction To Repetition The for loop
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Chapter 5: Control Structures II
Quick Test What do you mean by pre-test and post-test loops in C?
Chapter 8: Control Structures
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Chapter 8 JavaScript: Control Statements, Part 2
Structured Program
Statement-Level Control Structures
Homework Any Questions?.
Chapter8: Statement-Level Control Structures April 9, 2019
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Chap 7. Advanced Control Statements in Java
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Control Structures Ranga Rodrigo

Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end

Any structured program can be written using sequence statements, conditional statements, and repetition. Structured Programming Sequence Conditional Statements Loops

Control Structures They control the sequence of basic statements that is executed. They break a program up into relatively independent pieces, each of which behaves in a simple and predictable way: they introduce structure into programs. Eiffel tries to simplify the normal repertoire control structures in some aspects.

With No Control Structures Two mechanisms: Jump or goto instructions. Subroutine calls. FORTRAN example: X = 0 10 X = X + 1 A[X] =... IF (X.EQ.10) GOTO 20 GOTO CONTINUE X = 0 10 X = X + 1 A[X] =... IF (X.EQ.10) GOTO 20 GOTO CONTINUE

Structured: No gotos goto can create very complex programs: spaghetti-like. Edsgar Dijkstra: “goto statement … considered harmful.” Structured: replace the use of jumps by a small set of simple and well-understood control structures. E.g., the loop above is replaced by a for statement:

X = 0 10 X = X + 1 A[X] =... IF (X.EQ.10) GOTO 20 GOTO CONTINUE X = 0 10 X = X + 1 A[X] =... IF (X.EQ.10) GOTO 20 GOTO CONTINUE for (x = 0; x < 10; x++) { a[x] =... } for (x = 0; x < 10; x++) { a[x] =... }

One Entry- and Exit-Point Rule One commonly quoted rule of structured programming was that each control structure should have only one entry point and one exit point. The idea was that this would make it easy to understand what a program is going to do next.

One Entry- and Exit-Point Rule Eiffel enforces this rule consistently. C++/Java do not: break allows you to jump out of the middle of a loop. continue in effect jumps back to the beginning of the current loop. C and C++ have a goto statement.

Control Structures and Exceptions One problem with structured programming is that it can be awkward to deal with error trapping and exceptional situations. C++, Java, and Eiffel all have an exception mechanism which allow control to be transferred elsewhere in the program in certain circumstances.

Sequencing The default control structure is to perform statements one after another, in the order in which they are written: sequencing. Many languages use semi-colons to separate statements, but in Eiffel these are optional.

Conditional Statement: IF The elseif and else clauses are optional. Control enters an if statement at the top, and leaves at the end. if test then... elseif test then... else... end if test then... elseif test then... else... end

Case or Switch: INSPECT if option = 'c' then counter.clear elseif option = 'i' then counter.increment elseif option = 'p' then counter.print else io.print_string("Invalid option") end if option = 'c' then counter.clear elseif option = 'i' then counter.increment elseif option = 'p' then counter.print else io.print_string("Invalid option") end

Case or Switch: INSPECT inspect exp when a, b, c then... when x.. z then... else... end inspect exp when a, b, c then... when x.. z then... else... end exp must be of type INTEGER or CHARACTER. After when the programmer can specify lists or subranges of values. The else part of the instruction is optional.

Iteration: from-until-loop-end from initialization until exit condition loop... end from initialization until exit condition loop... end

Iteration This loop will continue until the exit condition becomes true. This is the opposite behaviour from C++ or Java while loops, which terminate when the condition becomes false. There is no loop statement in Eiffel with the test of the exit condition at the end (No do-while loop.) There is no for statement.

Iteration This loop will continue until the exit condition becomes true. This is the opposite behaviour from C++ or Java while loops, which terminate when the condition becomes false. There is no loop statement in Eiffel with the test of the exit condition at the end (No do-while loop.) There is no for statement.

for in Eiffel from i := 0 until i = 10 loop -- do stuff i := i + 1 end from i := 0 until i = 10 loop -- do stuff i := i + 1 end

Iteration The only way an Eiffel loop can finish is by the exit condition becoming true. Unlike C, C++ and Java there are no break or continue statements. This follows the structured programming principle of making the flow of control easy to follow. Arguably, it makes the conditions of some loops harder to write and read.

E.g., Locating a Value in an Array

find(v : INTEGER; a : ARRAY[INTEGER]) : INTEGER is local i : INTEGER do from i := a.lower found := false until found or i > a.upper loop if a.item(i) = v then found := true else i := i + 1 end Result := i end find(v : INTEGER; a : ARRAY[INTEGER]) : INTEGER is local i : INTEGER do from i := a.lower found := false until found or i > a.upper loop if a.item(i) = v then found := true else i := i + 1 end Result := i end

The debug Statement debug io.put_string("x = ") io.put_integer(x) end debug("all") io.put_string("y = ") io.put_real(y) end debug io.put_string("x = ") io.put_integer(x) end debug("all") io.put_string("y = ") io.put_real(y) end

Procedures p (arg1 : Type ;... ; argn : Type) is require -- precondition here local -- local entities declared here do -- compound statement here ensure -- postcondition here end p (arg1 : Type ;... ; argn : Type) is require -- precondition here local -- local entities declared here do -- compound statement here ensure -- postcondition here end

Procedures The only way of exiting from a routine body is by reaching its end. Eiffel has no return statement. Formal parameters (the args) cannot be assigned to in the body of a routine. Parameters are initialized by assignment. Procedure calls are written using p(x, y) notation. If there are no parameters, however, the brackets are omitted.

Functions f (a1 : T1 ;... ; an : Tn) : T is require -- precondition local -- local entities declared here do -- statement, assignment to 'Result' Result := return value -- more statements ensure -- postcondition end f (a1 : T1 ;... ; an : Tn) : T is require -- precondition local -- local entities declared here do -- statement, assignment to 'Result' Result := return value -- more statements ensure -- postcondition end

Fucntions In addition to the points made about routines: Result is an implicitly declared local variable of the return type of the function. The value held in Result when the function ends is the value returned by the function When a function is called, you must do something with the returned value.

Control Structures and DBC Pre- and post-conditions state properties that should be true at the beginning and end of a routine. They are not called by client code. They are not usually specified as formally as routines. However, it can be useful to annotate code with expected properties. As with DBC, this can be a good way of catching errors in program development.

Assertions The check statement can be used to add run- time checks at any point in a program. For example, after completion of the loop in the find function, the value of i should beat most one more than the upper bound of the array: check a.lower <= i and then i <= a.upper + 1 end check a.lower <= i and then i <= a.upper + 1 end

Specifying Loops: Variants and Invariants Loop design can be difficult: the exit condition must be correct, and the loop must execute the correct number of times. Eiffel supports the use of loop variants and loop invariants (not to be confused with class invariants) to specify the behaviour of loops. The invariant is an expression that must be true every time the loop restarts. The variant is an integer expression that must get less every time the loop repeats. This ensures that it will terminate.

make is local v : INTEGER a : ARRAY[INTEGER] i : INTEGER found : BOOLEAN do create a.make(1,10) from i := 1 found := false invariant -- v not in the array slice [1..i) variant 10 - i make is local v : INTEGER a : ARRAY[INTEGER] i : INTEGER found : BOOLEAN do create a.make(1,10) from i := 1 found := false invariant -- v not in the array slice [1..i) variant 10 - i CONTD.

until found or i > 10 loop if a[i] = v then found := true else i := i + 1 end io.put_integer(i) end until found or i > 10 loop if a[i] = v then found := true else i := i + 1 end io.put_integer(i) end CONTD.