Java Conditionals and Loops Lecture Notes

Jul 18, 2024

Java Conditionals and Loops Lecture Notes

Introduction

  • Topics Covered: Conditionals, Loops in Java
  • Prerequisites: Input-output, basic processing (e.g., a = a + 1)

Setting Up the Project

  • Setup project in IntelliJ IDEA
  • Project: Lecture 6 - Conditions and Loops

Conditionals

If Statement

  • Syntax: if (boolean expression) { body } else { body }`
    • Example:
      int salary = 25400;
      if (salary > 10000) {
          salary += 2000;
      } else {
          salary += 1000;
      }
      System.out.println(salary);
      
  • Comments: Multi-line comments: /* comment */

Else If Statement

  • Use else if to combine multiple conditions
  • Example:
    if (salary > 20000) {
        salary += 3000;
    } else if (salary > 10000) {
        salary += 2000;
    } else {
        salary += 1000;
    }
    ``
    

Loops

Why Use Loops?

  • Tasks that repeat multiple times (e.g., print numbers, iterate over data)
  • Types of Loops: For loop, While loop, Do-While loop

For Loop

  • Syntax: for (initialization; condition; increment/decrement) { body }
    • Example: Printing 1 to 5
      for (int num = 1; num <= 5; num++) {
          System.out.println(num);
      }
      
    • Debugging shows step-by-step execution
  • Variable Scope: Initialize, check condition, execute body, increment

While Loop

  • Syntax: while (condition) { body }
    • Example: Printing 1 to 5
      int num = 1;
      while (num <= 5) {
          System.out.println(num);
          num++;
      }
      
    • Use when number of iterations is unknown
    • Debugging: Shows how condition is repeatedly checked

Do-While Loop

  • Syntax: do { body } while (condition);
    • Example:
      int num = 1;
      do {
          System.out.println(num);
          num++;
      } while (num <= 5);
      
    • Executes at least once, condition checked after body execution

Conditional and Loop Examples

Example 1: Find Largest of Three Numbers

  • Method: Use nested if-else
    int a = 10, b = 20, c = 30;
    int max = a;
    if (b > max) max = b;
    if (c > max) max = c;
    System.out.println(max);
    

Example 2: Check Uppercase or Lowercase

  • Method: Compare ASCII values
    char ch = 'A';
    if (ch >= 'a' && ch <= 'z') {
        System.out.println("Lowercase");
    } else {
        System.out.println("Uppercase");
    }
    

Example 3: Fibonacci Sequence

  • Method: Use While loop
    int a = 0, b = 1, n = 7;
    while (count <= n) {
        int next = a + b;
        a = b;
        b = next;
        count++;
    }
    System.out.println(b);
    

Example 4: Count Specific Digit in Number

  • Method: Use remainder operation
    int num = 138957;
    int count = 0;
    while (num > 0) {
        int remainder = num % 10;
        if (remainder == 7) count++;
        num /= 10;
    }
    System.out.println(count);
    

Example 5: Reverse a Number

  • Method: Use remainder operation and multiplication
    int num = 23597;
    int result = 0;
    while (num > 0) {
        int remainder = num % 10;
        result = result * 10 + remainder;
        num /= 10;
    }
    System.out.println(result);
    

Example 6: Basic Calculator Program

  • Method: Continuously take input until 'x' is entered
    while (true) {
        char op = in.next().charAt(0);
        if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
            int num1 = in.nextInt();
            int num2 = in.nextInt();
            int result;
            if (op == '+') result = num1 + num2;
            else if (op == '-') result = num1 - num2;
            else if (op == '*') result = num1 * num2;
            else if (op == '/') result = (num2 != 0) ? num1 / num2 : 0;
            else result = num1 % num2;
            System.out.println(result);
        } else if (op == 'x' || op == 'X') {
            break;
        } else {
            System.out.println("Invalid operation");
        }
    }