|
| 1 | +/***************************************************************************** |
| 2 | + * PROJECT: Java-101 Comprehensive Programming Course |
| 3 | + * MODULE: Introduction to Java Fundamentals |
| 4 | + * LESSON: 4.3 - Logical Operators |
| 5 | + * |
| 6 | + * AUTHOR: Dr. Saad Laouadi, Ph.D. |
| 7 | + * CREATED: April 2025 |
| 8 | + * |
| 9 | + * Copyright © 2025 Dr. Saad Laouadi. All Rights Reserved. |
| 10 | + * See LICENSE file for complete terms. |
| 11 | + *****************************************************************************/ |
| 12 | + |
| 13 | +public class LogicalOperators { |
| 14 | + public static void main(String[] args) { |
| 15 | + // Variables for demonstration |
| 16 | + boolean p = true; |
| 17 | + boolean q = false; |
| 18 | + int a = 10; |
| 19 | + int b = 20; |
| 20 | + |
| 21 | + displayBanner('*', 72); |
| 22 | + System.out.println("LOGICAL OPERATORS DEMONSTRATION"); |
| 23 | + System.out.println("==============================="); |
| 24 | + System.out.println("Working with variables: p = " + p + ", q = " + q); |
| 25 | + System.out.println("And numeric values: a = " + a + ", b = " + b); |
| 26 | + |
| 27 | + // Logical AND operator (&&) |
| 28 | + System.out.println("\n1. Logical AND (&&)"); |
| 29 | + System.out.println(" p && p: " + (p && p) + " (true AND true = true)"); |
| 30 | + System.out.println(" p && q: " + (p && q) + " (true AND false = false)"); |
| 31 | + System.out.println(" q && p: " + (q && p) + " (false AND true = false)"); |
| 32 | + System.out.println(" q && q: " + (q && q) + " (false AND false = false)"); |
| 33 | + |
| 34 | + System.out.println("\n Example with conditions:"); |
| 35 | + System.out.println(" (a < b) && (a > 5): " + ((a < b) && (a > 5)) + |
| 36 | + " (both conditions true)"); |
| 37 | + System.out.println(" (a > b) && (a > 5): " + ((a > b) && (a > 5)) + |
| 38 | + " (first condition false, second true)"); |
| 39 | + |
| 40 | + // Logical OR operator (||) |
| 41 | + System.out.println("\n2. Logical OR (||)"); |
| 42 | + System.out.println(" p || p: " + (p || p) + " (true OR true = true)"); |
| 43 | + System.out.println(" p || q: " + (p || q) + " (true OR false = true)"); |
| 44 | + System.out.println(" q || p: " + (q || p) + " (false OR true = true)"); |
| 45 | + System.out.println(" q || q: " + (q || q) + " (false OR false = false)"); |
| 46 | + |
| 47 | + System.out.println("\n Example with conditions:"); |
| 48 | + System.out.println(" (a < b) || (a > 5): " + ((a < b) || (a > 5)) + |
| 49 | + " (both conditions true)"); |
| 50 | + System.out.println(" (a > b) || (a > 5): " + ((a > b) || (a > 5)) + |
| 51 | + " (first condition false, second true)"); |
| 52 | + System.out.println(" (a > b) || (a < 5): " + ((a > b) || (a < 5)) + |
| 53 | + " (both conditions false)"); |
| 54 | + |
| 55 | + // Logical NOT operator (!) |
| 56 | + System.out.println("\n3. Logical NOT (!)"); |
| 57 | + System.out.println(" !p: " + (!p) + " (NOT true = false)"); |
| 58 | + System.out.println(" !q: " + (!q) + " (NOT false = true)"); |
| 59 | + System.out.println(" !(a < b): " + (!(a < b)) + " (NOT true = false)"); |
| 60 | + System.out.println(" !(a > b): " + (!(a > b)) + " (NOT false = true)"); |
| 61 | + |
| 62 | + // Short-circuit evaluation |
| 63 | + System.out.println("\n4. Short-Circuit Evaluation"); |
| 64 | + System.out.println(" In logical expressions, Java uses short-circuit evaluation."); |
| 65 | + System.out.println(" This means the second condition is only evaluated if needed."); |
| 66 | + |
| 67 | + // Short-circuit AND |
| 68 | + System.out.println("\n Short-Circuit AND Example:"); |
| 69 | + System.out.println(" false && (any expression): Second expression is never evaluated"); |
| 70 | + |
| 71 | + boolean result1 = q && isPositive(0); |
| 72 | + System.out.println(" q && isPositive(0): " + result1 + |
| 73 | + " (second expression not evaluated because q is false)"); |
| 74 | + |
| 75 | + // Short-circuit OR |
| 76 | + System.out.println("\n Short-Circuit OR Example:"); |
| 77 | + System.out.println(" true || (any expression): Second expression is never evaluated"); |
| 78 | + |
| 79 | + boolean result2 = p || isPositive(0); |
| 80 | + System.out.println(" p || isPositive(0): " + result2 + |
| 81 | + " (second expression not evaluated because p is true)"); |
| 82 | + |
| 83 | + // Combining logical operators |
| 84 | + System.out.println("\n5. Combining Logical Operators"); |
| 85 | + boolean complexExpression = (a > 5) && (b < 30) || (a + b > 25); |
| 86 | + System.out.println(" (a > 5) && (b < 30) || (a + b > 25): " + complexExpression); |
| 87 | + System.out.println(" Precedence: AND is evaluated before OR"); |
| 88 | + |
| 89 | + boolean withParentheses = (a > 5) && ((b < 30) || (a + b > 25)); |
| 90 | + System.out.println(" (a > 5) && ((b < 30) || (a + b > 25)): " + withParentheses); |
| 91 | + System.out.println(" Parentheses change the order of evaluation"); |
| 92 | + |
| 93 | + // Using logical operators in control flow |
| 94 | + System.out.println("\nUsing Logical Operators in Control Flow:"); |
| 95 | + |
| 96 | + if (a < b && a > 5) { |
| 97 | + System.out.println(" a is less than b AND a is greater than 5"); |
| 98 | + } |
| 99 | + |
| 100 | + if (a > b || a > 5) { |
| 101 | + System.out.println(" a is greater than b OR a is greater than 5"); |
| 102 | + } |
| 103 | + |
| 104 | + displayBanner('*', 72); |
| 105 | + } |
| 106 | + |
| 107 | + // Helper method to demonstrate short-circuit evaluation |
| 108 | + public static boolean isPositive(int num) { |
| 109 | + System.out.println(" isPositive() method was called!"); |
| 110 | + return num > 0; |
| 111 | + } |
| 112 | + static void displayBanner(char c, int length){ |
| 113 | + for (int i=0; i<length; i++){ |
| 114 | + System.out.print(c); |
| 115 | + } |
| 116 | + System.out.println(); |
| 117 | + } |
| 118 | +} |
0 commit comments