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

Similar documents
Control Structures: if and while A C S L E C T U R E 4

More Complex Versions of the if Statement. Class 13

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

Introduction to Software Development (ISD) Week 3

Controls Structure for Repetition

Control Structures in Java if-else and switch

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

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

STUDENT LESSON A12 Iterations

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

Chapter 4: Control structures. Repetition

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Decisions (If Statements) And Boolean Expressions

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Chapter 4: Control structures

Oct Decision Structures cont d

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Loops. CSE 114, Computer Science 1 Stony Brook University

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

AP COMPUTER SCIENCE A

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

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

Chapter 4: Making Decisions

Module 2: Choice and Iteration

Chapter Goals. Contents LOOPS

1 Short Answer (10 Points Each)

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Loops (while and for)

LECTURE 04 MAKING DECISIONS

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Programming Paradigms: Overview to State Oriented

H212 Introduction to Software Systems Honors

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

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

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

Flow of Control. Chapter 3

Selection Statements and operators

How would you handle the case for when a user selects an option? 1/15/2016 CSE 11 WINTER LEC 6 2

Loops / Repetition Statements

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

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

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Flow of Control. Chapter 3

COMP-202 Unit 4: Programming with Iterations

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

Conditional Execution

Selection Statements and operators

Handout 4 Conditionals. Boolean Expressions.

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

Control Structures in Java if-else and switch

Chapter 4: Control Structures I

Building Java Programs

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Chapter 4: Making Decisions

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

CSC 1051 Data Structures and Algorithms I

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

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

Example Program. public class ComputeArea {

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Object-Oriented Programming

Chapter 3. Selections

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

McGill University School of Computer Science COMP-202A Introduction to Computing 1

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Programming with Java

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

Assessment - Unit 3 lessons 16-21

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

CS111: PROGRAMMING LANGUAGE II

Programming with Java

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

Midterm Practice Problems

CompSci 125 Lecture 11

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

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

Question: Total Points: Score:

BRANCHING if-else statements

Java Classes: Math, Integer A C S L E C T U R E 8

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

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

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

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

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS 106 Introduction to Computer Science I

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Example: Monte Carlo Simulation 1

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Building Java Programs

Chapter 7 User-Defined Methods. Chapter Objectives

Transcription:

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 sequential execution: statements execute one after the other in the order in which they appear in the program 2

Flow of Control cont. Consider the following tasks: You want to compute the quotient of two variables but only if the divisor is not zero You input some value (e.g., a date) and if it is in the correct format (mm/dd/yyyy) you continue the computation, otherwise you print an error Given a grade between 0 and 100, you want to convert the numeric value to a letter grade (e.g., A for grade greater than 90, B for grade between 80 and 90, etc.) How can we check these conditions and execute the appropriate piece of code depending on the outcome of the check? 3

Selection Statements Scanner in = new Scanner(System.in); System.out.print( Enter the dividend: ); int dividend = in.nextint(); System.out.print( Enter the (non-zero) divisor: ); int divisor = Integer.parseInt(in.nextLine()); if (divisor!= 0) { int quotient = dividend / divisor; Boolean expression System.out.println(dividend + / + divisor + = + quotient); else { System.out.println( Cannot divide by 0 ); 4

The boolean Type A variable of the boolean data type stores one of two values: true or false true and false are the only two boolean constants boolean values/expressions are used to make decisions in programs For example: if (x > 0) // boolean expression { System.out.println( x is positive ); 5

Boolean Expressions There are several kinds of boolean-valued expressions: a boolean variable or constant, e.g., boolean boolvar = in.nextboolean(); if (boolvar) {... an arithmetic expression followed by a relational operator followed by an arithmetic expression, e.g., int intvar = in.nextint(); if (intvar > 0) {... 6

Relational Operators == (equal)!= (not equal) > < >= <= x == y x!= y x > y x < y x >= y x <= y 7

Boolean Operators We can also build boolean expressions by combining two boolean expressions with a boolean operator: && (and) (x > 0) && (x < 10) (or) (x <= 0) (x >= 10)! (not)! (x == 0) 8

Boolean Operators cont. If A and B are boolean expressions, A && B is true if and only if both A and B are true (in other words, if either A or B or both are false, A && B is false) If A and B are boolean expressions, A B is true if either A or B or both are true (in other words, A B is false only if both A and B are false) If A is a boolean expression,!a is true if A is false, and!a is false if A is true 9

Some Boolean Expressions What s the value of each of the following expressions: int x = 5, y = 12; boolean a = true, b = false, c = true; (x > 0) && (x < 10) (x <= 0) (x >= 10)! (a && b && c) (a b c) ((x 1) == ((y / 5) + (y % 5))) ((x!= y)! (x == y)) 10

Your Turn Given three integer variable, i, j, and k, write a boolean expression for each of the following problems: i is equal to 3 or 5 i is between 1 and 7 (but not 1 or 7) i is even i is odd i is the smallest of i, j, and k 11

If: Syntax and Flow Chart boolean expression if ( test ) { if_block test true if_block false statement sequence 12

If-Else: Syntax and Flow Chart if ( test ) { if_block else { else_block boolean expression true if_block statement sequences test false else_block 13

An Example Given two integers i and j, write a piece of code that sets integer variable max to the value of the larger of the two. if (i > j){ max = i; else { // i <= j max = j; 14

Tracing an if-else statement To trace an if-else statement, trace only the portions of the statement that get executed Base this on an evaluation of the boolean expression 15

A trace with an initial state Program Line Program state i = 10 j = 5 max = 0 if (i > j) { max = i; else { max = j; 16

A trace with an initial state Program Line Program state i = 10 j = 5 max = 0 if (i > j) { max = i; i = 10 j = 5 max = 10 else { max = j; 17

A trace with an initial state Program Line Program state i = 10 j = 5 max = 0 if (i > j) { max = i; i = 10 j = 5 max = 10 else { max = j; i = 10 j = 5 max = 10 18

A trace with an initial state Program Line Program state i = 10 j = 5 max = 0 if (i > j) { max = i; Note that this portion gets skipped over with these inputs! else { max = j; i = 10 j = 5 max = 10 i = 10 j = 5 max = 10 19

A trace with another initial state Program Line Program state i = 10 j = 500 max = 0 if (i > j) { max = i; else { max = j; 20

A trace with another initial state Program Line Program state i = 10 j = 500 max = 0 if (i > j) { max = i; else { max = j; i = 10 j = 500 max = 500 21

A trace with another initial state Program Line Program state i = 10 j = 500 max = 0 if (i > j) { max = i; else { max = j; i = 10 j = 500 max = 500 i = 10 j = 500 max = 500 22

A trace with another initial state Program Line if (i > j) { max = i; And this time it s THIS portion that gets skipped with these inputs! Program state i = 10 j = 500 max = 0 else { max = j; i = 10 j = 500 max = 500 i = 10 j = 500 max = 500 23

An alternative solution Given two integers i and j, write a piece of code that sets integer variable max to the value of the larger of the two. max = i; if (j > max) { max = j; Trace through the code above? Does it do the same thing as the previous code? Sometimes a good choice of initial value can make for simpler code. 24

Problem solving with if-else Suppose we have an integer i and we need to know whether it is even or not Recall the % operation i is even if i % 2 == 0 This suggests a choice: If i % 2 == 0, then i is even Otherwise, i is odd Problems that can be solved with if-else often have this form: If x is true then perform action y otherwise perform action z 25

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. 26

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; 27

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; if ( ) { // i is even // set result to even else{ // i is odd // set result to odd 28

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; if ( ) { // i is even // set result to even else{ // i is odd // set result to odd What goes here? 29

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; if ( i % 2 == 0 ) { // i is even // set result to even else{ // i is odd // set result to odd Our boolean expression test! 30

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; if ( i % 2 == 0 ) { // i is even // set result to even else{ // i is odd // set result to odd What goes here? 31

Problem-solving with if-else Given an integer i, write some code that will set a string to even if i is even, and odd if I is odd. String result = ; if ( i % 2 == 0 ) { // i is even result = even ; else{ // i is odd result = odd ; The code that performs this action! 32

Another Example (Alternative) Given an integer i, write a piece of code that sets a string to even or odd depending on whether the value of i is even or odd. Sometimes a good choice of initial value can make your code simpler String result = odd ; if ((i % 2) == 0) // i is even { result = even ; Trace the above code Is it the same as what was on the previous slide? 33

Your Turn Given a string str and a boolean variable startswitha, write a segment of code that sets startswitha to true if the first character of str is a capital A, and false otherwise. 34

Nested ifs Given three integers i, j, and k, write a piece of code that sets integer variable max to the value of the largest of the three. What does this logic look like? 35

Nested ifs Given three integers i, j, and k, write a piece of code that sets integer variable max to the value of the largest of the three. What does this logic look like? If i is bigger than j, then set max to the larger of i and k Otherwise set max to the larger of j and k 36

Nested ifs if (i > j){ // set max to the larger of i and k else { // i <= j // set max to the larger of j and k 37

Nested ifs if (i > j){ // set max to the larger of i and k else { // i <= j // set max to the larger of j and k How do we do this? 38

Nested ifs if (i > j){ // set max to the larger of i and k else { // i <= j // set max to the larger of j and k How do we do this? We ve already done it! 39

Nested ifs if (i > j){ if (i > k) { // i is max max = i; else { // k is max max = k; else { // i <= j // set max to the larger of j and k How do we do this? We ve already done it! Just use the logic for finding the max of two values here! 40

Nested ifs if (i > j){ if (i > k) { // i is max max = i; else { // k is max max = k; else { // i <= j // set max to the larger of j and k How do we do this? 41

Nested ifs if (i > j){ if (i > k) { // i is max max = i; else { // k is max max = k; else { // i <= j // set max to the larger of j and k How do we do this? We ve already done it! 42

Nested ifs if (i > j){ if (i > k) { // i is max max = i; else { // k is max max = k; else { // i <= j if (j > k) // j is max { max = j; else // k is max { max = k; How do we do this? We ve already done it! Just use the logic for finding the max of two values here! 43

Max Of Three if (i > j) { if (i > k) // i is max { max = i; else // k is max { max = k; else // i <= j { if (j > k) // j is max { max = j; else { max = k; // k is max 44

Max Of Three if (i > j) { if (i > k) // i is max { max = i; else // k is max { max = k; else // i <= j { if (j > k) // j is max max = i; if (j > max) { max = j; if (k > max) { max = k; { max = j; else { max = k; // k is max 45

Your Turn, Again Given an integer, grade, holding a grade between 0 and 100, write a piece of code that converts the numeric value to a letter grade according to the following table and prints the letter grade. grade 90 80 grade<90 70 grade<80 60 grade<70 grade<60 A B C D E 46

Your Turn, One More Time Given a String, s, which is meant to hold a date in the mm/dd/yyyy format, check that it is in the correct format and print an error message if it is not. boolean Character.isDigit(char ch) allows you to check if a given character, ch, is a digit ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ) or not. 47