WikiGalaxy

Personalize

Basic if Statement

Understanding Basic if Statement:

The basic if statement in C++ evaluates a condition. If the condition is true, the code block within the if statement is executed.


#include <iostream>
using namespace std;

int main() {
    int age = 18;
    if (age >= 18) {
        cout << "You are eligible to vote.";
    }
    return 0;
}
    

Console Output:

You are eligible to vote.

if-else Statement

Understanding if-else Statement:

The if-else statement allows you to execute one block of code if the condition is true and another block if it is false.


#include <iostream>
using namespace std;

int main() {
    int number = 10;
    if (number % 2 == 0) {
        cout << "The number is even.";
    } else {
        cout << "The number is odd.";
    }
    return 0;
}
    

Console Output:

The number is even.

Nested if Statements

Understanding Nested if Statements:

Nested if statements are used when you need to check multiple conditions. An if statement inside another if statement is called nested if.


#include <iostream>
using namespace std;

int main() {
    int number = 15;
    if (number > 10) {
        if (number < 20) {
            cout << "The number is between 10 and 20.";
        }
    }
    return 0;
}
    

Console Output:

The number is between 10 and 20.

if-else-if Ladder

Understanding if-else-if Ladder:

The if-else-if ladder is used to check multiple conditions. It executes the block of code associated with the first true condition.


#include <iostream>
using namespace std;

int main() {
    int score = 85;
    if (score >= 90) {
        cout << "Grade A";
    } else if (score >= 80) {
        cout << "Grade B";
    } else if (score >= 70) {
        cout << "Grade C";
    } else {
        cout << "Grade D";
    }
    return 0;
}
    

Console Output:

Grade B

Conditional Operator

Understanding Conditional Operator:

The conditional operator (?:) is a shorthand for the if-else statement. It evaluates a condition and returns one of two values.


#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 10;
    int max = (a > b) ? a : b;
    cout << "The maximum value is " << max;
    return 0;
}
    

Console Output:

The maximum value is 10

Switch Statement

Understanding Switch Statement:

The switch statement allows you to execute different parts of code based on the value of a variable or expression. It's an alternative to if-else-if ladders when dealing with multiple conditions.


#include <iostream>
using namespace std;

int main() {
    int day = 3;
    switch(day) {
        case 1:
            cout << "Monday";
            break;
        case 2:
            cout << "Tuesday";
            break;
        case 3:
            cout << "Wednesday";
            break;
        default:
            cout << "Invalid day";
    }
    return 0;
}
    

Console Output:

Wednesday

Logical Operators

Understanding Logical Operators:

Logical operators are used to combine multiple conditions in an if statement. The most common logical operators are AND (&&), OR (||), and NOT (!).


#include <iostream>
using namespace std;

int main() {
    int age = 20;
    bool hasID = true;
    if (age >= 18 && hasID) {
        cout << "You can enter the club.";
    } else {
        cout << "Access denied.";
    }
    return 0;
}
    

Console Output:

You can enter the club.

Comparative Operators

Understanding Comparative Operators:

Comparative operators are used to compare two values. Common operators include less than (<), greater than (>), equal to (==), and not equal to (!=).


#include <iostream>
using namespace std;

int main() {
    int x = 10, y = 20;
    if (x != y) {
        cout << "x and y are not equal.";
    }
    return 0;
}
    

Console Output:

x and y are not equal.

Combining Conditions

Combining Conditions in if Statements:

You can combine multiple conditions in an if statement using logical operators to create complex decision-making structures.


#include <iostream>
using namespace std;

int main() {
    int age = 25;
    bool isStudent = false;
    if (age >= 18 && age < 30 || isStudent) {
        cout << "Eligible for a discount.";
    } else {
        cout << "Not eligible for a discount.";
    }
    return 0;
}
    

Console Output:

Eligible for a discount.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025