Skip to content

Week 2: Java Basics – Conditional Statements and Loops


Q1. Greeting Based on Time

Objective: Accept the user’s name and current hour, and print an appropriate greeting.

import java.util.Scanner;

public class q1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.print("Enter current hour (0-23): ");
        int time = sc.nextInt();

        if (5 <= time && time <= 11) {
            System.out.println("Good morning " + name);
        } else if (12 <= time && time <= 17) {
            System.out.println("Good Afternoon " + name);
        } else if (18 <= time && time <= 21) {
            System.out.println("Good Evening " + name);
        } else {
            System.out.println("Good Night " + name);
        }

        sc.close();
    }
}

Key Points:

  • if-else if-else handles multiple conditions.
  • Time is assumed in 24-hour format.
  • Single Scanner can be used for multiple inputs instead of creating multiple Scanner objects.

Q2. Simple Calculator Using Switch

Objective: Accept two numbers and an operator, and calculate the result.

import java.util.Scanner;

public class q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int num1 = sc.nextInt();

        System.out.print("Enter second number: ");
        int num2 = sc.nextInt();

        System.out.print("Enter operator (+, -, *, /, %): ");
        String op = sc.next();

        switch(op) {
            case "+":
                System.out.println("Result: " + (num1 + num2));
                break;
            case "-":
                System.out.println("Result: " + (num1 - num2));
                break;
            case "*":
                System.out.println("Result: " + (num1 * num2));
                break;
            case "/":
                System.out.println("Result: " + (num1 / num2));
                break;
            case "%":
                System.out.println("Result: " + (num1 % num2));
                break;
            default:
                System.out.println("Invalid Input!");
        }

        sc.close();
    }
}

Key Points:

  • switch is perfect for discrete choices.
  • Using sc.next() for string input avoids creating extra Scanner objects.

Q3. Print Even or Odd Numbers in a Range

Objective: Accept a range and type (even or odd) and print numbers accordingly.

import java.util.Scanner;

public class q3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the start of range: ");
        int num1 = sc.nextInt();

        System.out.print("Enter the end of range: ");
        int num2 = sc.nextInt();

        sc.nextLine(); // Consume leftover newline
        System.out.print("Enter even/odd: ");
        String type = sc.nextLine();

        switch(type.toLowerCase()) {
            case "even":
                System.out.println("Even numbers from " + num1 + " to " + num2 + ":");
                for (int i = num1; i <= num2; i++) {
                    if (i % 2 == 0) System.out.println(i);
                }
                break;

            case "odd":
                System.out.println("Odd numbers from " + num1 + " to " + num2 + ":");
                for (int i = num1; i <= num2; i++) {
                    if (i % 2 != 0) System.out.println(i);
                }
                break;

            default:
                System.out.println("Invalid type! Enter 'even' or 'odd'.");
        }

        sc.close();
    }
}

Key Points:

  • Use toLowerCase() to handle case-insensitive input.
  • Always consume the leftover newline after nextInt() before nextLine().

Week 2 Notes:

  1. Scanner is used for input; prefer a single object to avoid System.in conflicts.
  2. if-else if-else handles multiple ranges/conditions.
  3. switch works well for discrete options (operators or string choices).
  4. Loops (for) combined with conditionals can filter numbers (even/odd).

Comments