University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb 9 2006

Slides:



Advertisements
Similar presentations
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Advertisements

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
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.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
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.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selections Java.
Operator Precedence Operators Precedence Parentheses () unary
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Selection (if-then-else)
CS139 October 11, 2004.
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Controlling Program Flow
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb based on slides by Kurt Eiselt

Reading n This week: Chapter 6 all ( ) n Next week: Chapter 7 all ( ) n Reading summary so far: n Chap 1, 2, 3, 4, 6 n (no Chap 5!)

News n Next week is reading week n no lectures or labs or tutorials n Midterms returned today n Grades, statistics already posted on WebCT n returned end of class, line up by last name reversed (Z-A) n Assignment 1 was returned Tue n pick up after class if you don't have it yet

Midterm Results

News n Reminder: protocol for regrade requests n read solution and marking scheme first, carefully n no regrade requests accepted until at least 24 hours after material is handed back n exception: arithmetic errors n regrade requests must be in writing (paper or ) n assignments: to marker (listed on cover sheet) n if still have dispute after discussion with TA, can escalate to instructor n exams: to instructor

Recap: Static Methods n Static methods do not operate in context of particular object n cannot reference instance variables because they exist only in an instance of a class n compiler will give error if static method attempts to use nonstatic variable n Static method can reference static variables n because static variables exist independent of specific objects

Recap: Static Methods in java.Math n Java provides you with many pre-existing static methods n Package java.lang.Math is part of basic Java environment n you can use static methods provided by Math class n examples: > Math.sqrt(36) 6.0 > Math.sin(90) > Math.sin(Math.toRadians(90)) 1.0 > Math.max(54,70) 70 > Math.round( ) 3 > Math.random() > Math.random() > Math.pow(2,3) 8.0 > Math.pow(3,2) 9.0 > Math.log(1000) > Math.log10(1000) 3.0

Recap: Conditional Statement n Conditional statement: choose which statement will be executed next based on boolean expression n changes control flow n Example if (age < 20) System.out.println("Really, you look like you are " + (age + 5) + ".");

Recap: Boolean Expressions n Boolean expression: test which returns either true or false when evaluated n aka conditional n Consists of operands and operators, like arithmetic expression n but operators only return true or false when applied to operands n Two different kinds of operators n relational n sometime split into relational and equality n logical

Recap: Relational Operators n Tests two values (operands) n Operators n == equal n returns true if they are equal, false otherwise n note: do not confuse this with = n != not equal n returns true if they are not equal, false otherwise n < less than n <= less than or equal to n > greater than n >= greater than or equal to

Recap: Logical Operators n Way to combine results from relational operators into single test n AND, OR, and NOT n in terms from math or philosophy class n Operators n && logical AND n || logical OR n ! logical NOT

Objectives n Understand how to compare objects and primitive data types n Understand syntax to use for conditionals and switch statements

Comparing Strings n How do we test for equality between Strings? n Reminder: n Strings are sequences of alphanumeric characters n create with constructor n String firstname = new String("Donald"); n or with shortcut n String lastname = "Duck"; n Strings are objects, not primitive types!

Comparing Strings n Relational operator == is wrong way to compare String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1 == name2); // prints false n Equals method is right way to compare Strings String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1.equals(name2)); // prints true n why? diagrams will help

Comparing Strings n these values tested for equality with test of name1 == name2 n two different pointers (references), so false name1 name2 "Bubba"

Comparing Strings n these values tested for equality with name1.equals(name2) n contents of objects are same, so true name1 name2 "Bubba"

Short-Circuting Evaluation n Consider again expression if ((b > a) && (c == 10)) System.out.println("this should print"); n Java evaluates left to right n if (b>a) is false, does value of (c == 10) matter? n no! result of && must be false since one operand already evaluated to false n short-circuiting: Java does not evaluate n aka lazy evaluation

Short-Circuting Evaluation n Consider different expression if ((b > a) || (c == 10)) System.out.println("this should print"); n Java evaluates left to right n if (b>a) is true, does value of (c == 10) matter? n no! result of || must be true since one operand already evaluated to true

If Syntax n Syntax n reserved word if n followed by boolean expression enclosed in parentheses n followed by statement if (x == y) System.out.println("x equals y! "); n Results n if boolean evaluates to true, statement is executed n otherwise statement is skipped, execution continues with statement immediately following if statement

If-Else Syntax n If statement may include optional else clause n reserved word else n followed by another statement if (x == y) System.out.println("x equals y!"); else System.out.println("x is not equal to y!"); n Results n if boolean evaluates to true, first statement is executed n otherwise (if boolean evalutes to false), statement following else is executed

Block Statements n Often want to do many actions, not just one, based on condition n Replace single statement with many statements surrounded by curly braces if (x == y) { System.out.println("x equals y!"); System.out.println("I'm happy"); } else { System.out.println("x is not equal to y"); System.out.println("I'm depressed"); System.out.println("How about you?"); }

Block Statements n What if we leave out block in else clause? if (x == y) { System.out.println("x equals y!"); System.out.println("I'm happy"); } else System.out.println("x is not equal to y"); System.out.println("I'm depressed"); System.out.println("How about you?");

Nested If Syntax n Statements within if-else statements can themselves be if-else statements public class NestTest { public static void main (String[] args) { int x = 1; int y = 3; int z = 2; if (x == y) if (y == z) { System.out.println("all three values the same"); } else { System.out.println("y is not equal to z"); } else System.out.println("x is not equal to y"); }

Nested If Syntax n Multiple else statements also legal if( Boolean expression 1 ) { // statements } else if( Boolean expression 2 ) { // statements } else if( Boolean expression 3 ) { // statements } else { // statements }

Nested If Syntax n Rewriting NestTest using multiple else statements public class NestTest2 { public static void main (String[] args) { int x = 1; int y = 3; int z = 2; if ((x == y) && (y == z)) { System.out.println("all three values the same"); } else if ((x == y) && (y != z)) { System.out.println("y is not equal to z"); } else System.out.println("x is not equal to y"); }

Comparing Floating Point Numbers n Is 0.3 the same thing as 1.0/ / /10.0 ??? n Let’s try it out... `

Comparing Floating Point Numbers n Is 0.3 the same thing as 1.0/ / /10.0 ??? n No - very close, but not exactly what you expect n n Beware! Write tests for “darn near equal” like: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println (“Essentially equal.”); n where TOLERANCE is small number appropriate to problem like

Comparing Characters n You can compare character types with relational operators 'a' < 'b' 'a' == 'b' 'a' < 'A' n Remember, cannot compare Strings with relational operators n or any other objects! n must use methods like equals

Switch Syntax n Use switch statement to get program to follow one of several different paths based on single value switch (finalMark) { case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year"); }

Switch Syntax n Expression should be int, char n (or enumerated type) switch (finalMark) { case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year"); }

Switch Syntax n Case values cannot be variables switch (finalMark) { case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year"); }

Switch Syntax n Default statement optional, but very good idea switch (finalMark) { case 4 : System.out.println("You get an A"); break; case 3 : System.out.println("You get a B"); break; case 2 : System.out.println("You get a C"); break; default: System.out.println("See you next year"); }

Switch Syntax n Break statements really important switch (finalMark) { case 4 : System.out.println("You get an A"); break; case 3 : System.out.println("You get a B"); break; case 2 : System.out.println("You get a C"); break; default: System.out.println("See you next year"); }