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

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

Flow Control. CSC215 Lecture

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

Conditionals and Loops

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

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

1007 Imperative Programming Part II

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

QUIZ. What is wrong with this code that uses default arguments?

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

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

More Programming Constructs -- Introduction

CSCI 1061U Programming Workshop 2. C++ Basics

Scope of this lecture. Repetition For loops While loops

Introduction. C provides two styles of flow control:

5. Control Statements

Program Elements -- Introduction

Control Structures in Java if-else and switch

Java Bytecode (binary file)

Loops. CSE 114, Computer Science 1 Stony Brook University

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Text Input and Conditionals

Chapter 3: Operators, Expressions and Type Conversion

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

Visual C# Instructor s Manual Table of Contents

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Control Structures in Java if-else and switch

Introduction to C++ with content from

BRANCHING if-else statements

3 The L oop Control Structure

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

3. Java - Language Constructs I

Introduction to Programming Using Java (98-388)

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

These are notes for the third lecture; if statements and loops.

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Topics. Chapter 5. Equality Operators

Chapter 3 Selection Statements

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

BM214E Object Oriented Programming Lecture 4

Decision Making in C

Software Design & Programming I

Repetition Structures

Learning to Program with Haiku

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

CS50 Supersection (for those less comfortable)

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Chapter 3. Selections

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

(Refer Slide Time: 00:26)

Basics of Java Programming

C Language Programming

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 4: Conditionals and Loops

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Java Primer 1: Types, Classes and Operators

Pace University. Fundamental Concepts of CS121 1

ECE 122 Engineering Problem Solving with Java

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Quick Reference Guide

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Java enum, casts, and others (Select portions of Chapters 4 & 5)

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

PROGRAMMING FUNDAMENTALS

REPETITION CONTROL STRUCTURE LOGO

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CGS 3066: Spring 2015 JavaScript Reference

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

STUDENT LESSON A12 Iterations

Chapter Goals. Contents LOOPS

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Quiz 1: Functions and Procedures

CS11 Java. Fall Lecture 1

Chapter 13 Control Structures

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 2, Part III Arithmetic Operators and Decision Making

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

Operators. Java operators are classified into three categories:

Variables and literals

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Review of the C Programming Language for Principles of Operating Systems

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

Full file at

Compiler Theory. (Semantic Analysis and Run-Time Environments)

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Transcription:

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

Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an integer and add it to any other 32 bits in memory. It is up to the programmer to keep track of what he is keeping where. true i x 2 5.5F p

Why Types? If we added i and f on the previous slide, we would hope to get 7.5, or 7, or 8. But if we treat the bits of x like an int, we get: i + x = 1085276162 Java won t let us make that kind of mistake. Type checking finds some of our ugliest mistakes at compile time.

Char: A Special Integer In some ways char behaves like an integer. We can do some useful arithmetic with char values. int n = 7-0 ; /* n = 7 */ char c = R - ( A - a ) ; /* c = r */ Note: 0-9 form a sequence in the Unicode character set, as do a-z and A-Z.

Bitwise NOT The bitwise NOT operator inverts every bit of an integer value. 0xFF = 0x00 0x01 = 0xFE 0x74 = 0x8B

The Conditional Operator The conditional operator is ternary it takes three operands. The first operand is a boolean value, the second is the result of the expression if the boolean is true, the third is the result if the boolean is false. boolean a = true, b = false ; int i = a? 0 : 1 ; /* i = 0 */ int j = b? 5 : 2 ; /* j = 2 */ int k = i > j? i-j : j-i ; /* k = j-i = 2 */

Conversions In general, a variable can be implicitly converted to any type that is wider (i.e., has more bits). Integers can always be converted to floating-point. Floating-point numbers must be explicitly cast to integers. byte b = 0 ; int i = 1 ; double x = 6.2 ; i = b ; /* OK: i=0 */ i = (int) x ; /* OK: i=6 */ x = i ; /* OK: x=6.0 */

Type Casting The type cast operator (type) has very high precedence. Carelessness can lead to premature truncation. double x = 5.5 ; int i = 4 ; int a = (int) x * i ; /* a=20 */ int b = (int)( x * i ) ; /* b=22 */

Operator Precedence We ve now talked about 45 of Java s 48 operators. I can t possibly fit a full precedence chart on this slide. See Appendix D in the book for a full listing. You should learn and trust Java s operator precedence rules. Parenthesize when necessary and when it makes your code clearer, but try to avoid over-paranthesization. a = b*c + d*e Is better than a = (((b)*(c)) + ((d)*(e)))

Symbolic Constants Sometimes we want to give a name to a value that will not change. The final keyword allows us to assign a variable a value exactly once. final double PI = 3.141592653589793 ; final int MAX VALUE = 55 ; PI += 1 ; /* Error: can t modify PI */

If-Then Sometimes we only want to execute certain statements if a condition holds. The if statement evaluates a boolean expression and only executes the next statement if the expression is true. if( a >= b ) max = a ; if( a < b ) max = b ; We use indentation to make clear which statement is affected by the if.

If-Then-Else If we also want to do something when the condition is false, we can use an optional else. The previous example becomes: if( a >= b ) max = a ; else max = b ; max = b gets executed whenever a >= b is not true.

If-Then-Else vs.?: The previous example could also be written: int max = a >= b? a : b ; The conditional operator is an expression that has a value. if-then-else is a control-flow statement it changes the execution path, but it does not have a value. If the only thing an if-then-else does is set a variable, we can usually rewrite it using the conditional operator. Whether we choose to do so is a matter of style and preference.

The If-Then-Else Ambiguity What happens if we have two if statements and only one else? Which if does it apply to? n = 0 ; if( a > b ) if( a > c ) n = 1 ; else n = 2 ; What happens if a <= b? What if a > b && a <= c?

The If-Then-Else Ambiguity: 2 We resolve this ambiguity be associating every else with its nearest if. n = 0 ; if( a > b ) if( a > c ) n = 1 ; else n = 2 ; n = 2 is only executed if a > b && a <= c.

Blocks If we want to execute more than one statement as the result of a conditional, we can put a list of statements inside curly braces to form a block. if( num >= 0 ) { total += num ; pos++ ; } else { total -= num ; neg++ ; }

Blocks: 2 Blocks help resolve the if-then-else ambiguity: n = 0 ; if( a > b ) { if( a > c ) n = 1 ; else n = 1 ; } Now there is no question which if the else belongs to.

Scope A scope is the area of a program where a variable declaration is visible (i.e., where you can use the variable). An identifier can be associated with only one variable in each scope. Variables declared at the top-level of the main method have method scope. int i = 0 ; if( a < b ) { int i = 1 ; /* Error: i is already defined */... }

Block Scope Every block has its own scope. Variables with method scope are visible within the block, but variables declared inside the block are not visible outside it. They have block scope. if( a < b ) { int i = 1 ;... } else { int i = 2 ; /* OK: i above is out of scope */... } i++ ; /* Error: i is out of scope */

Else If We can chain a series of mutually exclusive conditions using else if. if( n == 0 ) {... } else if( n == 1 ) {... } else if( n == 2 ) {... } else if( n == 3 ) {... } else {... } The final else executes only if every if expression fails.

The Switch Statement When the execution path is governed by the value of an integer expression, the switch statement gives us a more compact syntax that cascading if-then-else statements. switch( n ) { case 0:... case 1:... case 2:... case 3:... default:... }

The Switch Statement: 2 Each case label must be an integer or character constant. If the value of the switch expression matches a case label, control transfers to the statement following the label. Execution ends when a break statement is encountered. If no case label matches, control transfers to the statement following the default label. If no case label matches and there is no default label, the entire switch statement is skipped.

The Break Statement Execution of a case is terminated by a break statement, not the next case. This can lead to subtle bugs: int n = 0 ; switch( a ) { case 0: n = 10 ; break ; case 1: n = 20 ; break ; case 2: n = 30 ; /* No break here */ case 3: n = 40 ; break ; /* If a==2, n=40 */ }

The Break Statement: 2 This fall-through behavior isn t just a source of bugs. It can be useful. switch( digit ) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : val = digit - 0 ; break ; } case A : case B : case C : case D : case E : case F : val = digit - A + 10 ; break ;

The While Statement If we want to repeat a statement or block an indefinite number of times, we can use the while statement. while evaluates a boolean expression and repeats its body until the expression becomes false. while( i < length ) {... i++ ; } while( keep playing ) {... }

Infinite Loops If the expression that controls a loop never becomes false, the program will enter an infinite loop. Here s a trivial example: while( true ) {... } If the program enters an infinite loop, the user will have to forcibly quit it (Ctrl-C does this in Unix). Be careful that your loop conditions are falsifiable and any event you are waiting for can actually occur.

The Do-While Statement If the boolean expression controlling a while statement is false the first time, the body of the while never executes. If you want the body to execute at least once, you can use do-while. boolean cond = false ; do { /* statement body */... } while( cond ) ; Note: there must be a semicolon after the while part.

The For Statement If we want to loop over a range of values, we can use the for statement. Here s a loop that counts from 1 to 10: for( int i=1 ; i<=10 ; i++ ) System.out.println("i="+i) ; The for statement is slightly more complicated than other statements we ve seen so far. Let s look at it components one at a time.

For: Initialization The first component of the for statement is the initialization expression. It may contain any expression at all, but we usually just use it to set up a loop control variable. for( int i=1 ; i<=10 ; i++ ) System.out.println("i="+i) ; Notice that the declaration of i is within the scope of the statement. This prevents the outer scope from getting cluttered with lots of little temporary variables.

For: Loop Condition The second component of the for statement is the loop condition. It must be a boolean expression. The loop executes until the loop condition becomes false. for( int i=1 ; i<=10 ; i++ ) System.out.println("i="+i) ; The loop condition is checked before every iteration of the loop, including the first.

For: Increment Expression The last component of the for statement is the increment expression. It may contain any expression at all, but it should always modify the loop control variable. for( int i=1 ; i<=10 ; i++ ) System.out.println("i="+i) ;

For: Examples You don t have to count by one: for( int i=0 ; i < 10 ; i+=2 ) System.out.println("i="+i) ; You don t have to count up: for( int i=10 ; i > 0 ; i-- ) System.out.println("i="+i) ; And you can have more than one counter: for( int i=0, j=10 ; j > 0 ; i++, j-- ) System.out.println("i="+i+" j="+j) ;

Nested Loops A loop can occur inside another loop. The inner loop will execute completely in every iteration of the outer loop. int count i = 0 ; int count j = 0 ; for( int i=0 ; i < 10 ; i++ ) { count i++ ; for( int j=0 ; j < 10 ; j++ ) count j++ ; } /* count i=10, count j=100 */