FIT1002 2006 1 Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff.

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CSE1301 Computer Programming Lecture 8 Selection.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 CSE1301 Computer Programming Lecture 9 Selection.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
C++ for Engineers and Scientists Third Edition
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Java Programming Fifth Edition
Revision of IO Streams printf scanf.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Selection (also known as Branching) Jumail Bin Taliba by
Selections Java.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Control Structures – Selection
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6 Control Statements: Part 2
CSI 121 Structure Programming Language Lecture 9 Selection
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Introduction to Computing Lecture 04: Booleans & Selection
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

FIT Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff between complex boolean expressions and nested selection be able to write simple code with conditional executions Reading: Savitch, Sec

FIT The if statement Determines whether a statement or block is executed. Implements the selection instruction within an algorithm. Decides what to do by evaluating a Boolean expression. If the expression is true, the block is executed. if ( expression ) block

FIT Reminder: Blocks A block is either a single statements (ending with a semicolon ;) or A compound statement or block is a series of statements surrounded by braces. { x = x +1; System.out.println(x); } An empty statement is a single semicolon. E.g.

FIT Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) System.out.println("X is odd"); }

FIT public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) ; System.out.println("X is odd"); } Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. Do not put a semicolon after rhe condition!

FIT Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. Do not put “then” here either! public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) System.out.println("X is odd"); }

FIT Notes on if Which of the following code fragments are equivalent? A B C if (x % 2 == 1) { System.out.println(x); } System.out.println(” is odd"); if (x % 2 == 1) System.out.println(x); System.out.println(” is odd"); if (x % 2 == 1) { System.out.println(x); System.out.println(” is odd"); }

FIT Notes on if Which of the following code fragments are equivalent? A B C A Statement A Block if (x % 2 == 1) { System.out.println(x); } System.out.println(” is odd"); if (x % 2 == 1) System.out.println(x); System.out.println(” is odd"); if (x % 2 == 1) { System.out.println(x); System.out.println(” is odd"); }

FIT The else statement Can only occur after an if statement Is only executed when the if block does not execute if ( expression ) block1 else block2

FIT public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddeventest() { if (x % 2 == 1) System.out.println("X is odd"); else System.out.println("X is even"); } Example: oddeventest (version 2) Test whether the instance variable “x” is odd or even and print an appropriate message in both cases.

FIT public class Test { private int x=0; public void set_x(int val) { x=val; } public boolean isodd() { if (x % 2 == 1) return true; else return false; } Example: isodd (version 1) Test whether the instance variable “x” is odd or even and return a boolean value indicating whether it is odd. if x is odd then return true else return false

FIT public class Test { private int x=0; public void set_x(int val) { x=val; } public boolean isodd() { return (x % 2 == 1) ; } Example: isodd (version 2) Since we are only selecting between two boolean values this can be written much more elegantly without selection. return the truth value of “x is odd” directly

FIT Boolean Expressions … are used as the selection condition. … Complex boolean expressions are formed with the boolean operators (also “logical” operators ) Constants: true, false Comparison Operators: Equality ( == ) Inequality ( != ) Comparison (, = ) Logical Operators: And ( && ) Or ( || ) Not ( ! )

FIT Equality True only if both arguments have identical values. 1 == 2 x == 0 (x+4) == 42 p1.x == p2.x Examples: not to be confused with assignment ( = )

FIT Inequality True only if arguments have different values. 1 != 2 x != 0 p.x != 42 p1.x != p2.x Examples:

FIT Comparison True only if the specified relationship holds 1 < 2 x > 1 p.x <= 42 p2.x+3 >= p2.x Examples:

FIT Not True only if the single Boolean argument is false. ! true ! (x<0) ! happy Examples:

FIT And True only if both Boolean arguments are true. (x > 0 && X < 100) healthy && wealthy && wise Examples:

FIT Or True if either Boolean argument is true (or both are true). ! (x 100) good || bad || ugly Examples:

FIT Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples:

FIT Precedence Highest to lowest: Brackets Not ( ! ) Comparison (, = ) Equality ( == ), Inequality ( != ) And ( && ) Or ( || ) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators. analyze: result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours;

FIT public boolean inRange(int x) { if (0 < x < 99 ) { System.out.println(“X is in Range”); } public boolean inRange(int x) { if (x > 0 && x < 99 ) { System.out.println(“X is in Range”); } is incorrect. Instead use Common Mistake: Multiple Comparison

FIT Cascaded if statement Multiple alternative blocks each with a Boolean expression. First expression which evaluates to true causes execution of the associated block. At most only one block will be executed.

FIT Example: months Determine the number of days in a given month: 30 days hath September, April, June and November. All the rest hath 31, Excepting February alone, Which hath 28 days clear, And 29 in each leap year. public class Months { int September = 9; int April = 4; int June = 6; int November = 11; int February = 2; …

FIT Example: months public int month_length(int month) { if (month==September || month==April || month==June || month==November ) { return 30; } else if (month==February) { return 28; } // only for non-leap year else // at this point the following values for // month have been ruled out: // September, April, June, November, February { return 31; } } Note that the braces around the return statements are not really required (but they never hurt!)

FIT Nested If Nested If statements correspond to a logical conjunction of the conditions: The innermost statement will only be executed if all enclosing conditions are true. int x; … … if (x>0) if (x<100) System.out.println(“X is in the range 1…99”); … if (x>0) { if (x<1000) System.out.println(“x is a small positive number”); if (x>=1000) System.out.println(“x is a large positive number”); }

FIT Nested If versus Complex Conditions You can often avoid nested If statements by using compound boolean expressions. The following two have identical function: if (a <= b) if (x >= a) if (x <= b) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); if (b <= a) if (x >= b) if (x <= a) System.out.println(“x is between a and b”); else System.out.println(“x is outside the range “+a+” to “+b); if ((a = a && x <= b) || (b = b && x <= a)) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); Does the first code segment really work? Can you simplify further?

FIT Dangling Else Problem An “else” statement always belongs to the closest unmatched “if”. If you want it to be associated with another if you need to indicated block boundaries of unmatched “if”s with braces. if (a <= b) { if (x >= a) if (x <= b) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); } else if (x >= b) if (x <= a) System.out.println(“x is between a and b”); else System.out.println(“x is outside the range “+a+” to “+b); Braces indicate end of inner if

FIT Conditional Operator We can write an expression such that its value depends on a TRUTH value Condition ? Expr2 : Expr3 A ternary operator For example: int max, a, b; … max = (a>b) ? a : b ;

FIT Switch Statements The switch statement selects one of several execution branches. int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); }

FIT Switch Statements Switch searches through the cases until a value is found that matches the value of the test expression. This case will be executed. The break statement terminates the switch case and execution continues after the whole switch statement. int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); }

FIT Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } The switch expression can be of type int, char, short or byte.

FIT Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } The case labels must match the type of the switch expression.

FIT Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } If you omit the break, the execution continues with the next case!

FIT Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } A default case is not mandatory, but it is always safer to have one

FIT Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } without a default case nothing will be executed if no case label matches.

FIT Equality of Objects What happens if you use “==“ to compare objects? You are not testing for the same field values, but for object identity! public class Test { int x=0; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a==c); System.out.println(a==b); … prints “true” (the very same object)

FIT Equality of Objects What happens if you use “==“ to compare objects? You are not testing for the same field values, but for object identity! public class Test { int x=0; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a==c); System.out.println(a==b); … prints “false” (different objects with same field values)

FIT Equality of Objects To test any other notion of equality that makes sense in your application (e.g. same field values) you need to write a test method yourself. By convention this should be a a method “equals” with a single parameter which is an object of the same class as “this” (see Java API) You are free to implement any notion of equality as long as it is transitive, refelxive and symmetric. public class Test { int x=0; public boolean equals(Test other) { return this.x=other.x; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a.equals(b)); System.out.println(c.equals(a)); … both now print “true” (tested for same field values) You are effectively overriding the pre-exisiting default equals method. This is inherited from the class object and uses object identity (==)

FIT Equality of Strings The class String comes with a number of useful equality operators. The basic “equals” compares two String for identical contents. You can also use “equalsIgnoreCase”. (check the class String in the Java API definition at to find other String methods) String s1=“Bernd Meyer”; String s2=“Bernd MEYER”; System.out.println(s1.equalsIgnoreCase(s2); …

FIT Exclusive OR True if “at most one” of two alternatives is true False if neither is true, also False if both are true! Define! A xor B : Do you need brackets ?

FIT Checking & Simplifying Boolean Expressions Use Truth Tables Transform Expressions using Boolean Algebra ABA xor B AB A && !B || B && !A

FIT Boolean Algebra --- History George Boole, Initially self-guided studies of languages and philosphy With 16 assistant teacher at private school With 20 opened a school and taught himself mathematics Published in “Cambridge Mathematical Journal” with 24 The Mathematical Analysis of Logic was published in 1847 Casts logical reasoning in the form of algebra + is OR, * is AND 0 is FALSE, 1 is TRUE An Investigation of the Laws of Thought (1854) extends the algebra

FIT Axioms of Boolean Algebra In the following, “a  b” means “is equivalent”. This means that you can re-write the form a (the left-hand side) into the form b (the right hand side) without changing the meaning. (and also the other way around) Commutative (A && B)  (B && A) (A || B)  (B || A) Associative A && (B && C)  (A && B) && C A || (B || C)  (A || B) || C

FIT Axioms of Boolean Algebra (cont) Distributive A && (B || C)  (A && B) || (A && C) A || (B && C)  (A || B) && (A || C) … plus some “technicalities”

FIT De Morgan’s Law From the axioms of Boolean algebra it follows that: ! ( A || B )  !A && !B !(A && B)  !A || !B You can use all rules above to simplify / verify expressions. Exercise: simplify ! ( A || !B ) && A

FIT Short-circuiting A complex Boolean expression is only evaluated as far as necessary (from left to right) The test for y>20 will never be executed. This is important if one of the tests can cause a so-called side effect. int x = 99; int y = 30; x 20 Examples:

FIT Side Effects (Advanced, optional): A side effect is caused when an operation (eg a method call) causes a change to the state of the program other than just returning a value (for example, changing a variable, printing something…) public class Test { private final static int January = 1; … private int discount=0; private int month=January;

FIT Shortcircuits and Side Effects private boolean storeWideSale() { if (month==January) { discount=50; return true; } else return false; } private boolean onSale(int productNumber) { if (productNumber == 999) { discount=10; return true; } else return false; } public void sell(int productNumber) { if (onSale(productNumber) || storeWideSale() ) System.out.println(”You receive "+discount+" % discount"); } (Advanced, optional): With this code Item 999 will even in January only get 10% discount

FIT Shortcircuits and Side Effects public void sell(int productNumber) { boolean discounted = false; discounted = onSale(productNumber)|| discounted; discounted = storeWideSale() || discounted; if (discounted) System.out.println(”You receive "+discount+" % discount"); } (Advanced, optional): Better style: avoid side effects where possible always evaluate every statement that has side effects explicitly You can also avoid short-circuiting by using complete evaluation operators – or: “|” instead of “||” – and: “&” instead of “&&”