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

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

COMP 110 Introduction to Programming. What did we discuss?

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

LAB 4.1 Relational Operators and the if Statement

COMP Primitive and Class Types. Yi Hong May 14, 2015

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Chapter 3 Selection Statements

Chapter 4: Control structures. Repetition

Darrell Bethea May 25, 2011

Chapter 4: Control structures

LAB 5: SELECTION STATEMENTS

Control Structures in Java if-else and switch

CS 112 Introduction to Programming

CS 112 Introduction to Programming

COMP 110 Introduction to Programming. What did we discuss?

LECTURE 04 MAKING DECISIONS

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

CS111: PROGRAMMING LANGUAGE II

More Complex Versions of the if Statement. Class 13

Decision Making in C

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

Logic & program control part 3: Compound selection structures

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

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

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

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Programming in the Small II: Control

Loops (while and for)

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

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

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Programming with Java

Eng. Mohammed S. Abdualal

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Selection Statement ( if )

Chapter 4: Making Decisions

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

Chapter 4: Making Decisions

CS112 Lecture: Making Choices

Selections. CSE 114, Computer Science 1 Stony Brook University

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

CS 112 Introduction to Programming

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

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

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

BRANCHING if-else statements

Lesson 39: Conditionals #3 (W11D4)

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Lecture 2: Intro to Java

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

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

Lecture 2: Intro to Java

Flow of Control: Programs can control the order in which their instructions are executed. Four types of flow:

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

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

Introduction to Programming

Lecture 6. Drinking. Nested if. Nested if s reprise. The boolean data type. More complex selection statements: switch. Examples.

Building Java Programs

Control Structures in Java if-else and switch

CS171:Introduction to Computer Science II

Conditionals !

conditional statements

Getting started with Java

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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

Building Java Programs

Midterm Review Session

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

Decision Structures. Chapter 4

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

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Topics. Chapter 5. Equality Operators

CompSci 125 Lecture 11

Building Java Programs

Lecture 5 Tao Wang 1

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

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

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

COMP 202 Java in one week

Quiz Determine the output of the following program:

Object-Oriented Programming

Principles of Computer Science

Chapter Goals. Contents LOOPS

CS111: PROGRAMMING LANGUAGE II

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

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

CONDITIONAL EXECUTION

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

CS171:Introduction to Computer Science II

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Transcription:

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, prints Odd Boolean Expression & Comparison Q2 How to compare values of primitive types? How to compare objects? Q3: Write out the boolean expression for testing a leap year The year is evenly divisible by 4; If the year can be evenly divided by 100, it is NOT a leap year, unless; The year is also evenly divisible by 400. Then it is a leap year.

Gotcha == var1 = var2 (assignment statement) Error!!!!!!! var1 == var2 (boolean expression) Do NOT use == to compare Strings string1 == string2 //BAD string1.equals(string2); //GOOD 3

Gotcha (Syntax) if (boolean expression); DO NOT DO THIS!!!!!!! if (boolean expression) {. else { // NO MORE boolean expression here.

Today s Contents Nested if else

More choices So far we have been working on problems with two choices (two-choose-one) What if we have more choices? Write a program that takes as input your year in college (as an integer) and outputs your year as freshman, sophomore, junior, senior, or super senior

Flow-Chart Prompt user for year 1 2 Which year? 3 4 freshman sophomore junior senior Next step

Solution Translate N-choose-1 to multiple 2-choose-1 operations Prompt user for year ==1? Freshman ==2? Sophomore ==3? Junior Senior

Write the nested if..else.. In Java if (year==1) { System.out.println( Freshman ); Prompt user for year else { if (year==2) { System.out.println( Sophomore ); else { == 1? if (year==3) { System.out.println( Junior ); else { Freshman == 2? System.out.println( Senior ); Sophomore == 3? Junior Senior

Write the nested if..else.. In Java if (year==1) { System.out.println( Freshman ); Prompt user for year else { if (year==2) { System.out.println( Sophomore ); else { == 1? if (year==3) { System.out.println( Junior ); else { Freshman == 2? System.out.println( Senior ); Sophomore == 3? Junior Senior

Write the nested if..else.. In Java if (year==1) { System.out.println( Freshman ); Prompt user for year else { if (year==2) { System.out.println( Sophomore ); else { == 1? if (year==3) { System.out.println( Junior ); else { Freshman == 2? System.out.println( Senior ); Sophomore == 3? Junior Senior

Parallel Tests These are four strictly parallel & mutually exclusive cases. We can simplify our code a bit: if (year==1) { System.out.println( Freshman ); else if (year==2) { System.out.println( Sophomore ); else if (year==3) { System.out.println( Junior ); else { System.out.println( Senior );

Parallel Tests In this form, we can easily expand the choices. if (year==1) { System.out.println( Freshman ); else if (year==2) { System.out.println( Sophomore ); else if (year==3) { System.out.println( Junior ); else if (year==4) { System.out.println( Senior ); else { System.out.println( Super senior );

Parallel Tests Order of tests is important when they are not mutually exclusive What s the problem with the code below? int x; if ( x % 2 == 0) { System.out.println( Multiple of 2 ); else if ( x % 3 == 0) { System.out.println( Multiple of 3 ); else if ( x % 4 == 0) { System.out.println( Multiple of 4 );

Parallel Tests Order of tests is important when they are not mutually exclusive Order fixed: int x; if ( x % 4 == 0) { System.out.println( Multiple of 4 ); else if ( x % 3 == 0) { System.out.println( Multiple of 3 ); else if ( x % 2 == 0) { System.out.println( Multiple of 2 );

Parallel & Mutually Exclusive Choices If the choices are mutually exclusive, we can write them out as a list of if-only statements. What s bad about this? if (year==1) { System.out.println( Freshman ); if (year==2) { System.out.println( Sophomore ); if (year==3) { System.out.println( Junior );

Non-parallel Cases The general form. Where does the non-parallelism come from? Outer Test Statements Statements Inner Test Statements Statements

Non-parallel Cases if (year==1) { // You cannot re-write this in the if-else-if form else { System.out.println( Freshman ); System.out.println( Great! You have passed your first year ); if (year==2) { else { System.out.println( Sophomore ); System.out.println( Great! You have passed your second year ); if (year==3) { else { System.out.println( Junior ); System.out.println( Great! You have passed your third year ); System.out.println( Senior );

Another Example Determine the last day of a month in a year if (month == 4 month == 6 month == 9 month == 11) { maxday = 30; else if (month == 2) { boolean isleapyear = (year % 4 == 0 && year % 100!= 0) (year % 400 == 0); if (isleapyear) { maxday = 29; else { maxday = 28; else { maxday = 31;

One More Example int a, b, c, max; if (a > b) { if (a > c) { max = a; else { max = c; else { if (b>c) { max = b; else { max = c; Find the max of three

Other tips Indentation in code makes it easy to read Corresponds to level in code logic Eclipse can automatically fix indentation: Try Source -> Correct Indentation & Format

The Conditional Operator (Optional) Short-cut for if-else statements with return value (boolean_expression? value1 : value2 ) If true, return value1. Otherwise return value2 Example 1: if (n1 > n2){ max = n1; else { max = n2; can be written as max = (n1 > n2)? n1 : n2;

The Conditional Operator (Optional) Short-cut for if-else statements with return value (boolean_expression? value1 : value2 ) If true, return value1. Otherwise return value2 Example 2: if (a > b){ System.out.println( a is bigger ); else { System.out.println( b is bigger ); can be written as System.out.println( ( a>b? a is bigger : b is bigger ) );

Next Class Lab 2,3 Bring your laptop & textbook