cs 5
Chapter 5: Conditionals and Loops
Overview of Conditionals and Loops
The chapter examines programming statements that allow:
Decision making based on conditions
Repetition of processing steps in loops
Key Concepts Covered
Boolean expressions
If and if-else statements
Data comparison
While loops
Iterators
ArrayList class
GUI controls
Flow of Control
Statement execution is typically linear unless specified otherwise.
Control structures allow for decision-making and repetition based on boolean expressions.
Flow of control refers to the order of statement execution in a program.
Conditional Statements
Conditional statements allow selection of which statement to execute next
Examples:
if statement
if-else statement
switch statement (explored in Chapter 6)
These statements enable basic decision-making in Java.
Boolean Expressions
Conditions typically use Java's comparison operators, returning boolean results:
==
(equal to)!=
(not equal to)<
(less than)>
(greater than)<=
(less than or equal to)>=
(greater than or equal to)
Important to distinguish between
==
and=
(assignment operator).
Example of an If Statement
Syntax:
if (condition) statement;
Execution Flow:
Condition is evaluated
If true, execute statement; if false, skip.
Logical Operators
Operators for combining boolean expressions:
!
(Logical NOT)&&
(Logical AND)||
(Logical OR)
Each takes boolean operands and returns boolean results.
Logical NOT
Negates the truth of a condition.
If
a
is true, then!a
is false, and vice versa.
Logical AND and OR
AND (
&&
) is true if both operands are true.OR (
||
) is true if at least one operand is true.
Short-Circuited Operators
These operators short-circuit evaluations:
If the left operand is sufficient to determine the result, the right operand is not evaluated.
Example:
if (count != 0 && total/count > MAX)
Be cautious with short-circuiting, as it may bypass necessary evaluations.
The If Statement
Syntax:
if (condition) statement;
Indentation is crucial for readability and indicates control structure relationships.
Reminder: The compiler ignores indentation, leading to potential logical errors.
Example Logic
Condition evaluated:
True: Execute statement.
False: Skip.
The If-Else Statement
Adds an else clause to the if statement:
if (condition) statement1; else statement2;
Only one of the statements is executed based on the condition's truth value.
Example Wages.java
Demonstrates the use of an if-else statement that calculates wages based on hours worked and pays overtime.
Nested If Statements
An if clause can contain another if statement (nested if).
The last unmatched
if
determines whichelse
applies.
Example:
if (num1 < num2)
if (num1 < num3)
min = num1;
else
min = num3;
else if (num2 < num3)
min = num2;
else
min = num3;
Comparing Data
Important nuances in data types when using boolean expressions.
Comparing floating point values for equality requires care to handle precision issues.
Special methods and techniques (e.g.,
Math.abs()
) can determine equality for floating-point comparisons.Character and String comparisons rely on Unicode values and appropriate methods (e.g.,
.equals()
).
Example with Floats
To compare floats:
if (Math.abs(f1 - f2) < TOLERANCE)
// Considered equal
Loops
Loops are used for repetition in Java.
Types of loops include: while, do, and for (discussed in Chapter 6).
The While Statement
Syntax:
while (condition) statement;
Loop runs while the condition is true; evaluates repeatedly until false.
Example of While Loop
Example demonstrating while loops:
int count = 1;
while (count <= 5)
{
System.out.println(count);
count++;
}
Sentinel Values
Sentinel values signify the end of input for loops.
Example with sentinel value to compute average input.
Input Validation
Loops are utilized to ensure valid input, enhancing program robustness.
Infinite Loops
Fail to change the loop's condition, leading to endless execution.
Example demonstrating bad logic in loops.
Nested Loops
Similar to nested if statements, loops can be nested.
Iterators
Iterators help process collections item by item through methods like
.hasNext()
and.next()
.
The ArrayList Class
Stores lists of objects; capacity adjusts as needed.
Methods include
add()
,remove()
,get()
, andsize()
.
GUI Controls
Check boxes, radio buttons, and event handlers are essential for user interaction.
Event Handling
Techniques to manage user actions and update the UI accordingly.
Summary
Chapter 5 emphasizes understanding boolean expressions, conditional statements, data comparison, loops, iterators, the ArrayList class, and GUI controls.