EXPLANATION OF CONDITIONAL STATEMENTS Of (IF, ELSE, SWITCH) IN EASY WAY

 Conditional Statements (If, Else, Switch) 

Introduction

 Conditional statements are fundamental to programming. They allow programs to make decisions and execute different blocks of code based on specific conditions. By using these statements, you can direct the flow of the program depending on various logical outcomes. In this lesson, we will cover three key conditional constructs: if, else, and switch.


1. If Statement

The if statement is used to execute a block of code if a specified condition evaluates to true. If the condition is false, the block of code will be skipped.


Syntax:

python 

Copy code 


if (condition) {

    // block of code to be executed if the condition is true

}


Example in Python:

python

Copy code


x = 10

if x > 5:

    print("x is greater than 5")

    

Explanation:

The condition x > 5 is evaluated. If it is True, the code inside the if block will execute.

In this case, since x is 10, the output will be:

      x is greater than 5


2. Else Statement

The else statement follows an if statement and executes a block of code if the if condition is False.


Syntax:

python

Copy code


if (condition) {

    // block of code if the condition is true

} else {

    // block of code if the condition is false

}


Example in Python:

python

Copy code


x = 3

if x > 5:

    print("x is greater than 5")

else:

    print("x is not greater than 5")

    

Explanation:

Here, the condition x > 5 is evaluated.

Since x is 3, the condition is False, so the else block is executed, resulting in the output:

x is not greater than 5


3. Else If (Elif in Python)

In many scenarios, you need to check multiple conditions. The else if (or elif in Python) statement allows for that. This checks another condition if the previous if was False.


Syntax:

python

Copy code


if (condition1) {

    // block of code to be executed if condition1 is true

else if (condition2) {

    // block of code to be executed if condition1 is false and condition2 is true

else {

    // block of code to be executed if both condition1 and condition2 are false

}


Example in Python:

python

Copy code


      x = 7

 if x > 10:

    print("x is greater than 10")

    elif x > 5:

    print("x is greater than 5 but less than or equal to 10")

    else:

    print("x is less than or equal to 5")

    

Explanation:

First, x > 10 is checked. Since it's False, it moves to the elif condition.

The condition x > 5 is True, so the corresponding block of code runs:


x is greater than 5 but less than or equal to 10


4. Nested If Statements

You can place if statements inside other if or else statements to create more complex decision structures.


Example in Python:

python

Copy code


x = 15

if x > 10:

    if x < 20:

        print("x is between 10 and 20")

    else:

        print("x is greater than or equal to 20")

else:

    print("x is less than or equal to 10")

    

Explanation:

The first condition x > 10 is True, so the inner if statement is evaluated.

Since x < 20 is also True, the inner block prints:

x is between 10 and 20


5. Switch Statement

In some programming languages, a switch statement is used to replace multiple if-else chains. The switch evaluates the expression and executes the corresponding case.

Syntax (in C-like languages such as Java, C++, etc.):

java

Copy code 


switch (expression) {

    case value1:

        // code to be executed if expression == value1

        break;

    case value2:

        // code to be executed if expression == value2

        break;

    default:

        // code to be executed if expression doesn't match any cases

}


Example in C++:

Copy code


int day = 2;

switch (day) {

    case 1:

        cout << "Monday";

        break;

    case 2:

        cout << "Tuesday";

        break;

    case 3:

        cout << "Wednesday";

        break;

    default:

        cout << "Invalid day";

}. 


Explanation:

In this example, since day is 2, the case 2 block will execute, printing:

Tuesday

The break statement prevents the code from falling through to the next case.


Note:

In Python, there is no built-in switch statement, but you can use a dictionary to achieve similar behavior using function mappings or conditions.

Switch Equivalent in Python:

python

Copy code


def day_name(day):

    switcher = {

        1: "Monday",

        2: "Tuesday",

        3: "Wednesday"

    }

    return switcher.get(day, "Invalid day")

day = 2

print(day_name(day))


Explanation:

A dictionary switcher holds the day names. The get() method is used to return a value based on the key (day), or "Invalid day" if the key is not found.

Output:

Tuesday


6. Key Points

If Statement: Executes code if a condition is True.

Else Statement: Executes code if the condition in the if is False.

Elif Statement (Else If): Used to check multiple conditions.


Nested If: Allows placing if statements inside other if or else statements for complex decision-making.


Switch Statement: Replaces multiple if-else statements in some languages, evaluating a single expression against multiple cases (not available in Python but can be simulated using dictionaries).


7. Best Practices

Avoid Deep Nesting: Too many nested if statements can make code harder to read. Instead, consider using elif or switch when possible.


Use Else Sparingly: Only use the else statement if you need a default behavior when all conditions fail.


Case Grouping in Switch: If two or more cases should execute the same code, group them together without a break statement between them.


Conclusion

Conditional statements are essential for decision-making in programming. By mastering if, else, elif, and switch, you can handle complex logic in your programs efficiently. Keep practicing different scenarios to solidify your understanding!


Exercises

Write a program that checks whether a number is positive, negative, or zero using if, elif, and else.

Create a simple calculator using the switch-like behavior in Python for operations like addition, subtraction, multiplication, and division.


Comments

Popular Posts