Lesson 24 of 60 – if-else Statement in C++
40%

if-else Statement in C++

The if-else statement is used when a program needs to choose between two possible blocks of code. If the condition is true, the if block executes. If the condition is false, the else block executes.

Note: Use if-else when you have two possible outcomes, such as Pass/Fail, Adult/Minor, Even/Odd, or Yes/No.

1. What is if-else?

The if-else statement allows a program to execute one block when a condition is true and another block when the condition is false.

if (condition) {
    // code when condition is true
}
else {
    // code when condition is false
}

2. Syntax of if-else

The basic syntax of an if-else statement is:

if (condition) {
    statement1;
}
else {
    statement2;
}

Only one of the two blocks executes.

3. Simple if-else Example

int age = 20;

if (age >= 18) {
    std::cout << "Adult";
}
else {
    std::cout << "Minor";
}

Since the age is 20, the condition is true and Adult is displayed.

4. How if-else Works

The program first checks the condition.

  1. If the condition is true, the if block executes.
  2. If the condition is false, the else block executes.
  3. Only one block is executed.
if (condition) {
    // True
}
else {
    // False
}

5. Checking Pass or Fail

int marks = 35;

if (marks >= 40) {
    std::cout << "Pass";
}
else {
    std::cout << "Fail";
}

Because 35 is less than 40, the output is Fail.

6. Checking Even or Odd

The modulus operator % can be used to check whether a number is even or odd.

int number = 7;

if (number % 2 == 0) {
    std::cout << "Even";
}
else {
    std::cout << "Odd";
}

7. Checking Positive or Negative

int number = -10;

if (number >= 0) {
    std::cout << "Positive";
}
else {
    std::cout << "Negative";
}

Since -10 is not greater than or equal to zero, the else block executes.

8. Taking User Input

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    if (age >= 18) {
        std::cout << "You can vote";
    }
    else {
        std::cout << "You cannot vote";
    }

    return 0;
}

9. Comparing Two Numbers

int a = 50;
int b = 30;

if (a > b) {
    std::cout << "A is greater";
}
else {
    std::cout << "B is greater or equal";
}

The condition compares the values of a and b.

10. Checking Equal Values

int a = 10;
int b = 10;

if (a == b) {
    std::cout << "Both are equal";
}
else {
    std::cout << "They are different";
}

11. Greater Than Condition

int marks = 80;

if (marks > 50) {
    std::cout << "Marks are greater than 50";
}
else {
    std::cout << "Marks are 50 or less";
}

12. Less Than Condition

int age = 15;

if (age < 18) {
    std::cout << "Minor";
}
else {
    std::cout << "Adult";
}

13. Greater Than or Equal To

int marks = 40;

if (marks >= 40) {
    std::cout << "Pass";
}
else {
    std::cout << "Fail";
}

The condition includes 40 because the operator is >=.

14. Less Than or Equal To

int age = 18;

if (age <= 18) {
    std::cout << "Age is 18 or below";
}
else {
    std::cout << "Age is above 18";
}

15. if-else with Boolean Values

bool loggedIn = true;

if (loggedIn) {
    std::cout << "Welcome";
}
else {
    std::cout << "Please login";
}

If the Boolean value is true, the if block executes. Otherwise, the else block executes.

16. if-else with Logical AND

The && operator can combine two conditions.

int age = 25;
bool hasID = true;

if (age >= 18 && hasID) {
    std::cout << "Entry allowed";
}
else {
    std::cout << "Entry denied";
}

Both conditions must be true for the if block to execute.

17. if-else with Logical OR

The || operator is used when at least one condition can be true.

int day = 6;

if (day == 6 || day == 7) {
    std::cout << "Weekend";
}
else {
    std::cout << "Working day";
}

18. Checking a Password

#include <iostream>
#include <string>

int main() {

    std::string password;

    std::cout << "Enter password: ";
    std::cin >> password;

    if (password == "1234") {
        std::cout << "Correct password";
    }
    else {
        std::cout << "Incorrect password";
    }

    return 0;
}

19. Checking a Character

char grade = 'A';

if (grade == 'A') {
    std::cout << "Excellent";
}
else {
    std::cout << "Other grade";
}

Characters are written inside single quotes.

20. Checking a String

#include <iostream>
#include <string>

int main() {

    std::string name = "Rahul";

    if (name == "Rahul") {
        std::cout << "Hello Rahul";
    }
    else {
        std::cout << "Unknown user";
    }

    return 0;
}

21. Multiple Statements in if Block

Both the if and else blocks can contain multiple statements.

int marks = 75;

if (marks >= 40) {

    std::cout << "Result: Pass" << std::endl;
    std::cout << "Congratulations!";

}
else {

    std::cout << "Result: Fail" << std::endl;
    std::cout << "Try again.";

}

22. Checking Discount Eligibility

double amount = 1500;

if (amount >= 1000) {
    std::cout << "Discount available";
}
else {
    std::cout << "No discount";
}

The customer gets the message for a discount when the amount is at least 1000.

23. Checking Voting Eligibility

#include <iostream>

int main() {

    int age;

    std::cout << "Enter age: ";
    std::cin >> age;

    if (age >= 18) {
        std::cout << "Eligible";
    }
    else {
        std::cout << "Not eligible";
    }

    return 0;
}

24. Checking Login Details

#include <iostream>
#include <string>

int main() {

    std::string username;
    std::string password;

    std::cout << "Username: ";
    std::cin >> username;

    std::cout << "Password: ";
    std::cin >> password;

    if (username == "admin" && password == "1234") {
        std::cout << "Login successful";
    }
    else {
        std::cout << "Invalid login details";
    }

    return 0;
}

25. Nested if inside if-else

An if statement can contain another if statement.

int age = 25;
bool hasID = true;

if (age >= 18) {

    if (hasID) {
        std::cout << "Entry allowed";
    }
    else {
        std::cout << "ID required";
    }

}
else {

    std::cout << "You are underage";

}

26. Positive or Negative Number

#include <iostream>

int main() {

    int number;

    std::cout << "Enter number: ";
    std::cin >> number;

    if (number >= 0) {
        std::cout << "Positive or zero";
    }
    else {
        std::cout << "Negative";
    }

    return 0;
}

27. Complete Student Result Example

#include <iostream>

int main() {

    int marks;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    if (marks >= 40) {

        std::cout << "Pass" << std::endl;

        if (marks >= 75) {
            std::cout << "Good performance";
        }

    }
    else {

        std::cout << "Fail" << std::endl;
        std::cout << "Work harder";

    }

    return 0;
}

28. if-else with Ternary Operator

For simple two-choice assignments, C++ also provides the conditional or ternary operator. However, a normal if-else statement is often easier for beginners to read.

int age = 20;

if (age >= 18) {
    std::cout << "Adult";
}
else {
    std::cout << "Minor";
}

The above logic can also be written using a ternary operator, but understanding if-else first is important.

29. Common if-else Mistakes

  • Using = instead of == for comparison.
  • Forgetting parentheses around the condition.
  • Putting an unnecessary semicolon after the if condition.
  • Forgetting curly braces when multiple statements are required.
  • Writing else without a corresponding if.
  • Using incorrect logical operators.
  • Writing conditions that do not represent the intended logic.

Incorrect:

if (marks >= 40); {
    std::cout << "Pass";
}
else {
    std::cout << "Fail";
}

Avoid the unnecessary semicolon after the if condition.

30. if-else Final Summary

Part Purpose
if Executes code when the condition is true.
else Executes code when the condition is false.
== Checks equality.
>, < Compare values.
&& Combines conditions where both must be true.
|| Combines conditions where at least one can be true.
if (condition) {
    // true block
}
else {
    // false block
}

📌 Key Points

  • The if-else statement provides two possible paths.
  • The if block executes when the condition is true.
  • The else block executes when the condition is false.
  • Only one of the two blocks executes.
  • Relational operators are commonly used in conditions.
  • Logical operators can combine multiple conditions.
  • The modulus operator is useful for even/odd checking.
  • User input can be checked using if-else.
  • String and character values can also be compared.
  • Nested if statements can be used for more complex decisions.

🧠 Quick Quiz

Question: What happens when the condition of an if statement is false in an if-else statement?