If Statement if (amount <= balance) balance = balance - amount;

Slides:



Advertisements
Similar presentations
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
Advertisements

String and Object Comparison Comparison operator.
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Chapter 5  Decisions 1 Chapter 5 Decisions. Chapter 5  Decisions 2 Chapter Goals  To be able to implement decisions using if statements  To understand.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Chapter 5: Decisions. To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 5 Decisions. Assignments  Written exercises pp 217 – 220 R5.1, R5.3, R5.5, R5.6, R5.9 – R5.15, R5.17, R R5.1, R5.3, R5.5, R5.6, R5.9 –
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
Decisions: Handling Infinite Cases CS 21a: Introduction to Computing I First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
Fall 2006Slides adapted from Java Concepts companion slides1 Decisions Advanced Programming ICOM 4015 Lecture 5 Reading: Java Concepts Chapter 6.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
If Statement if (amount
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Chapter 6 Conditionals.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 5/6 Decisions.
Chapter 4: Making Decisions.
Chapter 4: Control Structures I
Control Structure Chapter 3.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 2 Programming Basics.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structure.
Chapter 6 Conditionals.
Presentation transcript:

if Statement if (amount <= balance) balance = balance - amount;

if/else Statement if (amount <= balance) balance = balance - amount; else balance = balanceOVERDRAFT_PENALTY;

Syntax if (boolean expression) //use for 1 way decision statement; if (boolean expression) //use for 2 way decision statement1; else statement2; Boolean expression is an expression which evaluates to true or false Only 1 statement allowed where statement indicated.

Relational Operators evaluates to true if …. a < b a is less than b a > b a is greater than b a <= b a is less than or equal to b a >= b a is greater than or equal to b a != b a is not equal to b a == b a is equal to b

Block Statement if (amount <= balance) { double newBalance = balance - amount; balance = newBalance; } Note: block allows more than one ‘statement’ to be combined, to form a new ‘statement’

Equality Testing vs. Assignment  The = = operator tests for equality: if (x = = 0).. // if x equals zero  The = operator assigns a value to a variable: x = 0; // assign 0 to x  Don't confuse them.

Comparing Floating-Point Numbers  Consider this code: double r = Math.sqrt(2); double d = r * r -2; if (d == 0) System.out.println( "sqrt(2)squared minus 2 is 0"); else System.out.println( "sqrt(2)squared minus 2 is not 0 but " + d);

 It prints: sqrt(2)squared minus 2 is not 0 but E-16  Don't use == to compare floating-point numbers

Comparing Floating-Point Numbers  Two numbers are close to another if |x - y| <= ε  ε is a small number such as  Example: if ( Math.abs(x-y) < ) statement ;

String Comparison Don't use = = for strings! if (input = = "Y") // WRONG!!!  Use equals method: if (input.equals("Y"))  = = tests identity, equals tests equal contents  Case insensitive test ("Y" or "y") if (input.equalsIgnoreCase("Y"))

Lexicographic Comparison  if s and t are strings, if (s < t) also does not make sense  s.compareTo(t) < 0 means: s comes before t in the dictionary  "car"comes before "cargo" "cargo" comes before "cathode"  All uppercase letters come before lowercase: "Hello" comes before "car"

Lexicographic Comparison

Object Comparison  = = tests for identical object  equals for identical object content  Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Rectangle oatmealBox = new Rectangle(5, 10, 20, 30); Rectangle r = cerealBox;  cerealBox != oatmealBox, but cerealBox.equals(oatmealBox)  cerealBox == r Caveat: equals must be defined for the class (chap 11)

Object Comparison

The null Reference  null reference refers to no object at all  Can be used in tests: if (account == null)...  Use ==, not equals, to test for null  showInputDialog returns null if the user hit the cancel button: String input = JOptionPane.showInputDialog("..."); if (input != null) {... }  null is not the same as the empty string ""

Multiple Alternatives if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; else statement4; The first matching condition is executed. Order matters.

Write a method which accepts a double value (a grade average), and returns the letter grade which would be awarded. Assume 90 and above -- an A 80 and under a B 70 and under a C 65 and under a D under an F

Nested Branches Branch inside another branch if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;

Write a method accepts a grade point average parameter, and returns the String “Congratulations” if the GPA is 3.5 and above, but returns “Sorry” if the GPA is below 2.0. The method will return “Provide References” is the GPA does not fall into either of the above two categories.

The boolean Type  George Boole ( ): pioneer in the study of logic  value of expression amount < 1000 is true or false.  boolean type: set of 2 values, true and false

Predicate Method  return type boolean  Example public boolean isOverdrawn(){ return balance < 0; }  Use in conditions if (myaccount.isOverdrawn()) statement;

Boolean Operators  ! not  && and (short circuited)  || or (short circuited)

Truth Tables

Write a method called ‘even’ which accepts one int parameter and returns true if that value is even. Otherwise false is returned. Write a method which accepts three int parameters and returns the largest of the three. Write an application which asks the user for 3 int values and outputs the parity (even or odd) of the sum of the three, and the largest.

private boolean FirstLetterIsAVowel() { char first = word.charAt(0); return (first==‘a’ || first==‘e’ || first==‘i’|| first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’); } Problem: For the Pig Latin algorithm on the right, write a method that tests if first letter is vowel. if (the first letter of word is a vowel) return word as it is else modify word and return it private boolean FirstLetterIsAVowel() { char first = word.charAt(0); if (first == ‘a’ || first == ‘e’ || first == ‘i’ || first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’) return true; else return false; } First variant of the test Second variant of the test private boolean FirstLetterIsAVowel(){ string vowels = “aeiouyAEIOUY”; char first = word.charAt(0); return (vowels.indexOf(first) > -1); } Third variant of the test public void translateWord() { if (FirstLetterIsAVowel() ) return (word); else return(word.substring(1) + word.charAt(0) + “ay”); } Method translateWord()

Boolean Variables boolean married; married = input.equals("M"); //set to boolean value if (married == true) statement; else statement;

Switch statement switch ( test expression ) { case expression1: statement1; case expression2: statement2; ….. default: default statement; } ** matches and executes down ** expressions must be of primitive non-decimal type

switch ( test expression ) { case expression1: statement1; break; //to avoid fall thru case expression2: statement2; break; default: default statement; }

Write a method called “twelveDays” which accepts one in parameter, (a value which will be between 1 and 12 inclusive), and returns a string containing the item given on that day in the song “The Twelve Days of Christmas”. Write an application which prints out the lyrics to this song, using the method twelveDays.