COMP6700/2140 Control Flow

Similar documents
Control Structures in Java if-else and switch

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Control Structures in Java if-else and switch

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

COMP6700/2140 Data and Types

COMP6700/2140 Operators, Expressions, Statements

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Introduction. C provides two styles of flow control:

Warmup : Name that tune!

COMP6700/2140 Methods

Flow Control. CSC215 Lecture

COMP6700/2140 Code as Data

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

6. Operatoren. 7. Safe Programming: Assertions. Table of Operators. Table of Operators - Explanations. Tabular overview of all relevant operators

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Computer Programming I - Unit 5 Lecture page 1 of 14

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Repetition CSC 121 Fall 2014 Howard Rosenthal

COMP6700/2140 Abstract Data Types: Queue, Set, Map

CONDITIONAL EXECUTION: PART 2

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

CMPT 125: Lecture 4 Conditionals and Loops

G Programming Languages - Fall 2012

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

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

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Example: Monte Carlo Simulation 1

Repetition Structures

Control Flow Statements

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Flow of Control. Chapter 3. Chapter 3 1

COMP6700/2140 Abstract Data Types: Lists and Iteration

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Material from Recitation 1

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Controls Structure for Repetition

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Chapter 17. Iteration The while Statement

5. Control Statements

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

COMP6700/2140 Packages, Modules and Jigsaw

C++ Programming Lecture 1 Software Engineering Group

COMP 202 Java in one week

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

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

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Lecture 7 Tao Wang 1

COMP6700/2140 Recursive Operations

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Following is the general form of a typical decision making structure found in most of the programming languages:

Chapter 8. Statement-Level Control Structures

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Theory of Programming Languages COMP360

Programming Lecture 4

Lecture 10. Daily Puzzle

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

COMP6700/2140 Objects

COMP6700/2140 Std. Lib., I/O

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Chapter 4: Making Decisions

Chapter 8 Statement-Level Control Structure

Repetition with for loops

COMP 202 Java in one week

COMP6700/2140 Generic Methods

Chapter 4: Making Decisions

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 8 Statement-Level Control Structures

LECTURE 04 MAKING DECISIONS

Java Programming: from the Beginning. Chapter 8 More Control Structures. CSc 2310: Principle of Programming g( (Java) Spring 2013

COMP 202. Java in one week

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

The Basic Parts of Java

CMPT 125: Lecture 3 Data and Expressions

Conditionals and Loops

1.00 Lecture 5. Floating Point Anomalies

CSCI 1061U Programming Workshop 2. C++ Basics

Java Coding 3. Over & over again!

COS 126 Written Exam 2 (Spring 2015)

Programming Lecture 4

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Text Input and Conditionals

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

3 The L oop Control Structure

System Correctness. EEC 421/521: Software Engineering. System Correctness. The Problem at Hand. A system is correct when it meets its requirements

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Transcription:

COMP6700/2140 Control Flow Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU 3 March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 1 / 16

Control Flow: if-else We often want to execute different code paths depending on the data For boolean conditions, use the if-then-else statement with the syntax: if (b) // b is a boolean expression statement_1; else // this part is optional, it covers all logic alternatives to b,!b statement_2; For more complex cases of logical alternatives the extended form if-[else if]*-else is used: if (b1) statement_1; else if (b2) statement_2; else if (b3) statement_3; else // the remaining alternatives go!(b1 b2 v b3) statement; Branches are old hat: modern languages (Elixir, Rust) use match statements to achieve the same result but in more expressive manner Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 2 / 16

Dangling else (Source of Logical Errors) Conditionals can nest: if-else statement can be a part of a statement_i One should be careful to avoid the dangling else error when a conditional statement is executed at the wrong level (see a complete example in DanglingElsesjava) if (richter >= 0) if (richter <= 4) Systemoutprintln("The earthquake is harmless"); else // pitfall! This is a part of inner if-statement Systemoutprintln("Negative value not allowed"); Don t rely on indentation Java is not Python; use braces { even when the statement is a one-liner; To create an expression which depends on a condition, use the ternary operator: coursecode = undergraduate? "COMP2140" : "COMP6700"; When nesting gets three and more level deep consider code refactoring If a condition is written as complex boolean expression (involving three or more operands) also consider refactoring Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 3 / 16

Control Flow: switch If there are too many else-ifs in a situation when the boolean expression is an equality test for integer values, int_expr==valuei, i=1,2, k,, the switch statement can be used: int var = int_expr; //can be number type, or Enum (and String in JDK 7) switch (var) { case value_1: //this is a statement label statement_1; // label is just a label, not a condition for execution! break; // to prevent execution of default_statement case value_2: case value3: statement_23; // multiple case labels may require the same statement break; case value_k: // not all range of values needs to be explicitly checked statement_k; default: default_statement; // flow control jumps here if no matching case found default is // optional: in its absence, the entire switch statement is skipped The case or default labels do not force the break out of the switch, and do not imply the end of execution of statements The falling through can be avoided explicitly by break statement, or, if the switch is used inside a method which returns a value, by return Some advise not to use switch, but it may be useful for processing multiple command-line options For traps of the control flow falling through in switches see IfTestjava Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 4 / 16

Control Flow: while and do-while Need to repeat the same statement multiple times depending on a value of the boolean expression, and the number of repetitions isn t known in advance (indeterminate loop)? Use either a while loop: while (boolean-expr)// if boolean-expr evaluates to true, then execute statement; or, to execute the statement at least once, no matter what, use a do-while loop: do statement; while (boolean-expr); statement (especially in do-while case) is almost always a block class WhileDemo { public static void main(string[] args){ int count = 1; while (count < 11) { Systemoutprintln("Count is: " + count); count++; Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 5 / 16

while and do-while: Diagrams Execute zero or a few times until the loop condition is broken while-loops are used 100 more often Execute once before examining the loop condition May make the programming logic simpler, yet is equivalent to while-loop Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 6 / 16

Control Flow: for When the number of repetitions is known in advance (determinate loop), it can be controlled by a counter, most often, when the looping is done over a range of values, like an array elements, from beginning to end, usage of for loop is recommended: for (init-expr; boolean-expr; incr-expr) // all expressions are optional statement; which is equivalent to { //mind this block! int-expr; while (boolean-expr) { statement; incr-expr; // it often has that (in/dec)cremental form i++ / j-- There is also a simplified form of the for-loop, so called for-each loop (though there is no such keyword), when iterarion is performed over an collection type object (some sort of container, details will be in Block 4, arrays are regarded as collections): for (ElementType var: container) // container can be an array statement; Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 7 / 16

Control Flow: break and continue break is often important to control the exit from a loop or a block: while (years <= 100) { balance += payment; double interest = balance * interestrate/100; balance += interest; if (balance >= goal) break; //at this moment, while loop is exited years++; Systemoutprintln("No of years = " + years); continue: instead of breaking out of the loop, it only skips remaining statements in a current iteration and goes to the next one while (sum < goal) { String input = ; n = IntegerparseInt(input); if (n < 0) continue; sum +=n; // not executed if n<0 for (count=0; count < 100; count++) { String input = JOptionPane; n = IntegerparseInt(input); if (n < 0) continue; //jump to count++ sum += n; // not executed if n < 0 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 8 / 16

Structured Programming and SE/SE principle A version of break (and continue) with a label transfers the control flow to the statement immediately outside the labeled statement If such statement is a nested block (eg a loop inside a loop), a labelled break which terminates the innermost block transfers the control flow to the statement outside the outmost block (one can escape nested loops at once), while an ordinary (not labelled) break in the inner block merely transfers the control to the next-level block label_one: for (int i = 0; i < 99; i++) { break label_one When labeled break and continue are used in multi-nested blocks, they violate the basic tenet of structured programming known as Single Entry Single Exit principle: Every block of code must have a single entry point and a single exit point It should be impossible to enter or exit such a block in the middle Entry is at the top, exit is at the bottom (like in the program text) Labeled break allows you to exit deeply nested blocks (like double for-loop) from the middle This is bad because the enclosing blocks (outer loop) may not know that they are being exited; if such outer blocks assume that they control their execution and exit (which is normal), this can lead to errors which will be hard to pinpoint The example Labeledjava If possible, avoid even simple break and continue Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 9 / 16

Control Flow: return return statement terminates execution of a method and returns to the invoker double nonnegative(double val) { if (val < 0) return 0; //an int constant, but it's promoted to double else return val; // a double Even if the methods does not return a value, a simple return; will terminate the method execution (leaving whatever is left out of execution): void reportifnegative(double val) { if (val < 0) { Systemoutprintln("It's negative, alright"); return; Systemoutprintln("This will not be seen if val < 0"); Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 10 / 16

Principles and Practice All rules and principles of programming (except for very few) should not be followed dogmatically Sometime these rules contradict each other, and one has to choose For example: a return statement can be used more than once inside a method; it formally contradicts the (second part of) SESE principle, but this is an acceptable practice used to avoid executing unnecessary code: // this method is used in implementation of the quick sort algorithm: it // determines which one of the three int's --- a, b and c --- is in between public static int median_of_3(int a, int b, int c) { if (a < b) { if (b < c) return b; // stop at once when result is found else if (a < c) return c; else return a; else if (a < c) return a; else if (b < c) return c; else return b; Since quick sort is used extremely extensively in many(-many) software libraries throughout the virtual world, exerting the utmost efficiency is of paramount importance Out of the window flies the Single Exit (the second half of SESE) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 11 / 16

Why Branches and Loops? Turing Machine and Universal Computation Executing branches and making repetitions (often without a priori knowledge how many times) are the fundamental parts of computation They are present in a theoretical model of universal computation known as Turing Machine (Picture is courtesy of D Evans) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 12 / 16

Computation and Its Model Courtesy Jin Wicked Simple Turing Machine Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 13 / 16

Finiteness of means when dealing with (potential) infinity A lovely discussion can be found at What Every Hacker Should Know about Theory of Computation (don t be put off by the name hacker this lecture was aimed at a curious high-scholl student) Alan Turing analysed the mental process of computation by ruthlessly simplifying and removing everything which cannot be described with complete certainty necessary for performing the same process by a machine Success of this simplification is owned to both luck and Turing s ingenuity The result is Turing Machine a finite-state controller with a finite set of rules ( program ) which operates on an arbitrary large data set described by a finite set of symbols ( alphabet ) The Machine can solve any problem if that problem has a solution obtainable in finine number of steps (the latter is not not garanteed Halting Problem ) The key point is our (unexplained) ability to understand the infinite reality using a finite apparatus Existence (and discoverability) of Laws of Nature provides (indirect) support to the Church-Turing Thesis Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 14 / 16

Computability, Turing Machine and Loop/Recursions The Turing Machine is instructed to move the position of its controller unit in accordance with the program (instruction table stored in the controller) and the data it reads on the tape Universal Turing Machine gets its program from the same data tape (before it starts operation on real data) There is no preprogrammed condition that the Machine should perform only finite set of operations Turing proved that it is impossible to determine whether the Machine will ever stop or go on forever (the Halting Problem) In practical applications of a computation model is: a processor ( Turing Machine ) a program input data We have the control the termination either occurs (IT practitioners work with a subset of problems where halting and undecidability issues do not occur), or we pull the switch ( ) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 15 / 16

Further Reading Core Java for the Impatient, Ch 17 Control Flow Statements in Java Tutorial Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Control Flow 3 March 2017 16 / 16