|
| 1 | +Java Conditional Statements – Study Material |
| 2 | +Overview |
| 3 | +This session covers Java conditional statements, a key concept used to control the flow of execution in Java programs based on certain conditions. These are crucial when automating test cases using Selenium. |
| 4 | + |
| 5 | +Types of Conditional Statements in Java |
| 6 | +1. if Statement |
| 7 | +Syntax: |
| 8 | + |
| 9 | + java |
| 10 | +CopyEdit |
| 11 | +if (condition) { |
| 12 | + // code to execute if condition is true |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +Usage: Executes a block only if the condition is true. |
| 17 | + |
| 18 | + |
| 19 | +2. if-else Statement |
| 20 | +Syntax: |
| 21 | + |
| 22 | + java |
| 23 | +CopyEdit |
| 24 | +if (condition) { |
| 25 | + // true block |
| 26 | +} else { |
| 27 | + // false block |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +Usage: Provides an alternate block of code if the condition is false. |
| 32 | + |
| 33 | + |
| 34 | +3. if-else-if Ladder |
| 35 | +Syntax: |
| 36 | + |
| 37 | + java |
| 38 | +CopyEdit |
| 39 | +if (condition1) { |
| 40 | + // block 1 |
| 41 | +} else if (condition2) { |
| 42 | + // block 2 |
| 43 | +} else { |
| 44 | + // default block |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +Usage: Checks multiple conditions sequentially. |
| 49 | + |
| 50 | + |
| 51 | +4. Nested if |
| 52 | +Usage: if statement inside another if block. |
| 53 | + |
| 54 | + |
| 55 | +Allows more complex decision structures. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Switch Statement |
| 60 | +Syntax: |
| 61 | + |
| 62 | + java |
| 63 | +CopyEdit |
| 64 | +switch (expression) { |
| 65 | + case value1: |
| 66 | + // code block |
| 67 | + break; |
| 68 | + case value2: |
| 69 | + // code block |
| 70 | + break; |
| 71 | + default: |
| 72 | + // default block |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +Usage: Best used when evaluating one variable against many possible constant values. |
| 77 | + |
| 78 | + |
| 79 | +Limitations: Works only with byte, short, char, int, enum, and String (Java 7+). |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +Comparison and Logical Operators |
| 84 | +Used in condition checks: |
| 85 | +Comparison Operators: ==, !=, <, >, <=, >= |
| 86 | + |
| 87 | + |
| 88 | +Logical Operators: && (AND), || (OR), ! (NOT) |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +Practical Examples |
| 93 | +Age Validation: Check if a user is eligible to vote. |
| 94 | + |
| 95 | + |
| 96 | +Grade System: Use of if-else-if ladder to assign grades. |
| 97 | + |
| 98 | + |
| 99 | +Menu Selection: Switch case to select options like login, register, etc. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +Key Points |
| 104 | +Conditional statements allow branching logic in programs. |
| 105 | + |
| 106 | + |
| 107 | +Use if when there's only one condition. |
| 108 | + |
| 109 | + |
| 110 | +Use switch for cleaner code when checking a single variable against multiple constants. |
| 111 | + |
| 112 | + |
| 113 | +Always test boundary conditions. |
| 114 | + |
0 commit comments