Decision Structures if, if/else conditions. Selection DECISION: determine which of 2 paths to follow (1 or more statements in each path)

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

The if-else Statements
Fundamental of C programming
Control Structures Selections Repetitions/iterations
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Logic & program control part 3: Compound selection structures.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 4 Control Structures –Decisions –Loops. Chapter 4 selection structures This chapter begins a new path in our programming ability Basically we.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Logical Operators and Conditional statements
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 3 Making Decisions
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Decisions in Python Bools and simple if statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Rational Expressions relational operators logical operators order of precedence.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Branching statements.
Java Programming Fifth Edition
Chapter 3 Control Statements
Selections Java.
Loop Structures.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Control Structures – Selection
Summary Two basic concepts: variables and assignments Basic types:
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
The Java switch Statement
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Outline Software Development Activities
Decision Structures if, if/else conditions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Decision Structures if, if/else conditions

Selection DECISION: determine which of 2 paths to follow (1 or more statements in each path)

Selection options ( in Java ) [selection, guard, decision, conditional] Single selection if Double selection if... else Multiple selection switch or if … else if … else if … else …

Plain if ’s (3 variations) if (condition true) action; if (condition true) { action; } | if (condition true) | { | action1; |... | actionN; | } |

if … else (3 variations) if (condition true) action1; else action2; if (condition true) {action1; } else {action2; } | if (condition true) | { action1A; |... | action7A; | } | else | { action1B; |... | action5B; | }

Each action could be: 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...else or switch A while or do… while or for loop [or do absolutely NOTHING] ;

Conditions Comparison (equality, relational) operators: == != = NOTE: == compare for equality = the assignment operator Compare 2 operands, which can be: variables, constants, arithmetic expressions, returned values from a method call,... But NOT Strings (different methods to compare them)

Conditions are true or false (ageOfStudent < MI_DRINKING_AGE) (age != 25) (michiganResident) // a boolean variable ( (a + 2 * 3) <= ( (b - 4) % 3) ) ( (Math.PI * r * r) < maxSize ) NOTE: need ( ) around whole condition

Logic operators in conditions && (and) || (or) ! (not) ( !(a == 25) ) [same as (a != 25) ] ( (a < b) && (c < d) ) ( (a == 3) || (c == 1) ) [Note: use truth tables to determine results]

Order of precedence ? NOTE: need ( ) around whole condition ( (a == b) && (c > -14) ) typical (a == b) && (c > -14) WRONG ( a == b && c > -14 ) OK “All juniors and seniors with a gpa of at least a 3.0” (gpa >= 3.0 && classStatus == 3 || classStatus == 4) WRONG

Order of precedence of operators Unary operators - + ! Arithmetic operators * / % + - Relational operators = equality operators == != Logic operators && [AND] important: “and before or” || [OR] Assignment operator = but ( ) can over-ride these

Translation from English? “All juniors and seniors should get bonus points” if (classStatus == 3 && classStatus == 4) {... } WRONG classStatus is a variable that holds ONE value if (classStatus == 3 || classStatus == 4) {... } RIGHT

Actions total = total + exam; counter++; System.out.println(“blah blah”); num = keyboard.nextInt();...

“do nothing” ( ; )Action if (maritalStatus != ‘M’) numNotMarried = numNotMarried + 1; OK, but if it’s clearer (less likely to lead to bugs) to specify POSITIVE condition vs. NEGATIVE if (maritalStatus == ‘M’) ; // empty statement - do nothing else nNotMarried = nNotMarried + 1;

Caution with ; (it’s “Empty block of actions”) WRONG if (a < b) ; System.out.println(“a<b”); // println will ALWAYS happens; not related to if RIGHT if (a < b) // no ; here System.out.println(“a<b”); // println MAY happen, depending on if condition

Nested if/else if (a == 4) // note indent/align formatting if (b == 5) answer = 1; else answer = 2; else if (b == 5) answer = 3; else answer = 4; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >>

Empty statement if (a == 4) if (b == 5) ; // do nothing here, OK else answer = 2; else if (b == 5) answer = 3; else answer = 4;

Dangling else ? if (a == 4) if (b == 5) answer = 1; // WRONG ? Indentation suggests…, but... else // this else paired with if (b==5) if (b == 5) answer = 3; else answer = 4; // NOTE: compiler ignores formatting and does what instructions actually “say to do”

Prior example actually “says”: if (a == 4) if (b == 5) answer = 1; else // so b != 5 falls here if (b == 5) answer = 3; else answer = 4; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >>

Dangling else - the FIX if (a == 4) {if (b == 5) answer = 1; } else // else now applies to 1 st if if (b == 5) answer = 3; else answer = 4;

Nested if/else if/else... [works, but NOT TYPICAL FORMATTING] if (total >= 90) System.out.println(‘A’); else if (total >= 80) System.out.println(‘B’); else if (total >= 70) System.out.println(‘C’); else if (total >= 60) System.out.println(‘D’); else System.out.println(‘E’);

Nested… generally written as: if (total >= 90) System.out.println(‘A’); else if (total >= 80) System.out.println(‘B’); else if (total >= 70) System.out.println(‘C’); else if (total >= 60) System.out.println(‘D’); else System.out.println(‘E’);

Stacked ( vs. Nested ) if ’s int bonus = 0; if (attendancePercent >= 90) bonus = bonus + 5; if (labPoints >= 600) bonus = bonus + 35;

Stacked - WRONG if (total >= 90) System.out.println(‘A’); if (total >= 80) System.out.println(‘B’); if (total >= 70) System.out.println(‘C’); if (total >= 60) System.out.println(‘D’); System.out.println(‘E’);

Nested vs. Stacked NESTED if/else ’s control goes to ONLY 1 block (the 1 st condition that’s true), so ONLY 1 set of actions is done (or none) used for: –mutually exclusive categories- which state to use for tax –FIRST category that applies- grades example –~ GUI radio buttons STACKED if ’s control goes to ALL blocks (& checks all conditions), so ALL/many sets of actions MIGHT be done used for: –ALL categories that apply- cumulative bonus –~ GUI check boxes

Nested if/else ’s Since control goes to ONLY 1 (or 0) action block (the 1 st condition that applies) and none of the subsequent else if blocks (nor the final else block) (or, if there’s no final else maybe 0 actions occur) So the ORDER of conditions MAY be important –NO for mutually exclusive categories- state –YES for “use “1 st category that applies”- grade example (shown in earlier slide) NOTE: grades are really mutually exclusive, but example didn’t specify conditions in mutually exclusive way

Switch statement Equivalent to nested if/else Shown later