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

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

Topics. Chapter 5. Equality Operators

Selections. CSE 114, Computer Science 1 Stony Brook University

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

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

Making Decisions In Pascal

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

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

CS112 Lecture: Making Choices

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

Checking Multiple Conditions

CT 229 Java Syntax Continued

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

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

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Flow Control. So Far: Writing simple statements that get executed one after another.

CONDITIONAL EXECUTION

Control Structures in Java if-else and switch

Chapter 3. Selections

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

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

Building Java Programs

CS111: PROGRAMMING LANGUAGE II

COMP 110 Introduction to Programming. What did we discuss?

CS 106 Introduction to Computer Science I

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

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Conditional Programming

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

Chapter 3 Selection Statements

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

Chapter 4 Introduction to Control Statements

Program Control Flow

Program Control Flow

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

BRANCHING if-else statements

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

QUIZ: What value is stored in a after this

Repetition Structures

Conditionals and Loops

CS 112 Introduction to Programming

CS 112 Introduction to Programming

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

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

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

Making Decisions In Pascal

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

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.

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

cs1114 REVIEW of details test closed laptop period

Conditional Statement

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

Making Decisions In Python

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Control Structures in Java if-else and switch

Algorithms and Conditionals

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

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

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

Java Review. Fundamentals of Computer Science

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Handout 4 Conditionals. Boolean Expressions.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

Lesson 7 Part 2 Flags

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

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

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Boolean Expressions (Conditions)

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

CS 112 Introduction to Programming

CS Programming I: Branches

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

Chapter 4 C Program Control

5. Selection: If and Switch Controls

Oct Decision Structures cont d

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

CS111: PROGRAMMING LANGUAGE II

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Basic Computation. Chapter 2

Flow of Control. Chapter 3. Chapter 3 1

Chapter 4: Control Structures I

Conditional Statements

IEEE Floating-Point Representation 1

Midterms Save the Dates!

CS 101 Fall 2005 Midterm 2 Name: ID:

Unit 1 Lesson 4. Introduction to Control Statements

Computer Science & Engineering 150A Problem Solving Using Computers

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

CMPT 125: Lecture 3 Data and Expressions

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Transcription:

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 s (3 variations) [ selection, guard, decision, conditional ] Single selection Double selection Multiple selection switch if if... or if if if if (condition true) action; ------------------ if (condition true) { action; if (condition true) { action1; actionn; 3 4 if (3 variations) Each action could be any of: if (condition true) action1; action2; ------------------ if (condition true){ action1; { action2; if (condition true) { action1a; action7a; { action1b; action5b; 5 A simple action: Assignment statement with arithmetic expression or method call I/O from keyboard / file / window Call to another method Another selection statement: if or if... or switch A while or do...while or for loop or, do absolutely NOTHING ; 6 1

The Condition Comparison (equality & relational) operators: ==!= < > <= >= IMPORTANT: == compare for equality = the assignment operator Compare 2 operands - operands can be: variables, constants, arithmetic expressions, returned values from a method call, 7 But NOT Strings (different methods to compare them) A Condition is true or false (ageofstudent < MI_DRINKING_AGE) (age!= 21) (michiganresident) // a boolean variable ( (a + 2 * 3) <= ( (b - 4) % 3) ) ( (Math.PI * r * r) < maxsize ) NOTE: need ( ) around whole condition 8 Logic operators in conditions && (and) (or)! (not) (!(age == 25) ) [same as (age!= 25)] ( (a < b) && (c < d) ) ( (firstinitial == W ) (gender == F ) ) Truth Table c1 c2 c1 && c2 c1 c2!c1!c2 F F F F T T F T F T T F T F F T F T T T T T F F e.g., c1: (age >= 21) c2: (classyear = 1) [Note: use truth tables to determine results] 9 10 Order of precedence? NOTE: need ( ) around whole condition ( (a == b) && (c > -14) ) typical (a == b) && (c > -14) WRONG ( a == b && c > -14 ) OK 1 st 3 rd 2 nd All juniors and seniors with a gpa of at least a 3.0 (gpa >= 3.0 && classyear == 3 classyear == 4) WRONG Order of precedence of operators unary operators - +! arithmetic operators * / % + - relational operators < > <= >= equality operators ==!= logic operators && [and] NOTE: and before or assignment operator = [or] 11 but ( ) can over-ride these 12 2

Translation from English? All juniors and seniors should get bonus points if (status == 3 && status == 4) { WRONG NOTE: status variable holds ONE value if (status == 3 status == 4) { RIGHT 13 Actions total = total + exam; counter++; System.out.println( blah blah ); num = keyboard.nextint(); 14 do nothing ( ; )Action if (maritalstatus!= M ) numnotmarried = numnotmarried + 1; OK, but if it s clearer (less bug-prone) to specify POSITIVE condition vs. NEGATIVE condition Caution with ; (it means Empty block of actions ) WRONG if (a < b); System.out.println( a < b ); // println will ALWAYS happen; not related to if if (maritalstatus == M ) ; // empty statement - do nothing numnotmarried = numnotmarried + 1; 15 RIGHT if (a < b) // no ; here System.out.println( a < b ); // println MAY happen, depending on if condition 16 Nested if/ Empty statement // note indent/align formatting answer = 2; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> 17 a: 4, b: 2, answer >> a: 2, b: 2, answer >> if (b == 0) ; // do nothing here, OK answer = 2; if (b == 2) 18 3

Dangling? // WRONG? Indentation suggests, but... // this paired with NOTE: compiler ignores formatting and does what instructions actually say to do 19 Prior example actually says : // so b!= 5 falls here Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >> 20 Dangling - the FIX { // now applies to 21 Nested if/ if/ [works, but NOT TYPICAL FORMATTING] if (total >= 80) if (total >= 70) if (total >= 60) 22 Nested typical formatting: if (total >= 80) if (total >= 70) if (total >= 60) Stacked (vs. Nested) if s int bonus = 0; if (attendancepercent >= 90) bonus = bonus + 5; if (labpoints >= 600) bonus = bonus + 35; 23 24 4

Stacked - WRONG if (total >= 80) if (total >= 70) if (total >= 60) 25 Nested vs. Stacked guidance NESTED if/ s control goes to ONLY 1 block (to 1 st condition that s true block) so ONLY 1 set of actions is done (or none) ~ GUI radio buttons used for: mutually exclusive categories - which state to use for tax rate FIRST category that applies - grades example STACKED if s control goes to ALL blocks (& checks all conditions), so ALL/many sets of actions MIGHT be done ~ GUI check boxes used for: ALL categories that apply - cumulative bonus points 26 Caution on Nested Since control goes to ONLY 1 (or 0) action block (i.e., the 1 st condition that applies) and no subsequent if blocks nor the final block (if any) Switch statement Equivalent to nested if/ Shown later so ORDER of conditions MAY be important NO for mutually exclusive categories - state YES for use 1 st category that applies - grade example NOTE: grades are really mutually exclusive, but example didn t specify ALL conditions for a category 27 28 5