Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 4 - Control Statements
CS0007: Introduction to Computer Programming
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
C++ for Engineers and Scientists Third Edition
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Problem Solving with Decisions
Conditionals & boolean operators
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Lesson Two: Everything You Need to Know
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
COMP Loop Statements Yi Hong May 21, 2015.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control Statements: Part1  if, if…else, switch 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Random Functions Selection Structure Comparison Operators Logical Operator
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 – Control Structures Part 1
Microsoft Visual Basic 2005 BASICS
Programming 2 Decision-Making.
Selection Statements.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Unit 3: Variables in Java
Chapter 5: The Selection Structure
Presentation transcript:

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals (if statement) COMP 102 # T2

© Xiaoying Gao, Peter Andreae COMP :2 Admin A3 due Wed 10am Q1: call a method in the same class, arguments match parameters Q2: create objects, call methods form objects Help desk Tuesday 1-2 CO242B, tutorial Tuesday 12-1 CO219 Online help, forum And Me Reading Chapter 4: if statement

© Xiaoying Gao, Peter Andreae COMP :3 Summary: Call a method If the method is in the same class Call from this this.timeMeasure(…) Not in the same class If it is static, call from ClassName UI.askString(…) Not static, call from an object bf1.move(…) To create an object Butterfly bf1 = new Butterfly(200, 100); CartoonFigure jim = new CartoonFigure(“yellow”, 100, 150); What data to use: arguments ObjectName.methodName(arguments)

© Xiaoying Gao, Peter Andreae COMP :4 Making Decisions Examples:

© Xiaoying Gao, Peter Andreae COMP :5 Programs that make decisions Typically, programs perform the instructions sequentially, one step at a time, from top to bottom. Programs that perform the same action every time are boring! You can vary the action in a program! By calling method with different arguments: UI.setColor(col); UI.drawRect(left, top, width, height); By getting input from the user: String action = UI.askString(“What would you like to draw: ”); : UI.drawRect(left, top, width, height); UI.printf(“This is a %s”, action);

© Xiaoying Gao, Peter Andreae COMP :6 Programs that make decisions But this just changes the values, not the action itself. To vary the action inside a method: Need a conditional statement (choice statement) To respond to user's action: If user wants a rectangle, draw a rectangle

© Xiaoying Gao, Peter Andreae COMP :7 Decisions in Java Java has an if statement : Can do an action only in some circumstances: if ( action.equals(“rectangle") ) { UI.drawRect(left, top, width, height); } Java also has an if...else statement: Can choose between different actions: if ( action.equals(“rectangle”)) { UI.drawRect(left, top, width, height); } else { UI.drawOval(left, top, width, height); }

© Xiaoying Gao, Peter Andreae COMP :8 Java: if and if … else LDC 4.2 Forms of the if statement: if ( condition ) { actions to perform if condition is true } ⇒ just skip the actions when the condition is not true ! Forms of the if...else statement: if ( condition ) { actions to perform if condition is true } else { actions to perform if condition is false } Note: the { … } represent a “Block” – a sequence of actions that are wrapped up together into a single statement.

© Xiaoying Gao, Peter Andreae COMP :9 Example: method with conditions /** Ask for amount and currency; note if –ve, print note*/ public void convertMoney( ) { double amt = UI.askDouble(“Enter amount $NZ: ”); if ( amt < 0 ) { UI.println(“Note: you have entered a debt!”); } String cur = UI.askString(“Enter currency (US or Aus): ”); if ( cur.equals(“US”) ) { UI.printf(“$NZ%.2f = $US%.2f\n”, amt, (amt * 0.811)); } else { UI.printf(“$NZ%.2f = $AUS%.2f\n”, amt, (amt * 0.875)); } }

© Xiaoying Gao, Peter Andreae COMP :10 if … vs if … else … if(boolean valued expression{ } else statements ) { }

© Xiaoying Gao, Peter Andreae COMP :11 Multiple choices: if … else if … else if … Can put another if statement in the else part: if ( 〈 condition 1 〉 ) { 〈 actions to perform if condition1 is true 〉 : } else if ( 〈 condition 2 〉 ) { 〈 actions to perform if condition 2 is true (but not condition 1) 〉 : } else if ( 〈 condition 3 〉 ) { 〈 actions to perform if condition 3 is true (but not conditions 1 & 2 〉 : } else { 〈 actions to perform if condition is false 〉 : }

© Xiaoying Gao, Peter Andreae COMP :12 Example: method with multiple choices public void convertMoney( ) { double amt = UI.askDouble(“Enter amount”); if ( amt < 0 ) { UI.println(“Note: you have entered a debt!”); } String cur = UI.askString(“Enter currency (US or Aus)”); if ( cur.equals(“US”) ) { UI.printf(“$NZ%.2f = $US%.2f\n”, amt, amt * 0.811); } else if ( cur.equals(“Aus”) ) { UI.printf(“$NZ%.2f = $AUS%.2f\n”, amt, amt * 0.875); } else { UI.printf(“I cannot convert to %s currency\n”, cur); } Use “.equals” to compare Strings

© Xiaoying Gao, Peter Andreae COMP :13 If statement can be nested public void convertMoneyNestedIf( ) { double amt = UI.askDouble("Enter amount $NZ: "); if ( amt > 0 ) { String cur = UI.askString("Enter currency (US or Aus): "); if ( cur.equals("US") ) { UI.printf("$NZ%.2f = $US%.2f\n", amt, (amt * 0.811)); } else { UI.printf("$NZ%.2f = $AUS%.2f\n", amt, (amt * 0.875)); } else{ UI.println("The amount must be positive"); }

© Xiaoying Gao, Peter Andreae COMP :14 Example 2: with multiple choices public void printTutorPay( int day, int hours ) { double rate = 22.5; double pay = rate * hours; if ( day > 7 ) { UI.println(“Day must be between 1 and 7”); } else if ( day < 6 ) { UI.printf(“Pay = $ %4.2f \n”, pay); } else if ( day == 6 ) { pay = pay * 1.5; UI.printf(“Pay = $ %4.2f ( time-and-a-half) \n”, pay); } else { pay = pay * 2; UI.printf(“Pay = $ %4.2f ( double-time) \n”, pay); } …. this.printTutorPay(……., …….); Use “==” to compare numbers

© Xiaoying Gao, Peter Andreae COMP :15 Writing Boolean expressions Mostly, boolean expressions are straightforward, There are just a few traps: == is the “equals” operator, = is assignment But only use == for numbers (or characters, or references) age == 15 vs age = 15 ; Use the equals method for Strings, not == (occasionally == will give the right answer by chance!) cur.equals(“US”) vs cur == “US” String equality is case sensitive: “US”.equals(“us”) → false “US”.equalsIgnoreCase(“us”) → true

© Xiaoying Gao, Peter Andreae COMP :16 Writing Boolean expressions When combining with && and ||, which binds tighter ? if ( x > 5 && y <= z || day == 0 ) { …. Use ( and ) whenever you are not sure! if ( ( x > 5 && y <= z ) || day == 0 ) { … if ( x > 5 && ( y <= z || day == 0 ) ) { … The not operator ! goes in front of expressions: if ( !(x > 5 && y <= z) ) { … if ( ! cur.equals(“US”) ) { … if ( ! (count == 0) ) { … OR if ( count != 0 ) { …

© Xiaoying Gao, Peter Andreae COMP :17 Boolean Variables A boolean value is a value! ⇒ it can be stored in a variable. Useful if the program needs to remember some option. Must declare the variable, and assign to it, before using it boolean printSteps = UI.askBoolean(“Print all steps?”); : if ( printSteps ) UI.println(“Processed input”); : if ( printSteps ) UI.println(“Computed Statistics”); :

© Xiaoying Gao, Peter Andreae COMP :18 Boolean expressions LDC 4.1 What can go in the condition of an if statement ? A Boolean value – a value that is either true or false. Boolean expressions: constant values:true, false numeric comparisons: (x > 0) (day <= 7), (x == y), (day != 7) boolean method calls:UI.askBoolean(“End program?”) month.equals(“July”) name.equalsIgnoreCase(“John”) boolean variables: outlineOnly [declared: boolean outlineOnly; ] logical operators:!, &&, || (not, and, or) ( x > 0 && x < 7 && outlineOnly ) ( month.startsWith(“Ju”) || month.equals(“May”) ) ( ! fileModified || ! (cmd.equals(“exit”)) )

© Xiaoying Gao, Peter Andreae COMP :19 Exercise public void testIt(int x){ if ( x = 15 ) { UI.print("A "); } if ( x < 7 && x != 4 ) { UI.print("B "); } if ( ! (x == 4 || x == 10) ) { UI.print("C "); } UI.println(); } this.testIt(4) this.testIt(6) this.testIt(8)

© Xiaoying Gao, Peter Andreae COMP :20 Exercise Marks to grades >=75 A 60~75 B 50~60 C 40~50 D <40 E Write a method Ask the user for a number Print a grade