Lesson 23 of 60 – if Statement in C++
38%

if Statement in C++

The if statement is used to execute a block of code only when a specified condition is true. It is one of the most important decision-making statements in C++.

Note: The if statement checks a condition. If the condition evaluates to true, the statements inside the block are executed. If it is false, they are skipped.

1. What is an if Statement?

An if statement allows a program to make a decision.

if (condition) {
    // code to execute
}

The code inside the braces executes only when the condition is true.

2. Syntax of if Statement

The basic syntax of an if statement is:

if (condition) {
    statement;
}

The condition is written inside parentheses, and the statements to execute are written inside curly braces.

3. Simple if Example

int age = 20;

if (age >= 18) {
    std::cout << "You are an adult";
}

Because 20 is greater than or equal to 18, the message is displayed.

4. if with a Number

int number = 10;

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

The condition checks whether the number is greater than zero.

5. if with Equality Operator

The equality operator == can be used inside an if condition.

int number = 10;

if (number == 10) {
    std::cout << "Number is 10";
}

The condition is true, so the message is displayed.

6. if with Not Equal Operator

int number = 15;

if (number != 10) {
    std::cout << "Number is not 10";
}

Since 15 is not equal to 10, the condition is true.

7. if with Greater Than Operator

int marks = 80;

if (marks > 50) {
    std::cout << "Good marks";
}

The statement executes because 80 is greater than 50.

8. if with Less Than Operator

int age = 15;

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

The condition is true because 15 is less than 18.

9. if with Greater Than or Equal To

int marks = 40;

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

The condition is true because 40 is equal to the minimum required value.

10. if with Less Than or Equal To

int age = 18;

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

The condition is true because age is exactly 18.

11. Multiple Statements inside if

An if block can contain multiple statements.

int marks = 80;

if (marks >= 40) {

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

}

Both statements execute when the condition is true.

12. Using Braces with if

Curly braces are recommended when writing the body of an if statement, especially when there is more than one statement.

if (marks >= 40) {
    std::cout << "Pass";
    std::cout << " Well done!";
}

13. if with Boolean Variable

A Boolean variable can be used directly as a condition.

bool isLoggedIn = true;

if (isLoggedIn) {
    std::cout << "Welcome";
}

The block executes because isLoggedIn is true.

14. if with Logical AND

Multiple conditions can be combined using the logical AND operator &&.

int age = 25;
bool hasID = true;

if (age >= 18 && hasID) {
    std::cout << "Allowed";
}

Both conditions must be true.

15. if with Logical OR

The logical OR operator || allows the block to execute when at least one condition is true.

int day = 6;

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

16. if with Logical NOT

The logical NOT operator ! reverses a Boolean condition.

bool loggedIn = false;

if (!loggedIn) {
    std::cout << "Please log in";
}

Since loggedIn is false, !loggedIn is true.

17. if with User Input

#include <iostream>

int main() {

    int age;

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

    if (age >= 18) {
        std::cout << "You are an adult";
    }

    return 0;
}

The program displays the message only when the entered age is at least 18.

18. Checking Positive Number

#include <iostream>

int main() {

    int number;

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

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

    return 0;
}

19. Checking Even Number

The modulus operator can be used inside an if condition to check whether a number is even.

int number = 20;

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

20. Checking a Passing Mark

int marks;

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

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

The message is displayed when the marks are 40 or greater.

21. Checking a Range

The AND operator can be used to check whether a value is inside a range.

int age = 25;

if (age >= 18 && age <= 60) {
    std::cout << "Age is within the range";
}

22. Comparing Two Variables

int a = 50;
int b = 30;

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

The condition compares the values of two variables.

23. Comparing Strings

#include <iostream>
#include <string>

int main() {

    std::string password = "admin123";

    if (password == "admin123") {
        std::cout << "Correct password";
    }

    return 0;
}

C++ std::string objects can be compared using the == operator.

24. Nested if Statement

An if statement can be placed inside another if statement. This is called a nested if.

int age = 25;
bool hasID = true;

if (age >= 18) {

    if (hasID) {
        std::cout << "Access allowed";
    }

}

25. if with Character

char grade = 'A';

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

The condition compares the character with 'A'.

26. Practical Login Example

#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";
    }

    return 0;
}

The message is displayed only when both username and password match.

27. Practical Discount Example

#include <iostream>

int main() {

    double amount;

    std::cout << "Enter purchase amount: ";
    std::cin >> amount;

    if (amount >= 1000) {
        std::cout << "You are eligible for a discount";
    }

    return 0;
}

The message appears when the purchase amount is at least 1000.

28. Complete if Statement Example

#include <iostream>

int main() {

    int marks;

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

    if (marks >= 40) {
        std::cout << "You passed the examination."
                  << std::endl;

        if (marks >= 75) {
            std::cout << "You scored a high mark.";
        }
    }

    return 0;
}

The second if statement is checked only when the first condition is true.

29. Common if Statement Mistakes

  • Using = instead of == for comparison.
  • Forgetting the parentheses around the condition.
  • Forgetting curly braces when multiple statements belong to the if block.
  • Writing an incorrect logical condition.
  • Using a semicolon immediately after the if condition.
  • Creating unnecessarily complicated nested conditions.
  • Forgetting that an if statement executes only when its condition is true.

Example of a common mistake:

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

The semicolon ends the if statement. Avoid adding an unnecessary semicolon after the condition.

30. if Statement – Final Summary

Part Purpose Example
if Starts a conditional statement if
Condition Checks whether something is true age >= 18
{ } Contains statements to execute { std::cout << "Adult"; }
if (condition) {
    // statements
}

📌 Key Points

  • The if statement is used for decision-making.
  • The code inside the if block executes only when the condition is true.
  • The condition is written inside parentheses.
  • Curly braces define the body of the if statement.
  • Relational operators can be used in if conditions.
  • Logical operators can combine multiple conditions.
  • The modulus operator can be used for even/odd checks.
  • Boolean variables can be used directly as conditions.
  • An if statement can contain another if statement.
  • Do not confuse = with ==.
  • Avoid unnecessary semicolons after an if condition.

🧠 Quick Quiz

Question: Which keyword is used to execute a block of code only when a condition is true?