Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle."— Presentation transcript:

1 Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle

2 Boolean Expressions A boolean expression is an expression that evaluates to true or false. The word boolean is derived from the name of George Boole, a 19 th -century English logician and mathematician whose work was associated with expressions of this type.

3 Relational Operators Relational operators are used to form boolean expressions. Relational operators can be used to compare two values of the same primitive (i.e., simple) type, such as int or char. The relational operators we use in Java are the same as those we use in algebra, except that some of them look a little different.

4 Relational Operators Algebraic Operator Java Operator Java Expression Meaning of Java Expression >> num1 > num2 num1 is greater than num2 ≥>= num1 >= num2 num1 is greater than or equal to num2 << num1 < num2 num1 is less than num2 ≤<= num1 <= num2 num1 is less than or equal to num2 === num1 == num2 num1 is equal to num2 ≠!= num1 != num2 num1 is not equal to num2 For the examples given in the table below, assume that num1 and num2 are both integer ( int ) types.

5 Additional Examples of Boolean Expressions in Java Comparing integers int num1 = 5, num2 = 8, num3 = 13; num2 >= num1 // evaluates to true num1 == num3 // evaluates to false Comparing characters char letter1 = 'a', letter2 = ‘B’, letter3 = 'c'; letter1 < letter3 // evaluates to true letter1 < letter2 // evaluates to false (why?)

6 ASCII/Unicode Values for Letter and Digit Characters Character ASCII/Unicode Value '0' through '9' 48 through 57 'A' through 'Z' 65 through 90 'a' through 'z' 97 through 122

7 Question? Can anyone now explain why the second example comparing letters on slide 5 evaluates to false? Here it is again: char letter1 = 'a', letter2 = ‘B’; letter1 < letter2 // evaluates to false (why?)

8 Additional Examples of Boolean Expressions in Java Comparing strings Strings cannot be compared using the relational operators in Java (Aside: C++ yes, Java no). Strings cannot be compared using the relational operators in Java (Aside: C++ yes, Java no). Counter example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName == upperName // doesn’t work; actually // compares the locations of the // String objects in memory // instead of their contents Counter example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName == upperName // doesn’t work; actually // compares the locations of the // String objects in memory // instead of their contents

9 Additional Examples of Boolean Expressions in Java Comparing strings Strings can be compared using the methods implemented in the String class. Strings can be compared using the methods implemented in the String class. Example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName.equals(upperName) // evaluates to false upperName.equals(lowerName) // evaluates to false // (why?) upperName.equalsIgnoreCase(lowerName) // evaluates to true // (why?) Example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName.equals(upperName) // evaluates to false upperName.equals(lowerName) // evaluates to false // (why?) upperName.equalsIgnoreCase(lowerName) // evaluates to true // (why?)

10 Control Structures The Java programs we have written so far have consisted of statements that execute in the order in which they appear in our source code. The Java programs we have written so far have consisted of statements that execute in the order in which they appear in our source code. We will not always want every statement to execute, however. As a result, Java provides us with control, or decision, structures which enable us to control what statements in a program execute. These control structures can be divided into two general categories: Selection structures Selection structures Repetition structures Repetition structures

11 Selection Structures A selection structure is used to choose among alternative courses of action. There are several types: if if if-else if-else If-else-if If-else-if switch switch

12 Selection Structures if Usage: Usage: Used to test if a single boolean expression is true or false Statements are only executed if the boolean expression is true Note: Boolean expressions are also called conditions Note: Boolean expressions are also called conditions Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Example: float grade = 95; if (grade >= 90) out.writeln("You got an A!“); Example: float grade = 95; if (grade >= 90) out.writeln("You got an A!“);

13 Selection Structures if-else Usage: Usage: Used in either-or situations The statements executed depend on whether the condition is true or false Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } else { Statement(s) } Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } else { Statement(s) } Example: float grade = 85; if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-("); Example: float grade = 85; if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-(");

14 Selection Structures if-else-if Usage: Usage: Used in situations where there are multiple alternatives (more than two choices) The statements executed depend on which condition is true Format: if (Condition1) { // Braces are only needed when there // are multiple statements Statement(s) } else if (Condition2) { Statement(s) } else if (Condition3) { Statement(s) } else if … Format: if (Condition1) { // Braces are only needed when there // are multiple statements Statement(s) } else if (Condition2) { Statement(s) } else if (Condition3) { Statement(s) } else if …

15 Selection Structures if-else-if Example: float grade = 75; if (grade >= 90) out.writeln("You got an A!"); else if (grade >= 80) out.writeln("You got a B."); else if (grade >= 70) out.writeln("You got a C."); else if (grade >= 60) out.writeln("You got a D."); else out.writeln("You got an F."); Example: float grade = 75; if (grade >= 90) out.writeln("You got an A!"); else if (grade >= 80) out.writeln("You got a B."); else if (grade >= 70) out.writeln("You got a C."); else if (grade >= 60) out.writeln("You got a D."); else out.writeln("You got an F.");


Download ppt "Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle."

Similar presentations


Ads by Google