An “if” statement is a fundament control structure used for conditional execution in a program. It allows you to specify a condition, and if that condition evaluates to true, the code block or statement inside the “if” block is executed. If the condition is false, the code within the “if” block is skipped, and the program continues with the next set of instructions. This enables C++ programmers to create decision-making logic, making it possible to execute different code paths based on the evaluation of a given condition. The “if” statement can also be extended with “else if” and “else” clauses to handle multiple conditions and provide fallback options when the initial condition is not met.

 if-else statement

An if-else statement is a conditional statement that executes a block of code if a given condition is true. If the condition is false, an alternate block of code can be executed, specified by the “else” keyword.

if-else

Syntax: –

if (condition) {

    // Code block to be executed if the condition is true

} else {

    // Code block to be executed if the condition is false

}

How it’s work?

The program evaluates the condition within the parentheses of the if statement. If the condition is true, the code inside the if block executes. If the condition is false, the code inside the else block (if present) executes.

Example to check the given number in odd or even by using if-else statement: –


#include <iostream>

int main() {
    int number;

    // Prompt the user to enter a number
    std::cout << "Enter an integer: ";
    std::cin >> number;

    // Check if the number is even or odd using an if statement
    if (number % 2 == 0) {
        std::cout << number << " is even." << std::endl;
    } else {
        std::cout << number << " is odd." << std::endl;
    }

    return 0;
}
Output: -
Enter an integer: 5
5 is odd.

Write a program to check that given number is a multiple of 3 by using if-else statement?

#include <iostream>

using namespace std;

int main() {
    int number;

    cout << "Enter an integer: ";
    cin >> number;

    if (number % 3 == 0) {
        cout << number << " is a multiple of 3." << endl;
    } else {
        cout << number << " is not a multiple of 3." << endl;
    }

    return 0;
}
Output: -
Enter an integer: 33
33 is a multiple of 3.

else-if ladder

An “else-if” ladder is a series of conditional statements that are used to evaluate multiple conditions sequentially. It is an extension of the basic “if-else” construct and allows a program to choose from among several possible code blocks based on the outcome of multiple conditions. When the program encounters an “else-if” statement, it checks whether the condition associated with it is true. If the condition is true, the code block associated with that “else-if” statement is executed, and the program exits the ladder.

If the condition is false, the program continues to the next “else-if” statement, and the process repeats. The ladder ends with an optional “else” statement, which provides a fallback block of code to execute if none of the previous conditions are met. “else-if” ladders are useful for handling complex decision-making scenarios where multiple cases need to be considered and different actions taken based on various conditions.

else-if

The syntax of the “else if” statement is as follows:

if (condition1) {

    // Code block to be executed if condition1 is true

} else if (condition2) {

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

} else if (condition3) {

    // Code block to be executed if condition3 is true and condition2 is false

} else {

    // Code block to be executed if all conditions are false

}

Here’s how the “else if” statement works:

  1. The program first evaluates the initial “if” condition (condition1).
  2. If condition1 is true, the code inside the corresponding “if” block is executed, and the program skips the “else if” and “else” parts, proceeding to the next statement after the entire “if-else” construct.
  3. If condition1 is false, the program moves to the next “else if” condition (condition2) and evaluates it.
  4. If condition2 is true, the code inside the corresponding “else if” block is executed, and the program skips the “else” part, proceeding to the next statement after the entire “if-else” construct.
  5. If condition2 is false, the program moves to the next “else if” condition (condition3) and evaluates it.
  6. If condition3 is true, the code inside the corresponding “else if” block is executed, and the program skips the “else” part, proceeding to the next statement after the entire “if-else” construct.
  7. If condition3 is also false, the program executes the code inside the “else” block (if present), as it serves as the default option when all conditions are false.

The “else if” statement is particularly useful when you have multiple conditions to check, and each condition is exclusive of the others. It provides a clean and readable way to handle such situations without nesting multiple “if” statements, which can lead to code clutter and reduced readability.

Write a program that demonstrates the use of an “else-if” ladder to determine the grade based on a student’s score

#include <iostream>

int main() {
    int score;
    
    std::cout << "Enter the student's score: ";
    std::cin >> score;

    if (score >= 90) {
        std::cout << "Grade: A" << std::endl;
    } 
    else if (score >= 80) {
        std::cout << "Grade: B" << std::endl;
    }
    else if (score >= 70) {
        std::cout << "Grade: C" << std::endl;
    }
    else if (score >= 60) {
        std::cout << "Grade: D" << std::endl;
    }
    else {
        std::cout << "Grade: F" << std::endl;
    }

    return 0;
}
Output: -
Enter the student's score: 76
Grade: C

Write a program that calculates tax using an if-else if ladder based on income:


#include <iostream>

int main() {
    double income, tax = 0.0;

    // Input the income
    std::cout << "Enter your annual income: $";
    std::cin >> income;

    // Calculate tax based on income
    if (income <= 50000) {
        tax = income * 0.05;
    }
    else if (income <= 100000) {
        tax = 2500 + (income - 50000) * 0.10;
    }
    else if (income <= 200000) {
        tax = 7500 + (income - 100000) * 0.20;
    }
    else {
        tax = 27500 + (income - 200000) * 0.30;
    }

    // Display the calculated tax
    std::cout << "Your tax liability is: $" << tax << std::endl;

    return 0;
}
Output: -
Enter your annual income: $500000
Your tax liability is: $117500

Nested if-else

In C++, nested if-else statements allow you to create more complex decision-making structures within your program. These structures involve placing one if-else statement inside another. When the program encounters a nested if-else statement, it first evaluates the condition of the outer if statement. If the condition is true, it proceeds to the code block associated with the outer if statement.

Within this code block, you can have another if-else statement, creating a second level of decision-making. This inner if-else statement will only be evaluated if the outer if condition is met. By nesting if-else statements, you can build intricate decision trees, making your code capable of handling various scenarios and conditions, enabling more advanced and precise control over your program’s behavior’s.

The general syntax of a nested if-else statement looks like this:

if (condition1) {

    // Code block to be executed if condition1 is true

    if (condition2) {

        // Code block to be executed if condition2 is true

    } else {

        // Code block to be executed if condition2 is false

    }

} else {

    // Code block to be executed if condition1 is false

}

In a nested if-else statement, you have an outer if statement that contains another if statement (or an if-else statement) within its block. This allows you to check multiple conditions and execute different code blocks based on the combination of conditions being true or false. The inner if statement is executed only if the condition in the outer if statement is true.

Here’s a breakdown of the syntax:

  1. The outer if statement checks condition1, and if it’s true, it executes the code block within its braces.
  2. Inside the code block of the outer if, there is an inner if statement that checks condition2. If condition2 is true, it executes the code block within its braces. If condition2 is false, the else block of the inner if is executed.
  3. The else block of the outer if is executed when condition1 is false.

Nested if-else statements provide a way to make precise and intricate decisions. They enable you to manage various conditions and respond differently depending on how these conditions interact. Nonetheless, it’s vital to exercise caution when nesting too deeply, as excessive nesting can make your code harder to understand and upkeep. In certain situations, it might be more advisable to utilize alternative control structures, such as switch statements or separate functions, to tackle complex conditions in a more organized fashion.

Nested if-else

Write a program to Determine the Quadrant of a Point by using nested-if:

#include <iostream>

int main() {
    double x, y;

    // Prompt the user to enter the coordinates of a point
    std::cout << "Enter the x-coordinate: ";
    std::cin >> x;
    std::cout << "Enter the y-coordinate: ";
    std::cin >> y;

    // Check in which quadrant the point lies
    if (x > 0) {
        if (y > 0) {
            std::cout << "The point is in the first quadrant." << std::endl;
        } else if (y < 0) {
            std::cout << "The point is in the fourth quadrant." << std::endl;
        } else {
            std::cout << "The point is on the positive x-axis." << std::endl;
        }
    } else if (x < 0) {
        if (y > 0) {
            std::cout << "The point is in the second quadrant." << std::endl;
        } else if (y < 0) {
            std::cout << "The point is in the third quadrant." << std::endl;
        } else {
            std::cout << "The point is on the negative x-axis." << std::endl;
        }
    } else {
        if (y != 0) {
            std::cout << "The point is on the y-axis." << std::endl;
        } else {
            std::cout << "The point is at the origin." << std::endl;
        }
    }

    return 0;
}
Output: -
Enter the x-coordinate: 3
Enter the y-coordinate: -1
The point is in the fourth quadrant.

Write a program to Check Leap Year by using nested-if:

#include <iostream>

int main() {
    int year;

    // Prompt the user to enter a year
    std::cout << "Enter a year: ";
    std::cin >> year;

    // Check if the year is a leap year
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0) {
                std::cout << year << " is a leap year." << std::endl;
            } else {
                std::cout << year << " is not a leap year." << std::endl;
            }
        } else {
            std::cout << year << " is a leap year." << std::endl;
        }
    } else {
        std::cout << year << " is not a leap year." << std::endl;
    }

    return 0;
}
Output: -
Enter a year: 1992
1992 is a leap year.


Read more about Computers:
History of Computer: – https://xcoode.co.in/basic_computer/history-of-computer/
Basic Computer: – https://xcoode.co.in/basic_computer/basic-introduction-to-computer/
Want to learn Programming:
Start with HTML: – https://xcoode.co.in/html/introduction-to-html/
C Programming: – https://xcoode.co.in/c_programming/introduction-to-c-programming/

Follow Us on: –
Facebook Page: –https://www.facebook.com/groups/294849166389180
Quora: – https://xcoode404.quora.com/

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *