Unit_3_Selection_Statements_Gaddis
CS102 - Java Programming Language
Conditional (Selection Statement)
- Unit 3 Overview
- Instructor: Charles Edeki
Objectives of Unit 3
- Main topics covered:
- The if Statement
- The if-else Statement
- Nested if Statements
- The if-else-if Statement
- Logical Operators
- Comparing String Objects
- The Conditional Operator
- The switch Statement
The if Statement
- Definition:
- Determines whether a section of code executes based on a boolean condition.
- Structure:
if (boolean expression is true) execute next statement.
Flowcharts
Visual Representation of if Statements
- Flowchart for a basic if statement:
- Example: "Is it cold outside?"
- Decision: Yes → wearCoat();
Block if Statement Flowchart
- Example:
- Decision: Is it cold outside?
- Actions: wearCoat(); wearHat(); wearGloves();
- Note: Curly braces
{}
block multiple statements.
Relational Operators
- Definition:
- Used in boolean expressions for comparisons.
- Operators and Meanings:
>
: greater than<
: less than>=
: greater than or equal to<=
: less than or equal to==
: equal to!=
: not equal to
Boolean Expressions
- Definition:
- Results in true or false conditions.
- Examples:
x > y
: Is x greater than y?x < y
: Is x less than y?
if Statements and Examples
- Example code:
if (x > y) System.out.println("X is greater than Y");
- More complex usage:
if(x != y) { ... }
Programming Style in if Statements
- Tips:
- Multi-line if statements are still a single statement.
- Proper structure helps readability.
- Use indentation and curly braces for clarity.
- Example:
- Prefer:
if (average > 95) grade = 'A';
Block if Statements
- Definition:
- Group statements using
{}
.
- Example:
if (expression) { statement1; statement2; }
- Without braces, only the next statement after the condition will execute.
Flags
- Definition:
- Boolean variable that monitors a condition.
- Example usage of a flag:
if (average > 95) highScore = true;
Comparing Characters
- Key Points:
- Characters stored using Unicode.
- Can be compared using relational operators.
- Example:
if(c < 'Z') System.out.println("A is less than Z");
if-else Statements
- Structure:
if (expression) statementOrBlockIfTrue; else statementOrBlockIfFalse;
Nested if Statements
- Definition:
- An if statement within another.
- Flowchart representation.
- Example code structure showing nested conditions.
if-else Matching
- Reminder:
- Curly braces make code clearer, especially with matching else statements.
if-else-if Statements
- Structure:
if (expression_1) { ... } else if (expression_2) { ... } else { ... }
- Simplifies nested logic.
Logical Operators
Types of Logical Operators
- Logical AND (
&&
) - Both conditions must be true. - Logical OR (
||
) - At least one condition must be true. - Logical NOT (
!
) - Reverses the truth value.
The && Operator
- Operands must be boolean expressions.
- Example truth table for
&&
.
The || Operator
- Operands must be boolean expressions.
- Example truth table for
||
.
The ! Operator
- Example usage of NOT with conditional statements.
Short Circuiting
- Behavior of logical operators in evaluation.
- AND stops evaluating when one operand is false.
- OR stops evaluating when one operand is true.
Order of Precedence
- Operators precedence hierarchy explained with examples.
Comparing String Objects
- Relational operators cannot be used directly.
Ignoring Case in String Comparisons
- Methods:
equalsIgnoreCase
and compareToIgnoreCase
.
Variable Scope
- Definition of local variable scope.
- Importance of when a variable comes into scope.
The Conditional Operator
Overview
- Ternary operator to simplify if-else statements.
- Syntax and functionality.
The switch Statement
Structure
- Use of ordinal values to branch logic.
The case Statement
- Explanation of break statement and its behavior.
The System.out.printf Method
- Overview and formatted output.
- Examples using format specifiers like
%d
, %f
, and %s
.
- Similar to
System.out.printf
, but returns formatted strings.