2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.

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
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
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.
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.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
E0001 Computers in Engineering Procedures: subprograms and functions.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Conditionals & boolean operators
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
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.
Lesson Two: Everything You Need to Know
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
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 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 Programs with Choice Booleans,
COMP Loop Statements Yi Hong May 21, 2015.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
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.
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.
Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
CS0007: Introduction to Computer Programming
Bill Tucker Austin Community College COSC 1315
The Selection Structure
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.
Life is Full of Alternatives
Unit 3: Variables in Java
The Selection Structure
Chapter 5: The Selection Structure
Presentation transcript:

2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda Conditionals and Boolean Expressions

2 RECAP-TODAY RECAP  “Wishful programming” (Top-down design) TODAY  Recall key concepts  Conditionals LDC 4.2  Boolean expressionsLDC 4.1 and 4.3 Announcements  Terms Test#1: This Thursday (24 th March, 3pm) –  A – Ley:MC LT103,  Li – Ross:CO LT122,  Rundle – Z:KK LT301  Test Review session: 4pm today, AM101  Equity help session: will start next week  Videos of test questions, assignments, on web site

3 The story so far...  What key concepts have we covered ?  How many are mirrored from the real world ?

4 Making Decisions  Examples:

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);

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

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); }

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.

9 if … vs if … else … if(boolean valued expression{ } else statements ) { }

10 Example: method with conditions /** Ask for amount and currency; note if –ve, print value.*/ 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.711)); } else { UI.printf(“$NZ%.2f = $AUS%.2f\n”, amt, (amt * 0.775)); } }

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 〉 : }

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.711); } else if ( cur.equals(“Aus”) ) { UI.printf(“$NZ%.2f = $AUS%.2f\n”, amt, amt * 0.775); } else { UI.printf(“I cannot convert to %s currency\n”, cur); } Use “.equals” to compare Strings

13 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.printPay(today, hoursWorked); ….. Use “==” to compare numbers

14 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: month.equals(“July”) name.equalsIgnoreCase(“John”) UI.askBoolean(“End program?”)  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”)) ) More String methods

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

16 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”); :

17 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 ) { …

18 Summary  What did we learn today ?