Conditional Statements and Loops

If-Else Statement

  • Used to check Boolean conditions (True or False).

  • Syntax:

    if (boolean expression){
        // Body
    } else {
        // Do this
    }
  • Example:

    public class IfElse {
        public static void main(String[] args) {
            int salary = 25400;
            if (salary > 10000) {
                salary = salary + 2000;
            } else {
                salary = salary + 1000;
            }
            System.out.println(salary);
        }
    }
  • Output: 27400

Multiple If-Else Statement

  • Executes one condition from multiple statements.

  • Syntax:

  • Example:

  • Output: 28400

Loops

  • For Loop:

    • Used when the number of iterations is known.

    • Syntax:

    • Example 1:

    • Output: 1, 2, 3, 4, 5

    • Example 2:

      • Input: 6

      • Output: 1 2 3 4 5 6

  • While Loop:

    • Used when the number of iterations is not known.

    • Syntax:

    • Example:

    • Output: 1, 2, 3, 4, 5

  • Do-While Loop:

    • Used when the loop needs to execute at least once.

    • Exit-controlled loop.

    • Syntax:

    • Example:

    • Output: 1, 2, 3, 4, 5

  • Comparison between While Loop and Do-While Loop:

While Loop
Do-While Loop

Used when the number of iterations is not fixed

Used when we want to execute the statement at least once

Entry-controlled loop

Exit-controlled loop

No semicolon required at the end of while (condition)

Semicolon is required at the end of while (condition)

Programs

🎯 Program: Largest of Three Numbers

  • Problem Statement: "Find largest among three numbers."

  • Approach 1:

  • Approach 2:

  • Approach 3 (Using Math.max):

  • Input: 3 6 5

  • Output: 6

🎯 Program: Alphabet Case Check

  • Problem Statement: "Take an input character from the keyboard and check whether it is an uppercase alphabet or lowercase alphabet."

  • Example:

  • Input: a

  • Output: Lowercase

  • Input: Z

  • Output: Uppercase

🎯 Program: Fibonacci Numbers

  • Problem Statement: "Find the nth Fibonacci number."

  • Example:

  • Input: 7

  • Output: 13

🎯 Program: Counting Occurrence

  • Problem Statement: "Input two numbers, find how many times the second number's digit is present in the first number."

  • Example:

  • Input: 45535 5

  • Output: 3

🎯 Program: Reverse a Number

  • Problem Statement: "Input a number from the keyboard and show the output as the reverse of that number."

  • Example:

  • Input: 458792

  • Output: 297854

🎯 Program: Calculator Program

  • Example of a simple calculator program.

  • Input and output are managed until the user presses 'X' or 'x'.

Conclusion

To wrap up, diving into Conditional Statements and Loops equips you with essential tools for effective programming. The covered topics, from basic If-Else statements to versatile loops, offer practical solutions for various scenarios. The hands-on examples, like finding the largest number or building a basic calculator, reinforce your skills and prepare you for real-world coding challenges. In a nutshell, this learning journey sets a strong foundation for making informed decisions and handling repetitive tasks in Java programming.

Was this helpful?