Download presentation
Presentation is loading. Please wait.
Published byAshlee Whitehead Modified over 9 years ago
1
CS 139 – Programming Fundamentals Lecture 08A
2
Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to
3
Example – Relational operator
4
Boolean/Logical Operators Logical “and” (conjunction) –true only when both expressions are true (A 5) ---- True Logical inclusive “or” (disjunction) –true when either or both expressions are true (A<B) || (C<D) // (3<4) || (10<5) ---- True Logical “not” (negation)- reverses the truth of a Boolean expression (MINIMUM_WAGE != wages) Logical operator connect two or more relational expressions into one or reverse the logic of an expression
5
Boolean/Logical Operator Truth Table
6
Expression Data Type of result Result (!(true == false)) || (6.0 < 4.0) booleantrue “Your guess is " + (5 == 5) String Your guess is true (20 – 10) > 5 > (6 - 8)Invalid (true || false) && (!(false || false))booleanTrue Expression evaluation
7
if-else/Decision statement
8
Decisions (if) if (boolean_expression) { statement 1; } int x = 10; if (x > 0) { System.out.println(“x is positive”); } Note: else is not mandatory !!
9
Decisions (if-else) if (boolean_expression) { statement 1; } else { statement 2; } int x = 10; if (x >= 0) { System.out.println(“x is positive”); } else { System.out.println(“x is negative”); } Note: if and else are exclusive or branch !
10
Decisions (if-else if-else) if (boolean_expression) { statement 1; } else if(boolean_expression) { statement 2; } else { statement 3; } int x = 10; if (x > 0) { System.out.println(“x is positive”); } else if (x < 0) { System.out.println(“x is negative”); } else System.out.println(“x is zero”); Note: testing more than 2 exclusive or conditions
11
Decisions (Logical operators) int x = 10; int y = -5; if (x > 0 && y > 0) System.out.println(“x & y are positive”); else if (x > 0 && y < 0) System.out.println(“y is negative”); else if (x 0) System.out.println(“x is negative”); else System.out.println(“x & y are negative”);
12
Comparing Strings if (str1 == str2) { statement 1; } else { statement 2; } String str1 = “CS139”; String str2 = “CS139”; if(str1 == str2) { System.out.println(“They are the same.”); } else { System.out.println(“They are different”); } What are we testing with == ?
13
Comparing Strings String str1 = “CS139”; String str2 = “CS139”; if(str1.equals(str2) { System.out.println(“They are the same.”); } else { System.out.println(“They are different”); } if (str1.equals(str2)) { statement 1; } else { statement 2; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.