Factoring if/else code

Slides:



Advertisements
Similar presentations
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
If Statements & Relational Operators Programming.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Topic 12 more if/else, cumulative algorithms, printf Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 – 5.5.
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
Lecture 3 Selection Statements
Building Java Programs
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs Chapter 4
Lecture 4: Program Control Flow
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 4: Conditionals
Type boolean boolean: A logical type whose values are true and false.
CSc 110, Spring 2017 Lecture 8: input; if/else
Building Java Programs
Building Java Programs Chapter 4
Chapter 8 JavaScript: Control Statements, Part 2
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
Executes a block of statements only if a test is true
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
Building Java Programs
Lecture 6: Conditionals AP Computer Science Principles
Factoring if/else code
Building Java Programs
Building Java Programs
SSEA Computer Science: Track A
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Building Java Programs
Factoring if/else code
Building Java Programs
Presentation transcript:

Factoring if/else code factoring: Extracting common/redundant code. Can reduce or eliminate redundancy from if/else code. Example: if (a == 1) { System.out.println(a); x = 3; b = b + x; } else if (a == 2) { x = 6; y = y + 10; } else { // a == 3 x = 9; } System.out.println(a); x = 3 * a; if (a == 2) { y = y + 10; } b = b + x;

if/else with return Methods can return different values using if/else // Returns the larger of the two given integers. public static int max(int a, int b) { if (a > b) { return a; } else { return b; } Methods can return different values using if/else Whichever path the code enters, it will return that value. Returning a value causes a method to immediately exit. All paths through the code must reach a return statement.

All paths must return The following also does not compile: public static int max(int a, int b) { if (a > b) { return a; } // Error: not all paths return a value The following also does not compile: } else if (b >= a) { return b; The compiler thinks if/else/if code might skip all paths, even though mathematically it must choose one or the other.

Logical operators Tests can be combined using logical operators: "Truth tables" for each, used with logical values p and q: Operator Description Example Result && and (2 == 3) && (-1 < 5) false || or (2 == 3) || (-1 < 5) true ! not !(2 == 3) p q p && q p || q true false p !p true false

Evaluating logical expressions Relational operators have lower precedence than math; logical operators have lower precedence than relational operators 5 * 7 >= 3 + 5 * (7 – 1) && 7 <= 11 5 * 7 >= 3 + 5 * 6 && 7 <= 11 35 >= 3 + 30 && 7 <= 11 35 >= 33 && 7 <= 11 true && true true Relational operators cannot be "chained" as in algebra 2 <= x <= 10 true <= 10 (assume that x is 15) Error! Instead, combine multiple tests with && or || 2 <= x && x <= 10 true && false false