L6: Decision-making Statements in Java
Overview
- Decision-making statements allow a program to follow different paths based on specific conditions, enhancing the logic and functionality of your code.
- Before decision-making, code execution was sequential. Now, you can introduce multiple execution paths.
The if Statement
The
ifstatement evaluates a boolean condition:if (condition) { // Statement(s) to execute if condition is true }If the condition is
true, the statements inside the braces execute. Iffalse, they are skipped.Relational operators (
<,>,<=,>=) and equality operators (==,!=) are commonly used withinifconditions.Example:
int age = 18; if (age >= 18) { System.out.println("You are an adult."); }
The if-else Statement
Extends the
ifstatement to provide an alternative path if the condition isfalse:if (condition) { // Statement(s) if condition is true } else { // Statement(s) if condition is false }Only one of the blocks (
iforelse) will execute.Example:
int score = 75; if (score >= 50) { System.out.println("Pass"); } else { System.out.println("Fail"); }
Multi-way Branching with else if
Allows for multiple conditions to be checked sequentially:
if (condition1) { // Statement(s) if condition1 is true } else if (condition2) { // Statement(s) if condition2 is true } else { // Statement(s) if none of the above conditions are true }Conditions are evaluated one by one until a
truecondition is found, at which point the corresponding block executes.Example:
int number = 0; if (number > 0) { System.out.println("Positive"); } else if (number < 0) { System.out.println("Negative"); } else { System.out.println("Zero"); }
The switch Statement
In Java, you can’t directly use complex expressions in a traditional switch-case statement. The case labels must be constant expressions.
Used for multi-way branching based on the value of an expression:
switch (expression) { case value1: // Statement(s) break; case value2: // Statement(s) break; default: // Statement(s) }Each
casematches a potential value of the expression. Thedefaultblock runs if no cases match.The
breakstatement prevents “fall-through” to subsequent cases. Without it, execution continues through the remaining cases.Example:
char grade = 'B'; switch (grade) { case 'A': System.out.println("Excellent"); break; case 'B': System.out.println("Good"); break; default: System.out.println("Invalid grade"); }
The Conditional (Ternary) Operator
A compact way to perform simple
if-elseoperations:variable = (condition) ? expression1 : expression2;If the condition is
true,expression1is evaluated; otherwise,expression2is evaluated.Example:
int age = 20; String result = (age >= 18) ? "Adult" : "Minor";
Logical Operators
AND (
&&): True if both operands are true.OR (
||): True if at least one operand is true.NOT (
!): Reverses the truth value.Used to create complex conditions:
if (age > 18 && age < 65) { System.out.println("Working-age adult"); }
Short-circuit Evaluation
- For
&&: If the first operand isfalse, the second operand is not evaluated since the overall expression is alreadyfalse. - For
||: If the first operand istrue, the second operand is not evaluated since the overall expression is alreadytrue. - Useful for optimizing code and avoiding unnecessary computation.
Comparing Strings
equals()Method:Compares the content of two strings, not their memory references.
Example:
String str1 = "hello"; String str2 = new String("hello"); if (str1.equals(str2)) { System.out.println("Strings are equal."); }
String Interning and String Constant Pool:
- Literal strings are stored in a common pool to optimize memory usage.
- Directly assigning strings (e.g.,
String str = "text";) uses the pool, whereas usingnew(e.g.,new String("text");) creates a separate object in the heap.
compareTo()Method:Compares two strings lexicographically (dictionary order) based on Unicode values.
Returns:
0if strings are equal.- A negative value if the calling string is less than the argument.
- A positive value if the calling string is greater than the argument.
Example:
String a = "apple"; String b = "banana"; int result = a.compareTo(b); // Returns a negative number since "apple" < "banana"
Tracing if and switch Statements
- Tracing: Refers to following the flow of execution in code to understand which statements are executed based on various conditions.
- For
ifstatements, trace the conditions in the order they appear. - For
switchstatements, trace thecasevalues matched and follow the execution until abreakor the end of the statement.
Additional Concepts
- Nesting: Placing one
iforif-elsestatement inside another. Use careful indentation to make the code readable. - Dangling
elseProblem: In nestedifstatements,elsepairs with the nearest unmatchedif. Use braces{}to clarify associations.