home
0%
Progress
CBSE Code 802 | Unit 3: Java Programming

03. Conditionals & Looping Structures

if-else-if ladder, switch-case, for, while, do-while loops, break vs continue.

Unit 3: Java Programming

Chapter 3: Control Structures - Conditionals and Loops

1. Flow of Control

By default, statements in a Java program execute sequentially from top to bottom. Control structures alter this sequential flow: 1. Selection (Decision Making): if, if-else, nested if, switch. 2. Iteration (Looping): for, while, do-while. 3. Jump Statements: break, continue, return.


2. Decision Making Statements

1. if-else and if-else-if Ladder

int marks = 85;

if (marks >= 90) {
    System.out.println("Grade: A+");
} else if (marks >= 80) {
    System.out.println("Grade: A");
} else if (marks >= 70) {
    System.out.println("Grade: B");
} else {
    System.out.println("Grade: C");
}

2. The switch Statement

Selects one of many code blocks to be executed based on matching an expression value. - Expression can be: byte, short, int, char, String, or enum. - break prevents "fall-through" into subsequent cases. - default executes if no case matches.

int day = 3;
String dayName;

switch (day) {
    case 1: dayName = "Monday"; break;
    case 2: dayName = "Tuesday"; break;
    case 3: dayName = "Wednesday"; break;
    case 4: dayName = "Thursday"; break;
    case 5: dayName = "Friday"; break;
    case 6: dayName = "Saturday"; break;
    case 7: dayName = "Sunday"; break;
    default: dayName = "Invalid Day Number"; break;
}
System.out.println(dayName);

3. Iteration / Looping Statements

1. The for Loop (Definite Loop)

Ideal when the number of iterations is known beforehand.

// Syntax: for (initialization; condition; update) { ... }
for (int i = 1; i <= 5; i++) {
    System.out.print(i + " ");
}
// Output: 1 2 3 4 5

2. The while Loop (Entry-Controlled Loop)

Condition is tested before entering the loop body. If condition is false initially, the loop body never executes.

int count = 1;
while (count <= 5) {
    System.out.print(count + " ");
    count++;
}

3. The do-while Loop (Exit-Controlled Loop)

Condition is evaluated after the loop body executes. - Key Guarantee: Guaranteed to execute at least once, even if the condition is false from the start.

int num = 10;
do {
    System.out.println("Executed at least once!");
} while (num < 5);

Loop Comparison Table

Feature for Loop while Loop do-while Loop
Control Point Entry-controlled Entry-controlled Exit-controlled
Min. Iterations 0 0 1
Ideal Scenario Known number of iterations Indefinite condition checking Interactive menu-driven programs

4. Jump Statements: break vs. continue

Feature break Statement continue Statement
Effect Terminates loop entirely and jumps out Skips current iteration and moves to next
Used In Loops and switch statements Only inside loops
// Example demonstrating break and continue
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue; // skips 3
    if (i == 5) break;    // terminates before printing 5
    System.out.print(i + " ");
}
// Output: 1 2 4

5. Board Exam Output Questions

  1. What will be the output of the following Java snippet? java int x = 2; while (x < 10) { x += 3; } System.out.println(x);
  2. Trace:
    • Start: $x = 2$ ($2 < 10 o x = 5$)
    • Iteration 2: $x = 5$ ($5 < 10 o x = 8$)
    • Iteration 3: $x = 8$ ($8 < 10 o x = 11$)
    • Iteration 4: $11 < 10$ is False $ o$ Loop terminates.
  3. Output: 11

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.