Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Java Control Structures: Switch Statements, While Loops, and Variable Scope

Tech 2

Switch Statements

Purpose

Primarily used for equality comparisons against multiple values.

Syntax

switch(variable) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // default code block
        break;
}

Key notes:

  • Default block is optional
  • Break statemants exit the switch block
  • Supported variable types: integers, characters, strings (JDK 1.6+)
  • Multiple cases can be combined

Example:

int rank = input.nextInt();
switch(rank) {
    case 1:
        System.out.println("Trip");
        break;
    case 2:
        System.out.println("Laptop");
        break;
    case 3:
        System.out.println("Hard drive");
        break;
    default:
        System.out.println("Nothing");
        break;
}

While Loops

Purpose

Execute code blocks repeatedly while a condition remains true.

Basic Structure

while(condition) {
    // loop body
}

Fixed-count example:

int counter = 1;
while(counter <= 5) {
    System.out.println("Iteration: " + counter);
    counter++;
}

Summing numbers 1-100:

int total = 0;
int num = 1;
while(num <= 100) {
    total += num;
    num++;
}
System.out.println("Sum: " + total);

User-controlled loop:

String response = "y";
while(response.equals("y")) {
    System.out.println("Running lap...");
    response = input.next();
}

Variable Scope

  • Local variables: Dcelared within braces, only accessible within those braces
  • Global variables: Declared outside braces, accessible throughout the containing scope

Practice Exercises

  1. Print numbers from 100 to 5 in decrements of 5:
int value = 100;
while(value >= 5) {
    if(value % 5 == 0) {
        System.out.print(value + " ");
    }
    value--;
}
  1. Sum numbers divisible by 7 between 1-50:
int sum = 0;
int current = 1;
while(current <= 50) {
    if(current % 7 == 0) {
        sum += current;
    }
    current++;
}
System.out.println("Sum: " + sum);
  1. Count numbers divisible by both 3 and 5 between 1-100:
int count = 0;
int number = 1;
while(number <= 100) {
    if(number % 3 == 0 && number % 5 == 0) {
        count++;
    }
    number++;
}
System.out.println("Count: " + count);
  1. Weekly schedule using switch:
switch(day) {
    case 1: case 3: case 5:
        System.out.println("Programming");
        break;
    case 2: case 4: case 6:
        System.out.println("English");
        break;
    case 7:
        System.out.println("Rest day");
        break;
}
  1. Shopping cart calculator:
int price, total = 0, items = 0;
price = input.nextInt();
while(price != 0) {
    total += price;
    items++;
    price = input.nextInt();
}
System.out.println(items + " items, total: " + total);

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.