All Labs

Selection & Iteration Lab

Trace through loops and boolean expressions step by step. Watch how for, while, do-while, and nested loops execute iteration by iteration, explore short-circuit evaluation in boolean logic, and build common code patterns like accumulators, counters, and FizzBuzz.

Guided Experiment: Boolean Logic and Short-Circuit Evaluation

When does Java skip evaluating part of a boolean expression? What determines whether the right side of && or || is evaluated?

Write your hypothesis in the Lab Report panel, then click Next.

Evaluation Flow

a
true
b
false
true && false
false
c
true
false || true
true
Result: true

Controls

Operators: && (AND), || (OR), ! (NOT), parentheses for grouping

Variables
a=
b=
c=

Evaluation Steps

1Variable: a = true
true
2Variable: b = false
false
3Evaluate: true && false → false
false
4Variable: c = true
true
5Evaluate: false || true → true
true
Final Result
true

Data Table

(0 rows)
#TrialStructureCode PatternIterationsFinal VariablesOutput
0 / 500
0 / 500
0 / 500

Reference Guide

Boolean Operators and Short-Circuit Evaluation

Java evaluates boolean expressions left to right with short-circuit behavior. If the left side of && is false, the right side is never evaluated. If the left side of || is true, the right side is skipped.

// Short-circuit AND: b is never called
if (a == null && a.length() > 0)  // safe!

// Short-circuit OR: second check skipped
if (found || checkNext())

// Precedence: && binds tighter than ||
// a || b && c  means  a || (b && c)

This matters when the right operand has side effects like method calls or assignments.

For and While Loops

A for loop is best when the number of iterations is known. A while loop works when the stopping condition depends on runtime data. A do-while always executes the body at least once.

// for: known count
for (int i = 0; i < 10; i++) {
    sum += i;
}

// while: unknown stopping point
while (scanner.hasNext()) {
    process(scanner.next());
}

// do-while: at least one execution
do {
    input = getInput();
} while (input != sentinel);

Nested Loops and Patterns

Nested loops multiply their iteration counts. A 5-row by 5-column nested loop executes 25 times total. The inner loop completes all its iterations for each single iteration of the outer loop.

// Multiplication table
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 5; j++) {
        System.out.printf("%4d", i * j);
    }
    System.out.println();
}

// Enhanced for (for-each)
for (String name : names) {
    System.out.println(name);
}

Common Loop Patterns

Most loop bodies follow one of a few patterns. Recognizing these patterns makes code easier to write and debug.

// Accumulator: build a running total
int sum = 0;
for (int x : arr) sum += x;

// Counter: tally items matching a condition
int count = 0;
for (int x : arr) if (x > 0) count++;

// Flag: detect if a condition was ever true
boolean found = false;
for (int x : arr) if (x == target) found = true;

// Early exit: stop at first match
for (int x : arr) {
    if (x < 0) { first = x; break; }
}