# The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.

## Presentation on theme: "The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005."— Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today Control Structures Relational Operators Comparing Strings Boolean Expressions If statements If-Else statements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Example: Sorting Cards Some words used in the algorithm: when, until, if, otherwise, once… How can we express them in Java?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Control Structures Three methods of processing a program: 1.In sequence ♦ statements are executed one after another in order 2.Branching ♦ altering the flow of program execution by making a selection or choice 3.Looping ♦ altering the flow of program execution by repetition of statement(s)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Flow of Execution

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Examples of Conditionals Note: the following are not actual Java statements 1) if (score is greater than or equal to 90) grade is A 2) if (hours worked are less than or equal to 40) wages = rate * hours otherwise wages = (rate * 40) + 1.5 * (rate * (hours - 40)) 3) if (temperature is greater than 70 degrees and it is not raining) recommended activity is golfing

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Relational Operators Relational Operator ♦ allows you to make comparisons in a program ♦ binary operator needs two operands Condition is represented by a logical (Boolean) expression ♦ expression that has a value of either true or false

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Relational Operators Less than < Greater than > Equal to == ♦ not assignment ‘ = ‘ Not equal to != Less than or equal to <= Greater than or equal to >=

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Comparing Characters In Java, characters are ordered according to the Unicode / ASCII character set (pg. 855) ‘a’ comes before ‘b’ in the character set, so we can say ‘a’ < ‘b’ Order ♦ space character (' ') ♦ digits (‘0’, ‘1’, …) ♦ uppercase ♦ lowercase Application: sort alphabetically

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Comparing Characters 'a' > 'A' '6' < 7 ' ' <= 's' 97 > 65 true 54 < 7 false 32 <= 115 true

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Comparing Floating-Point Be careful when using the equality ( == ) to compare floating point numbers Every bit in the representation must be equal ♦ computer can only store a certain number of digits after the decimal If the numbers are results of computation, it’s unlikely that two floating point numbers will be exactly equal

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Comparing Floating-Point 3.0 / 7.0 2.0 / 7.0 (3.0 / 7.0) + (2.0 / 7.0) + (2.0 / 7.0) == 1 0.4285714285714285 false 0.2857142857142857 If we did this math with exact arithmetic, this expression would be true 0.9999999999999999

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Comparing Strings Strings are compared on a character- by-character basis ♦ first character not in common determines how the strings compare ♦ ex: "Airplane" is less than "Airport" If two strings have different lengths, and one is a substring of the other, the shorter one is evaluated as less ♦ ex: "Air" is less than "Airplane"

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Comparing Strings Don't use relational operators (==, ) for Strings str1.compareTo (str2) Returns an integer value: ♦ < 0 if str1 is less than str2 ♦ 0 if str1 is equal to str2 ♦ >0 if str1 is greater than str2 str1.equals (str2) Returns a boolean value of true or false method parameter

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Questions Given String str = "Homer"; str.compareTo("Marge") str.equals("homer") str.compareTo("Bart") negative value false positive value

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Boolean Expressions Remember that booleans in Java are ♦ true ♦ false A boolean expression is an expression that can be evaluated to either true or false Examples of boolean expressions: ♦ 2+2 is not equal to 5 ♦ a is equal to 0 ♦ 10 is less than 5 Named for George Boole (1815-1864), the founder of symbolic logic

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Boolean Operators NOT ! (unary) !(2+2==5) AND && (binary) (2+2==5) && (1+1==2) OR || (binary) (2+2==5) || (1+1==2) true false true

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 NOT Operator

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 AND and OR Operators

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Boolean Expressions Examples Simple: ♦ 2+2 is not equal to 5 2+2 != 5 ♦ 10 is less than 5 10 < 5 ♦ 'A' is equal to 7 'A' == 7 Compound: ♦ a is equal to 3 and b is equal to 5 (a==3) && (b==5) ♦ x is not equal to 2 or y is less than 4 (x!=2) || (y<4) ♦ s is not equal to b and t is greater than or equal to 3 or t is less than 0 (s!=b) && (t>=3) || (t<0)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Short Circuited Operators The processing of logical AND ( && ) and logical OR ( || ) is “short- circuited” If the left operand is sufficient to determine the result, the right operand is not evaluated count != 0 && (total/count > MAX)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Precedence of Operators

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Boolean Expressions Logical expression that evaluates to true if the value of num is between 0 and 10 0 <= num <= 10 0 <= num && num <= 10 incorrect syntax correct syntax

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 Questions 1.What type of primitive variable can the result of a logical expression be stored in?_______ 2.Under what conditions would the expression ((ch >= 'A') && (ch <= 'Z')) evaluate to false ? ______________________ 3.What method do you use to compare two Strings? __________ 4.Why is 'a' greater than 'A'? ______________________________________ boolean ch 'Z' compareTo 'a' comes after 'A' in the ASCII character table

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 Questions Given the following declarations, evaluate each Boolean expression: int count = 0, sum = 54; double x = 4.3, y = 1.2; boolean wrong = true; 1.(wrong && sum > 60) 2.((x > 5) || !(sum == 55)) 3.!((y > 1.0) && (x < 4)) 4.((count != 4) || (sum > 100) && wrong) false true (false || !(false)) !(true && false) (true || false && true) (true && false) (true || false)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 Conditional Statements Let us choose which statement will be executed next ♦ also called selection statements Java's conditional statements: ♦ the if statement ♦ the if-else statement ♦ the switch statement (tomorrow’s class)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 One-Way Selection Syntax: if (expression) statement

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 The if Statement if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 29 The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not. If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 Block Statements Syntax: { statement1 statement2. statementn } We use curly braces to group a set of individual statements. This way we can have multiple statements execute based on a decision. If we don't use curly braces, only a single statement will be executed based on the result of a decision.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 31 The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); A if (sum > MAX) { delta = sum - MAX; } System.out.println ("The sum is " + sum); B if (sum > MAX) { delta = sum - MAX; System.out.println ("The sum is " + sum); } C

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 32 Curly Braces int num = 87, max = 25; if (num >= max*2) { System.out.println ("apple"); } System.out.println ("orange"); System.out.println ("pear"); int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear");

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 33 ToLower.java Example

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 34 Watch out! if (score >= 90); grade = "A"; No matter the result of the condition, grade will be assigned "A". The semicolon after the if statement is a semantic error.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 35 Two-Way Selection

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 36 The if-else Statement if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 37 The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 38 ToLower.java Continued

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 39 To do HW 2 ♦ Add if statements Reading: Chapter 4 (pp. 195 - end) – Multiple Selections Bring laptop