Data dependent execution order data dependent control flow

Similar documents
Reasoning about program behaviour Assertions

Flexible data exchange with a method Parameters and return value

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Arrays. Eng. Mohammed Abdualal

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Example: Monte Carlo Simulation 1

Controls Structure for Repetition

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

AP CS Unit 3: Control Structures Notes

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Loops. CSE 114, Computer Science 1 Stony Brook University

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Object Oriented Programming. Java-Lecture 6 - Arrays

Mr. Monroe s Guide to Mastering Java Syntax

Java+- Language Reference Manual

Spring 2010 Java Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Introduction to the Java Basics: Control Flow Statements

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Java Coding 3. Over & over again!

Example Program. public class ComputeArea {

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Handout 9: Imperative Programs and State

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

User-built data types Mutable and immutable data

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

array Indexed same type

( &% class MyClass { }

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Full file at

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Programming with Java

Instructor: Eng.Omar Al-Nahal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Loops and Expression Types

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Give one example where you might wish to use a three dimensional array

2.2 - Making Decisions

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

Repetition Structures

Introduction to Programming Using Java (98-388)

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Computer Science II (20082) Week 1: Review and Inheritance

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Quarter 1 Practice Exam

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

DM550 Introduction to Programming part 2. Jan Baumbach.

Array. Array Declaration:

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Introduction to Computer Science Unit 2. Notes

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Motivation was to facilitate development of systems software, especially OS development.

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

COMP 202 Java in one week

Lab #10 Multi-dimensional Arrays

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Control Structures: if and while A C S L E C T U R E 4

Some notes about Event-B and Rodin

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

NATIONAL UNIVERSITY OF SINGAPORE

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved.

Chapter 3: Operators, Expressions and Type Conversion

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

COMP 202. Java in one week

JME Language Reference Manual

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

egrapher Language Reference Manual

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Lecture 3. Lecture

Flow of Control. Chapter 3

Motivation was to facilitate development of systems software, especially OS development.

Introduction to Computer Science Unit 2. Notes

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

APCS Semester #1 Final Exam Practice Problems

Transcription:

Chapter 5 Data dependent execution order data dependent control flow The method of an object processes data using statements, e.g., for assignment of values to variables and for in- and output. The execution order of statements is determined by the order in which control passes to statements, the control flow. Up till now, statements were executed in the order as written; control flow was sequential. Furthermore, this order was independent of the data values being processed; control flow was data independent. In many cases this is too restrictive, as at certain points in the execution it may depend on the current data values which of several possible actions should be taken, i.e., which of several statements should be executed. Java provides data dependent control statements that make this possible. 5.1 Choice if-else statement Depending on data values one of two statements is chosen for execution. 5.1.1 Aim We want to describe that one of two statements that are both written in the program are executed, where which one is executed depends on data values that may differ for different executions of the program. 5.1.2 Means We use the if-else statement, which selects one of two statements for execution, depending on the value of a selection guard. The flow of control is data dependent: which statement is chosen for execution depends on data values and may, e.g., differ for executions with different input values. In the example below, different updates are performed on an account depending to whether a is solvent or overdrawn. import java.util. ; // handles account public class AccountUpdate { Scanner sc = new Scanner(System.in); ; // of account in some currency int rate; // interest rate in percent 33

int debitrate; // debit rate in percent // set and update void { System.out.println( Type interest rate. ); rate = sc.nextint(); System.out.println( Type debit rate. ); debitrate = sc.nextint(); System.out.println( Type amount for. ); = sc.nextdouble(); if (>=0) { // guard = + (rate )/100; System.out.println(rate + % interest has been added. ); else { = + (debitrate )/100; System.out.println(debitRate + % debit interest has been subtracted. ); System.out.println( New is + +. ); // start up code public static void main(string[] args) { new AccountUpdate().; Result output is, in case of inputting a non-negative amount of, e.g., 60: Type interest rate >3 Type debit rate >5 Type amount for. >60 3% interest has been added. New is 61.8. Result output is, in case of inputting a negative amount of, e.g., -60: Type interest rate >3 Type debit rate >5 Type amount for. >-30 5% debit interest has been subtracted. New is -31.5. When the if-else statement is executed, first the guard, ( >= 0), is evaluated. In case the guard is true, i.e., the account is solvent, the statements between the brackets following the if are executed, adding interest. In case the guard is false, i.e., the account is overdrawn, the statements between the brackets following the else are executed, imposing the penalty. After execution of the chosen statement, execution 34

of the if-else statement is completed and control goes, in both cases, to the next statement, showing the new. The general shape and meaning of the if-else statement are as follows. Syntax Template if ( boolean expression ) { statements1 else { statements2 A group of statements surrounded by brackets, a so-called code block is again one statement. By convention, we always write the statements in the two clauses of the if-else statement between brackets, also if a clause consists of only one statement. The complete if-else statement together with its sub-statements is again one statement. If the program only needs to perform a statement when the guard is true, and does not need to do anything in case the guard is false, the else clause may be omitted. Semantics The guard is evaluated, if boolean expression is true, statements1 is executed, else statements2 is executed. Then the execution of the if-else statement is completed and control goes to the next statement in the program. 5.1.3 Execution model Example to be added 5.2 Repetition while statement Depending on, changing, data values a statement is executed repeatedly. 5.2.1 Aim We want to describe that a statement (or group of statements) is executed repeatedly, where how often the statement is executed depends on variables that change during the execution of the statement. 5.2.2 Means We use the while statement to repeatedly execute a statement, the while body, depending on the value of a repetition guard. The flow of control is data dependent: how many times the statement is executed depends on data values that change during the execution of the program. The data values that govern the control flow should be updated in the statement that is repeatedly executed: data change is used to stop the repetition. In the example below, the build-up of compound interest of 5% from an inputted is shown, until the owner is a millionaire. import java.util. ; 35

// handles account public class AccountCompound { Scanner sc = new Scanner(System.in); ; int rate; // shows compound interest build up void { rate = 5; System.out.println( Type amount for. ); = sc.nextdouble(); System.out.println( Balance is + Math.round() +. ); while ( <= 1000000) { = + (rate )/100; System.out.println( Balance is + Math.round() +. ); System.out.println( Goodbye, millionaire. ); // main instantiates and uses the object public static void main(string[] args) { new AccountCompound().; Console shows (input is in bold face): Type amount for. 0 Balance is 0. Balance is 840000. Balance is 882000. Balance is 926100. Balance is 972405. Balance is 1021025. Goodbye, millionaire. When control arrives at the while statement, the guard, >=0 is evaluated. If it is true the body, System.out.println( Balance is + +. ); = 100; is executed and after that control goes back to the guard and the execution repeats. If it is false, the while statement is completed and control goes to the next statement. The general shape and meaning of the while statement are as follows. 36

Syntax Template while ( boolean expression (guard) ) { statements (body) Semantics When control arrives at the while-construct, the guard is evaluated. When it is false, the while-construct terminates immediately, otherwise the body is repeatedly executed. After each execution of the body, the guard is evaluated and the execution of the while-construct is terminated if it is false. 5.2.3 Execution model In figure 1.1 the first states of the execution of AccountCompound are shown. The value of the variable scanner is presented as a number similar to the number that labels the AccountCompound object. We will explain this later. 5.3 Remarks 5.3.1 More repetition constructs There are two more repetition constructs that are similar to the while construct. There is no essential difference, but in some case one construct is intuitively easier to use than the other. There is a repetition construct that is quite similar to the while loop, where the body is executed at least once, also when the guard is initially false. In the example program this construct could be naturally used in the case that always, regardless of the, at least one donation of 100 is given. The while-code would then be replaced by the following. do { System.out.println( Balance is + ); = + (5 )/100; while ( < 1000000); Syntax Template do { statements while (boolean_expression); Semantics statement0 initialises the variables that are used in the guard. Then control arrives at the body, which is executed. Then control arrives at the guard update statements. If the guard is true, control returns to the start of the body, else control goes to the end of the while statement. The constructs have the same expressive power. 37

Figure 5.1: Initial part of the execution of AccountCompound 0.0 int rate 0 void { rate = 5; System.out.println("Type amount for."); = sc.nextdouble(); 0.0 void { rate = 5; System.out.println("Type amo...."); = sc.nextdouble(); 0.0 Scanner scanner #13 void { rate = 5; System.out.println("Type amo...."); = sc.nextdouble(); Type amount for Type amount for 0.0 void { rate = 5; System.out.println("Type amo...."); = sc.nextdouble(); 0.0 Scanner scanner = sc.nextdouble(); System.out.println("Balance is " + Math.round() + "."); while ( <= 1000000) { #13 Type amount for Type amount for Balance is 0. 0.0 Scanner scanner = sc.nextdouble(); System.out.println("Balance is " + Math.round() + "."); while ( <= 1000000) { #13 0.0 Scanner scanner System.out.println("Balance is " + Math.round () + "."); while ( <= 1000000) { = + (rate * )/100; #13 Type amount for Balance is 0. Type amount for Balance is 0. 0.0 Scanner scanner #13 System.out.println("Balance is " + Math.round () + "."); while ( <= 1000000) { = + (rate * )/100; 840000.0 while ( <= 1000000) { = + (rate * )/100; S..ln("Balance is " + Math.round() + "."); Type amount for Balance is 0. Balance is 0. Balance is 840000. int rate 840000.0 5 while ( <= 1000000) { = + (rate * )/100; S..ln("Balance is " + Math.round() + "."); 840000.0 while ( <= 1000000) { = + (rate * )/100; S..ln("Balance is " + Math.round() + "."); Balance is 0. Balance is 840000. Balance is 0. Balance is 840000. 882000.0 while ( <= 1000000) { = + (rate * )/100; S..ln("Balance is " + Math.round() + ".");... 38

There is yet another repetition construct. This construct groups the initialization of the guard, the evaluation of the guard and the update of the guard (that in case of the while and the do-while takes pace in the body) together and separates-out the body. The change to the example program consists in removing the initialization of and replacing the while loop with the following. for ( = sc.nextint(); ( < 1000000); = + (5 )/100)) { System.out.println( is + ); Syntax Template for ( initialization ; guard ; update ) { body statements Semantics The initialization gives the variable in the guard its starting value (if more than one initialization statement is needed, these have to be separated by commas). Then control arrives at the guard. If the guard is true, control arrives at the body, else control goes to the end of the for-statement. Then the update is executed and control returns to the guard. Note that the update is executed after each execution of the body. All three repetition constructs of java have the same expressive power. 5.3.2 Which control statements should there be goto? Intuitively, this dependency of execution order on data could be achieved more flexibly by a control statement like: if control is at a certain point and some variable has certain value, then control goes to some other point, for example indicated by a label. This idea was adopted in early programming languages. This so-called goto control statement fully achieved data dependent control flow, intuitively to encode all control flow orderings. However, the resulting programs became very difficult to understand, because the labels could be placed anywhere: from the static coded of program it was very hard to see where during the dynamic excution control would go, resulting in error-prone programs. Therefore modern programming languages do not have a goto statement anymore, but the more well-structured data dependent control statements given above: the if-else construct and the while (or an equivalent variant). This does not limit the programming tasks that can be solved: the two statements can be shown to enable to write programs with the same computation power as the goto, i.e., provide the same expressive power but in a more structured, less error-prone manner. 5.3.3 Computability Not only provide the above statements the same computation power as the goto, but a much stronger and more general result holds. There is an intuitive, not formally defined, notion of effectively computable: anything that can be computed following a recipy of simple instructions. There are various quite different formalizations of this intuitive notion, notably Turing machines, an operational definition of an idealized computer, and recursive 39

functions, a non-operational, mathematical notion. This thesis is not provable, because it links an intuitive notion to a formal one. Such formal notions have been proven to be equivalent in computation power. This led to Church s Thesis: The effectively computable functions are the ones that are computable by a Turing machine (or a recursive function, or any of the other provably equivalent formal notions). The result now is that, given some suitable coding agreements, any computable function is computable by a computer that has the data type integer and the control flow constructs sequential composition, choice and while. This means that at this point we can in principle program everything! However, the main problem in programming is not what can be programmed in principle, but how to obtain the programs. For this, structuring, more specifically decomposition into manageable parts and abstraction to a manageable level is the main option. This is what the rest of the book is about. 40

Chapter 6 Matrices of variables Arrays of variables Up till now only the built-in types were used. To enable structured storage there is a type constructor in Java to make new types from already available ones. There is some flexibility because the precise form desired may differ from program to program. 6.1 Aim We want to store several data items of one type in a matrix that can be accessed using indices. 6.2 Means We use the array construct to make a new type that is a list of fixed length of variables of an already available type, with integer indices. To use such a type involves three things. 1. Definition of the new type, an array of the desired type. Java provides variables of the new array type if [] is put behind the type of the desired items (see the example). 2. Declaration of a variable of the new type. (Step 1 and 2 are performed together.) 3. Creation of an array of the desired type. Java provides the new array type if [] is put behind the type of the desired items. The length, i.e., the number of elements, of the array is set here, by putting it between the []. After creation the length of an array cannot be changed anymore. The indexing starts with 0. Elements of an array are accessed by writing the array name and the index, say i: arrayname [i]. Each element is a variable of the given type, so all operations of that type can be performed on it, and also assignment. Provided with an array is the variable arrayname.length, that contains the length of the array. Note, that since the indexing starts at 0, the last index in the array is the length minus 1. As an example we write a program that stores numbers of type int in an array numbers. 41

import java.util. ; class ArrayDemo { // handles array of integer numbers Scanner sc = new Scanner(System.in); int[] numbers; // introduction of the type int[] // fills and prints array void demo(){ numbers = new int[5]; // creation of the array numbers // and declaration of the array numbers for (int i = 0; i < numbers.length; i++) { System.out.println( Type array element with index + i); numbers[i] = sc.nextint(); // initialization of the elements of numbers System.out.println( First array element is + numbers[0]); System.out.println( Array element with index 2 is + numbers[2]); System.out.println( Last array element is + numbers[numbers.length 1]); public static void main(string[] args) { new ArrayDemo().demo(); NB In the example we put the creation of the array in the method. It is also possible, to create the array outside the method, when the variable is declared: int[] numbers = new int[10]; Java does not allow to do declaration and creation as separate steps outside the method. Arrays of more dimensions are also possible in Java: for example, a 2-dimensional array of integers is defined as an array of arrays through int[][]. Creation then is split over several statements. First, a statement where between the first [] pair the number of rows is written, leaving the next [] pair empty. Second, statements for each row where between the next [] pair the number of columns for that row, i.e., the length of the row-array, is written. Numbering starts from the left-hand upper corner, top to bottom for rows (!), left to right for columns. Also, two dimensional arrays with rows of unequal length are possible: ragged arrays. The number of row-arrays is obtained through arrayname.length, the number of elements in each of the row-arrays i through arrayname [i].length. 6.3 Execution model The variable naming an array is, unlike the variables of the primitive types, a reference variable (explanation deferred to chapter??). An array in the object is depicted as follows. --------------- ----------- numbers -> ----------- 0 1 2 3 4 length 5 --------------- 42