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

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

CONDITIONAL EXECUTION: PART 2

COMP 110 Introduction to Programming. What did we discuss?

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

Flow of Control. Chapter 3

Control Structures in Java if-else and switch

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.

Flow of Control. Chapter 3

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

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Control Structures in Java if-else and switch

LAB 4.1 Relational Operators and the if Statement

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Darrell Bethea May 25, 2011

Flow of Control. Chapter 3

Handout 4 Conditionals. Boolean Expressions.

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

Chapter 3 Selection Statements

More Complex Versions of the if Statement. Class 13

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

CS111: PROGRAMMING LANGUAGE II

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

BRANCHING if-else statements

AP Computer Science A Summer Assignment 2017

Controls Structure for Repetition

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

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

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

Course Outline. Introduction to java

CS313D: ADVANCED PROGRAMMING LANGUAGE

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

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

CS302: Self Check Quiz 2

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

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

COMP 110 Introduction to Programming. What did we discuss?

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

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

COMP String and Console I/O. Yi Hong May 18, 2015

Getting started with Java

CONDITIONAL EXECUTION

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Eng. Mohammed S. Abdualal

CONDITIONAL EXECUTION

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Topics. Chapter 5. Equality Operators

CompSci 125 Lecture 11

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

Logic & program control part 3: Compound selection structures

Chapter Goals. Contents LOOPS

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

Flow of Control. Chapter 3. Chapter 3 1

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

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

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

Assignment 2.4: Loops

Flow of Control. Chapter 3 Part 3 The Switch Statement

Logical Operators and switch

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

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

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy

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

1007 Imperative Programming Part II

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

Object-Oriented Programming

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

Chapter 4: Control structures. Repetition

Loops (while and for)

Condi(onals and Loops

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

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

Selections. CSE 114, Computer Science 1 Stony Brook University

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

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

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

Selection Statements and operators

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Midterm Review Session

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 4: Control structures

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Java I/O and Control Structures

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Computer Programming I - Unit 5 Lecture page 1 of 14

Decision Making -Branching. Class Incharge: S. Sasirekha

AP Programming - Chapter 6 Lecture

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.

Java I/O and Control Structures Algorithms in everyday life

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

Introduction to Programming Using Java (98-388)

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

Lecture 2: Intro to Java

Condensed Java. 12-Oct-15

Chapter 3. Selections

Programming with Java

COMP 202 Java in one week

Transcription:

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 & Comparison Q2: How to compare values of primitive types? How about objects? Q3: Write the boolean expression for testing a leap year A leap year is divisible by 4, but not by 100 (except if divisible by 400)

Today More if / else statements The switch statements

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

Tracing if / else code Simple if-else statement: 2-choose-1 Nested if-else statement: N-choose-1 Translate to multiple 2-choose-1 operations Example: Write a program that takes an 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 5 freshman sophomore junior senior super senior Next step

With Nested if / else if (year == 1) System.out.println( freshman ); else if (year == 2) System.out.println( sophomore ); else if (year == 3) Prompt user for year ==1? ==2? System.out.println( junior ); else if (year == 4) System.out.println( senior ); else if (year == 5) System.out.println( super senior ); freshman sophomore junior ==3? ==4? ==5? else System.out.println( unknown ); senior super senior unknown

Switch Statement Case labels Default case: all other values switch(year) { case 1: System.out.println( freshman ); case 2: System.out.println( sophomore ); case 3: System.out.println( junior ); case 4: System.out.println( senior ); case 5: System.out.println( super senior ); default: System.out.println( unknown ); Controlling expression Break statements

Switch Statement Syntax String has been supported in JDK 7 switch (Controlling_Expression) { case Case_label: statements; case Case_label: statements; default: statements; Only int, char, enum can be used in the controlling expression Case labels must be of same type as controlling expression The break statement ends the switch statement, go to the next step outside the braces in the code The default case is optional

Practice with Switch Statements Write a switch statement that takes as the controlling expression the number of siblings a person has (as an int) and outputs an appropriate messages as follows: Number of Siblings Message 0 An only child 1 Just one you say 2 Two siblings! 3 Big Family! 4 or more I don t believe you

Switch Statements switch (numofsiblings) { case 0: System.out.print( An only child ); case 1: System.out.print( Just one you say ); case 2: System.out.print( Two siblings! ); case 3: System.out.print( Big family! ); default: System.out.print( I don t believe you ); 11!

Enumerations Lists the values that a variable can have E.g.: rate movies as either excellent, average, or bad enum MovieRating {EXCELLENT, AVERAGE, BAD MovieRating rating; rating = MovieRating.AVERAGE; Provides a way to restrict the values of a variable Actually a class, typically define it within another class, but always outside of method definitions. more discussion in Chapter 6

An Example switch (rating) { case EXCELLENT: System.out.print( You must see this movie! ); case AVERAGE: System.out.print( This movie is OK, but not great. ); case BAD: System.out.print( Skip it! ); default: System.out.print( Something is wrong. );

Avoid Logical Errors in the Multibranch if-else Statement

Order of Boolean Expressions What s the problem with the code below? int n; if (n % 2 == 0) { System.out.println( Multiple of 2 ); else if (n % 3 == 0) { System.out.println( Multiple of 3 ); else if (n % 4 == 0) { System.out.println( Multiple of 4 ); Ordering is important when boolean expressions are not mutually exclusive

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

An Example Determine the number of days in each month of 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;

The Conditional Operator Short-cut for if-else statements with return value boolean_expression? value1 : value2 ; If true, return value1; otherwise, return value2 Example: if (n1 > n2) max = n1; else max = n2; can be written as max = (n1 > n2)? n1 : n2; The? and : together are call the conditional operator or ternary operator.

Some Tips Indentation in code makes it easy to read Corresponds to level in code logic Eclipse can automatically fix indentation Try Source à Correct Indentation or Format

Applications and Applets We will cover two kinds of Java programs: applications and applets Applications are regular programs, run on your computer Applets have graphical features, run within a Web browser

Next Class Lab 2 & 3 About String, Java applets (Chapter 1.4), flow chart and if-else Bring your laptop and textbook