Java Control Statements Lecture Notes

Jul 25, 2024

Java Control Statements Lecture Notes

Overview of Control Statements

  • Control statements allow for conditional execution of code based on specific conditions.
  • Regular Java program execution occurs top to bottom, but control statements change execution flow.

Types of Control Statements

  1. Conditional Statements: Execute statements based on conditions.
  2. Looping Statements: Repeat the execution of a block of code multiple times.
  3. Jumping Statements: Control the flow of execution in various ways (e.g. break, continue).

Conditional Statements

Purpose

  • To execute specific statements based on certain conditions.

Types of Conditional Statements in Java

  1. If Statement: Simple conditional execution.
  2. If-Else Statement: Conditional execution with an alternative path.
  3. Nested If-Else Statement: If-Else conditions nested within each other.
  4. Switch Statement: An alternative to multiple if-else for handling multiple conditions based on a variable.

If Statement Syntax

  • Syntax:
    if (condition) {
        // statements to execute if condition is true
    }
    

If-Else Statement Syntax

  • Syntax:
    if (condition) {
        // if true
    } else {
        // if false
    }
    
  • If-Else allows for an alternative execution path if the condition is false.

Example of If Statement

  • Checking if a person is eligible for voting:
    int personAge = 25;
    if (personAge >= 18) {
        System.out.println("Eligible for voting");
    }
    
  • If age is less than 18, no output will occur.

Example of If-Else Statement

  • Check if a number is even or odd:
    if (number % 2 == 0) {
        System.out.println("Even number");
    } else {
        System.out.println("Odd number");
    }
    

Nested If-Else Example

  • Checking if a number is positive, negative or zero:
    if (number > 0) {
        System.out.println("Positive");
    } else if (number < 0) {
        System.out.println("Negative");
    } else {
        System.out.println("Zero");
    }
    

Switch Statement

Syntax

  • Syntax:
    switch (variable) {
        case value1:
            // statements
            break;
        case value2:
            // statements
            break;
        default:
            // statements
    }
    

Purpose

  • Use when you have multiple conditions to check against a single variable.
  • Reduces the lines of code compared to multiple if-else statements.

Example of Switch Statement

  • Displaying day names based on the week number:
    switch (weekNumber) {
        case 1:
            System.out.println("Sunday");
            break;
        case 2:
            System.out.println("Monday");
            break;
        // Additional cases...
        default:
            System.out.println("Invalid week number");
    }
    

Break Statement

  • Used to exit the switch case block after executing a matching case.
  • Recommended to prevent falling through to subsequent cases.

Summary

  • Control statements allow us to modify the execution flow based on conditions.
  • Java provides various types of control statements (conditional, looping, jumping) to handle different scenarios effectively.

Assignments

  1. Find the largest of two numbers using both if-else and ternary operator.
  2. Find the smallest of three numbers using if-else conditions.
  3. Write a program to take the name of the week and return its corresponding number using a switch case.