Download presentation
Presentation is loading. Please wait.
1
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle
2
2 The Type Boolean The boolean type is a primitive data type whose value is either true or false. Associated with it are two symbolic constants, true and false. An example of a symbolic constant with which you are already familiar is . An example of a symbolic constant with which you are already familiar is .
3
3 The Type Boolean You declare variables of type boolean the same way you declare variables of type int, float, double, or char. Examples: boolean isPositive; boolean isEven; Examples: boolean isPositive; boolean isEven; You assign values to variables of type boolean the same way you assign values to variables of type int, float, double, or char. Examples: boolean isPositive = true; boolean isEven = false; Examples: boolean isPositive = true; boolean isEven = false;
4
4 The Type Boolean Let’s look at a Java program that uses boolean variables: EvenOrOdd.java EvenOrOdd.java
5
5 Boolean (or Logical) Operators The Java logical operators can be used to form complex conditions by combining two or more simple conditions. The most frequently used logical operators are: && (and) && (and) || (or) || (or) ! (not) ! (not)
6
6 Boolean (or Logical) Operators && (and) Usage: Usage: When used to combine two simple conditions, the resulting condition is true if both of the simple conditions are true; false, otherwise Format: (Condition1 && Condition2) // Condition1 and Condition2 // are simple conditions Format: (Condition1 && Condition2) // Condition1 and Condition2 // are simple conditions Example: char letter = 'd'; if (letter >= 'A' && letter = 'a' && letter = 'A' && letter = 'a' && letter <= 'z') out.writeln("Lowercase");
7
7 The Type Boolean Let’s look at a Java program that uses the and operator: UpperOrLower.java UpperOrLower.java
8
8 Boolean (or Logical) Operators || (or) Usage: Usage: When used to combine two simple conditions, the resulting condition is true if at least one of the simple conditions is true; false, if both conditions are false Format: (Condition1 || Condition2) // Condition1 and Condition2 // are simple conditions Format: (Condition1 || Condition2) // Condition1 and Condition2 // are simple conditions Example: float examGrade = 80; if (examGrade 100) out.writeln("Invalid grade!"); else out.writeln("Valid grade!"); Example: float examGrade = 80; if (examGrade 100) out.writeln("Invalid grade!"); else out.writeln("Valid grade!");
9
9 The Type Boolean Let’s look at a Java program that uses the or operator: Grade3.java Grade3.java
10
10 Boolean (or Logical) Operators ! (not) Usage: Usage: Used to negate the value of a condition; i.e., returns the opposite value of a condition Format: (!Condition1)// Condition1 is a simple condition Format: (!Condition1)// Condition1 is a simple condition Example: boolean isPositive = false; boolean isNegative = !isPositive; if (isNegative) out.writeln("The number is negative."); else out.writeln("The number is positive."); Example: boolean isPositive = false; boolean isNegative = !isPositive; if (isNegative) out.writeln("The number is negative."); else out.writeln("The number is positive.");
11
11 Truth Table for the Boolean && (and) Operator Value of X Value of Y Resulting Value of X && Y truetruetrue truefalsefalse falsetruefalse falsefalsefalse
12
12 Truth Table for the Boolean || (or) Operator Value of X Value of Y Resulting Value of X || Y truetruetrue truefalsetrue falsetruetrue falsefalsefalse
13
13 Truth Table for the Boolean ! (not) Operator Value of X Resulting Value of !X truefalse falsetrue
14
14 Operator Precedence Revisited First: Unary operators +, -, ! Second: Binary arithmetic operators *, /, % Third: Binary arithmetic operators +, - Fourth: Boolean operators, = Fifth: Boolean operators ==, != Sixth: Boolean operator & (similar to &&) Seventh: Boolean operator | (similar to ||) Eighth: Boolean operator && Ninth: Boolean operator || Highest Precedence Lowest Precedence
15
15 Conditional Operator (? :) The conditional operator (?:) can be used in place of an if-else statement. Example: out.writeln( grade >= 60 ? "You passed! :-)" : "You didn’t pass. :-(" ); is equivalent to: if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-(");
16
16 Conditional Operator (? :) Example: out.writeln( grade >= 60 ? "You passed! :-)" : "You didn’t pass. :-(" ); To clarify: The boolean expression to the left of the ? Is the condition. The boolean expression to the left of the ? Is the condition. The expression between the ? And the : is the value of the boolean expression if the condition evaluates to true. The expression between the ? And the : is the value of the boolean expression if the condition evaluates to true. The expression to the right of the : is the value of the boolean expression if the condition evaluates to false. The expression to the right of the : is the value of the boolean expression if the condition evaluates to false.
17
17 Conditional Operator (? :) Note: I would “spare you” this operator were it not for the fact that it appears in Lab 3. I would “spare you” this operator were it not for the fact that it appears in Lab 3.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.