Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement.

Size: px
Start display at page:

Download "Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement."

Transcription

1 1 Lecture 4 CS118 Term planner You should have done, or be working through, problem sheet 2; next is sheet 3 Coursework - common mistakes do while (robot.look(direction) = IRobot.WALL); if (robot.look(irobot.ahead) = IRobot.WALL) You are here If you want to get the coursework running on your home computer and are stuck talk to a friend who has got it working, or mail the CS118 Forum 4 Control flow: selection The simple if statement The order in which instructions are executed is central to imperative programming It is in controlling this order that we manage the order in which the side effects (e.g. assignment) take place This ultimately dictates the result of the code Selection statements in Java: if if switch Syntax: if (booleanexpression) Brackets optional if there is only one statement in the body if (booleanexpression) History: Introduced in Algol 60 If the booleanexpression evaluates to true, then the body of the if statement is executed, otherwise control passes to the next program statement after the if construct 7 8 The if statement The if statement cont Syntax: if (booleanexpression) Note the use of indentation Brackets optional if there is only one statement in the body if (booleanexpression) If the booleanexpression evaluates to true, then the body of the if statement is executed, otherwise control passes to the body of the part. Sequential control resumes at the next program statement after the if construct. Examples if ( robot.look(direction) == IRobot.WALL ) if (!(a>b) & (x>1) ) P 1 P

2 17 Nested if statements Nested if statements It is possible to nest ifs to any level if (percent > 80) if (percent > 70) grade = 'C'; if (percent > 60) grade = 'D'; grade = 'E'; Dangling problem if (x > 5) if ( y > 5) System.out.println( Both x and y are > 5 ); System.out.println( x <= 5 ); Style of indentation if (percent > 80) if It is possible to nest ifs to any level if (percent > 80) if (percent > 70) grade = 'C'; if (percent > 60) grade = 'D'; grade = 'E'; Dangling problem What gets printed when x = 5? if (x > 5) if ( y > 5) System.out.println( Both x and y are > 5 ); System.out.println( x <= 5 ); 19 Example: Truth table Dog s dinner II: Shortcut ifs Truth table for a logical or with two inputs Input A Input B Output false false false false true true true false true true true true Coded using if if (!InputA &!InputB) if (!InputA & InputB) if (InputA &!InputB) if (InputA & InputB) Factoring unnecessary cases if (!InputA &!InputB) Output = false; Output = false; Standard if statement if (x>0) y = 1; y = -1; Shortcut (dog s dinner) if statement y = (x>0)? 1 : -1; Just don t bother The switch statement The switch statement cont Suppose that there are several choices Some syntax rules: if (typesize == b ) store = 1; if (typesize == s ) store = 2; if (typesize == i ) store = 4; if (typesize == l ) store = 8; Java provides an alternative control structure switch (typesize) case b : store = 1; break; case s : store = 2; break; case i : store = 4; break; case l : store = 8; break; default : System.out.println( Invalid typesize ); switch (switchexpression) case value1 : s1; break; case value2 : s2; break; case value3 : s3; break; case valuen : sn; break; default : sd; The switchexpression must yield a value of type char, byte, short or int The value1 valuen must have the same datatype as the switchexpression case statements are evaluated top to bottom; The branch of a case that matches the result of the switchexpression will be evaluated; The break statement is optional; it will terminate the entire switch statement; The default statement is optional; if reached, its body will always be evaluated 25 29

3 40 Programming tricks and commonc mistakes Some languages (Modula 2, Ada, Fortran 90) allow you to compare ranges of values switch ( some value ) case 3..4 : second choice; case 1..2 : first choice; As Java (and C) require each value to have a separate label, you can simulate this as follows switch ( some value ) In this case the program falls through to the next case case 1 : case 2 : first choice; break; case 3 : case 4 : second choice; break; While the use (or not) of breaks (and defaults) seems like a good idea, often this will just lead to errors Consider the following example int i = 1; switch (i) case 0 : System.out.print( zero ); case 1 : System.out.print( one ); case 2 : System.out.print( two ); case 3 : System.out.print( three ); default : System.out.print( done ); This results in: one two three done This weird action is because case 2 onwards are discounted by the fact that the control flow of the program continues though the statements (to the right of the colon) until a break is reached or the switch terminates 46 and commonc mistakes You also need to be careful about the cases which your switch covers switch (typesize) case b : store =1; break; case s : store =2; break; case i : store =4; break; case l : store =8; What happens if typesize is f? Notice that there is no break statement here; this is OK Reasoning with choice statements Under what conditions will p4 be executed? if (a<0) if (b!=0) if (a==b) p3 if (a==0) p4 In some languages (Ada, Pascal) this causes a dynamic semantic error, in Java (and C and Fortran) we can get away with it Add preconditions Add preconditions cont condition : true condition: true.: true & (a<0) if (b!=0) if (a==b) p3 if (a==0) p4.: true & (a<0) if (b!=0).: true & (a>=0) & (b!=0) if (a==b) p3 if (a==0) p

4 53 condition: true condition: true.: true & (a<0) if (b!=0).: true & (a>=0) & (b!=0) if (a==b).: true & (a>=0) & (b==0) & (a==b) p3 if (a==0) p4.: true & (a<0) if (b!=0).: true & (a>=0) & (b!=0) if (a==b).: true & (a>=0) & (b==0) & (a==b) p3 if (a==0).: true & (a>=0) & (b==0) & (a!=b) & (a==0) p4 54 General rule for reasoning about if statements Reasoning about if statements if (guard) & guard S1 Combining the precondition and guard logic allows us to strengthen the precondition of the guarded statements in S1 if (guard) & guard S1 &!guard S Reasoning about switch statements case v1 : & (e==v1) statement1; break; case v2 : & (e!=v1) & (e==v2) statement2; break; case v3 : & (e!=v1) & (e!=v2) & (e==v3) statement3; break; case vn : & (e!=v1..v(n-1)) & (e==vn) statementn; break; default : & (e!=v1..vn) statement for default; Missing break: alters precondition case v1 : & (e==v1) statement1; case v2 : & ((e==v1) (e==v2)) statement2; break; Missing default: pre => (e==v1) (e==v2) / case v1 : & (e==v1) statement1; break; case v2 : & (e!=v1) & (e==v2) statement2; since if neither case were true, Java does nothing 57 58

5 59 Pragmatism This technique will not always be applied When should I use it? When the logic is complicated When there can be no question as to the result When your programs fail

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

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

More information

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

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements Evolution: - FORTRAN I control statements were based directly on IBM 704 hardware - Much research and argument

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 03: Control Flow Statements: Selection Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad Noor

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

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

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1

More information

In this lab, you will learn more about selection statements. You will get familiar to

In this lab, you will learn more about selection statements. You will get familiar to Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write

More information

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

Following is the general form of a typical decision making structure found in most of the programming languages: Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined

More information

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3) if Statement Flow of Control: Branching (Savitch, Chapter 3) TOPICS Conditional Execution if and Statement Boolean Data switch Statement Ensures that a statement is executed only when some condi5on is

More information

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3) if Statement Flow of Control: Branching (Savitch, Chapter 3) TOPICS Conditional Execution if,, and if boolean data switch statements CS 160, Fall Semester 2015 1 Programs o-en contain statements that may

More information

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar 2 Lecture 2 CS118 Term planner For your first seminar Meet at CS reception Bring The Guide Bring your CS account details Finish the problem sheet in your own time Talk to each other about the questions

More information

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

Flow of Control. Flow of control The order in which statements are executed. Transfer of control 1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

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

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

Selections. CSE 114, Computer Science 1 Stony Brook University

Selections. CSE 114, Computer Science 1 Stony Brook University Selections CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation If you assigned a negative value for radius in ComputeArea.java, then you don't want the

More information

BM214E Object Oriented Programming Lecture 4

BM214E Object Oriented Programming Lecture 4 BM214E Object Oriented Programming Lecture 4 Computer Numbers Integers (byte, short, int, long) whole numbers exact relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often

More information

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool Chapter 3: Selections and Conditionals CS1: Java Programming Colorado State University Motivations If you assigned a negative value for radius in Listing 2.2, ComputeAreaWithConsoleInput.java, the program

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements Programming Languages Third Edition Chapter 9 Control I Expressions and Statements Objectives Understand expressions Understand conditional statements and guards Understand loops and variation on WHILE

More information

Chapter 3 Selection Statements

Chapter 3 Selection Statements Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are

More information

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching 1 Flow of Control The order in which statements in a program are executed is called the flow of control So far we have only seen

More information

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

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths. Loop Control There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first,

More information

Selection Statements. Pseudocode

Selection Statements. Pseudocode Selection Statements Pseudocode Natural language mixed with programming code Ex: if the radius is negative the program display a message indicating wrong input; the program compute the area and display

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011 Java Tutorial Ashkan Taslimi Saarland University Tutorial 3 September 6, 2011 1 Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements 2 Review Programming Style and Documentation

More information

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009 CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II.

More information

Flow of Control Execution Sequence

Flow of Control Execution Sequence Flow of Control Execution Sequence Sequence sequence Decision tree Repetition graph Language Constructs sequence statements (incl Foc) simple / compound decision (1 /2 / n way) if then [else] case / switch

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

CS112 Lecture: Making Choices

CS112 Lecture: Making Choices CS112 Lecture: Making Choices Objectives: Last revised 1/19/06 1. To review the Java if and if... statements 2. To introduce relational expressions and boolean operators 3. To discuss nested if statements

More information

Logical Operators and switch

Logical Operators and switch Lecture 5 Relational and Equivalence Operators SYS-1S22 / MTH-1A66 Logical Operators and switch Stuart Gibson sg@sys.uea.ac.uk S01.09A 1 Relational Operator Meaning < Less than > Greater than

More information

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence Expression Evaluation: Outline CSCI: 4500/6500 Programming Languages Control Flow Chapter 6 Infix, Prefix or Postfix Precedence and Associativity Side effects Statement versus Expression Oriented Languages

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #16: Java conditionals/loops, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Midterms returned now Weird distribution Mean: 35.4 ± 8.4 What

More information

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015 COMP 110-001 Flow of Control: Branching 2 Yi Hong May 19, 2015 Review if else Q1: Write a program that Reads an integer from user Prints Even if the integer is even Otherwise, prints Odd Boolean expression

More information

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure

More information

Review. Relational Operators. The if Statement. CS 151 Review #4

Review. Relational Operators. The if Statement. CS 151 Review #4 Review Relational Operators You have already seen that the statement total=5 is an assignment statement; that is, the integer 5 is placed in the variable called total. Nothing relevant to our everyday

More information

Datatypes, Variables, and Operations

Datatypes, Variables, and Operations Datatypes, Variables, and Operations 1 Primitive Type Classification 2 Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit

More information

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences Lecture 5 Selection Statements Review from last week cin and cout directives escape sequences member functions formatting flags manipulators cout.width(20); cout.setf(ios::fixed); setwidth(20); 1 What

More information

Ch. 7: Control Structures

Ch. 7: Control Structures Ch. 7: Control Structures I. Introduction A. Flow of control can be at multiple levels: within expressions, among statements (discussed here), and among units. B. Computation in imperative languages uses

More information

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

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements CS 199 Computer Programming Spring 2018 Lecture 5 Control Statements Control Structures 3 control structures Sequence structure Programs executed sequentially by default Branch structure Unconditional

More information

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions ICOM 4036 Programming Languages Ch7. Expressions & Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment

More information

Lecture 9: Control Flow

Lecture 9: Control Flow Programming Languages Lecture 9: Control Flow Benjamin J. Keller Department of Computer Science, Virginia Tech Programming Languages Control Flow 2 Command Overview Assignment Control Structures Natural

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Motivations If you assigned a negative value for radius

More information

Topics. Chapter 5. Equality Operators

Topics. Chapter 5. Equality Operators Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

More information

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops COMP 110-003 Introduction to Programming If-Else Statement, Switch Statement and Loops February 5, 2013 Haohan Li TR 11:00 12:15, SN 011 Spring 2013 Announcement Office hour is permanently changed Wednesday,

More information

CONDITIONAL EXECUTION

CONDITIONAL EXECUTION CONDITIONAL EXECUTION yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Conditional Execution if then if then Nested if then statements Comparisons

More information

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

5. Selection: If and Switch Controls

5. Selection: If and Switch Controls Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

More information

CompSci 125 Lecture 11

CompSci 125 Lecture 11 CompSci 125 Lecture 11 switch case The? conditional operator do while for Announcements hw5 Due 10/4 p2 Due 10/5 switch case! The switch case Statement Consider a simple four-function calculator 16 buttons:

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures More Examples Lecture 24: More Scala CSC 131 Fall, 2014 Kim Bruce MyList, MyArrayList, SinglyLinkedList - Val vs var - Can create Array[T] (unlike Java), though need implicit ClassManifest - foreach(f)

More information

CS Week 5. Jim Williams, PhD

CS Week 5. Jim Williams, PhD CS 200 - Week 5 Jim Williams, PhD The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to teach it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations) Decision Structures if, if/ conditions Selection DECISION: determine which of 2 paths to follow (1+ statements in each path) CS1110 - Kaminski (ELSE path optional) 2 Selection options (in Java) Plain if

More information

The Java language has a wide variety of modifiers, including the following:

The Java language has a wide variety of modifiers, including the following: PART 5 5. Modifier Types The Java language has a wide variety of modifiers, including the following: Java Access Modifiers Non Access Modifiers 5.1 Access Control Modifiers Java provides a number of access

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Conditional Execution Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Conditional Execution 1 / 14 Structured Programming In reasoning

More information

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title Flow of Control Branching 2 Cheng, Wei COMP110-001 May 19, 2014 Title Review of Previous Lecture If else Q1: Write a small program that Reads an integer from user Prints Even if the integer is even Otherwise,

More information

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

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Motivation In the programs we have written thus far, statements are executed one after the other, in the order in which they appear. Programs often

More information

Statement level control structures

Statement level control structures 1 Statement level control structures CS 315 Programming Languages Pinar Duygulu Bilkent University Control Statements: Evolution 2 FORTRAN I control statements were based directly on IBM 704 hardware Much

More information

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures Chapter 4: Control Structures I Java Programming: Program Design Including Data Structures Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 4 Loops 1 Motivations Suppose that you need to print a string (e.g., "Welcome to Java!") a hundred times. It would be tedious to have to write the following statement a hundred times: So, how do

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals What are Computers Really Good at? Complex calculations Repetitive tasks Identifying repetition is key to many programming

More information

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions CMSC 330: Organization of Programming Languages OCaml Expressions and Functions CMSC330 Spring 2018 1 Lecture Presentation Style Our focus: semantics and idioms for OCaml Semantics is what the language

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

Control Structure: Selection

Control Structure: Selection Control Structure: Selection Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure Selection Structure Single

More information

3chapter C ONTROL S TATEMENTS. Objectives

3chapter C ONTROL S TATEMENTS. Objectives 3chapter C ONTROL S TATEMENTS Objectives To understand the flow of control in selection and loop statements ( 3.2 3.7). To use Boolean expressions to control selection statements and loop statements (

More information

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives LIANMC03v3_0132221586.QXD 5/15/06 7:41 PM Page 67 CCHAPTER HAPTER 3 1 SELECTION STATEMENTS Objectives To declare boolean type and write Boolean expressions ( 3.2). To distinguish between conditional and

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Chapter 8 Statement-Level Control Structures

Chapter 8 Statement-Level Control Structures Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence rules, was discussed. This chapter discusses

More information

CHAPTER 5 FLOW OF CONTROL

CHAPTER 5 FLOW OF CONTROL CHAPTER 5 FLOW OF CONTROL PROGRAMMING CONSTRUCTS - In a program, statements may be executed sequentially, selectively or iteratively. - Every programming language provides constructs to support sequence,

More information

THE JAVA FOR STATEMENT

THE JAVA FOR STATEMENT THE JAVA FOR STATEMENT The for statement behaves as a while statement. Its syntax visually emphasizes the code that initializes and updates the loop variables. for ( init; truth value; update ) statement

More information

Logic & program control part 3: Compound selection structures

Logic & program control part 3: Compound selection structures Logic & program control part 3: Compound selection structures Multi-way selection Many algorithms involve several possible pathways to a solution A simple if/else structure provides two alternate paths;

More information

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 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

More information

Computer Programming, I. Laboratory Manual. Final Exam Solution

Computer Programming, I. Laboratory Manual. Final Exam Solution Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Final Exam Solution

More information

All about flow control

All about flow control All about flow control Prof. Zhang March 11, 2014 1 Read Coding Style Guideline Please go to the class website, find the resource part, click on the how labs are graded? link. Read the guideline, and then

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

More information

5 The Control Structure Diagram (CSD)

5 The Control Structure Diagram (CSD) 5 The Control Structure Diagram (CSD) The Control Structure Diagram (CSD) is an algorithmic level diagram intended to improve the comprehensibility of source code by clearly depicting control constructs,

More information

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

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS CHAPTER DECISIONS 3 Chapter Goals To implement decisions using the if statement To compare integers, floating-point numbers, and Strings To write statements using the Boolean data type To develop strategies

More information

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science Control Flow COMS W4115 Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science Control Flow Time is Nature s way of preventing everything from happening at once. Scott identifies

More information

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers. Control Flow COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Control Flow Time is Nature s way of preventing everything from happening at once. Scott

More information

Chapter 3: Decision Structures

Chapter 3: Decision Structures Chapter 3: Decision Structures Chapter Topics Chapter 3 discusses the following main topics: The if Statement The if-else Statement Nested if statements The if-else-if Statement Logical Operators Comparing

More information

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming Decisions, Decisions, Decisions GEEN163 Introduction to Computer Programming You ve got to be very careful if you don t know where you are going, because you might not get there. Yogi Berra TuringsCraft

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #8: More on Conditional & Loop Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements:

More information

Flow of Control. 2.1 The if Statement

Flow of Control. 2.1 The if Statement Flow of Control 2 In almost any computer program written for a scientific computing application, we need to allow the computer to execute a collection of statements if and only if some criterion is met.

More information

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators Flow of Control of Program Statements CS 112 Introduction to Programming (Spring 2012) q Java provides two types of program flow of control statements: decision statements, or conditional statements: decide

More information

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction CSc 520 Principles of Programming Languages 26 : Control Structures Introduction Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

More information

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Flow Control. Key Notion. Statement Categories. 28-Oct-10 Boaz Kantor Introduction to Computer Science, Fall semester 2010-2011 IDC Herzliya Flow Control Raistlin: This alters time. Astinus: This alters nothing...time flows on, undisturbed. Raistlin: And carries

More information