← Back to list

Conditions and Control Statements in C++

When writing programs, we often need the computer to make decisions or repeat tasks depending on certain conditions. This is where…

Abdallah · 2026-03-07 16:42 · 0 claps · 3.6 min read paywalled
#cpp-programming #loop #logical #programming #software-development
Open on Medium ↗
Wiki topics: 💻 · Programming

Conditions and Control Statements in C++

When writing programs, we often need the computer to make decisions or repeat tasks depending on certain conditions. This is where conditions and control statements become essential in programming.

In C++, control statements allow developers to determine how and when different parts of the program should execute. They help create dynamic and intelligent programs that respond to input and different situations.

In this article, we will explore the most important control structures in C++, including:

  • if and if-else statements
  • Logical operators
  • Loops (while, do-while, and for)
  • The switch statement

These structures form the foundation of program logic and decision-making.

The if Statement

The if statement is used when we want a program to execute a block of code only if a specific condition is true.

Example

#include <iostream>
using namespace std;

int main() {

    int number = 10;

    if (number > 0) {
        cout << "The number is positive." << endl;
    }

    return 0;
}

Explanation

The condition number > 0 is evaluated first. If the condition is true, the program prints the message. If the condition is false, the program skips the block of code.

The if–else Statement

Sometimes a program must choose between two possible outcomes. The if-else statement allows us to define an alternative block of code if the condition is false.

Example

#include <iostream>
using namespace std;

int main() {

    int number = -5;

    if (number > 0) {
        cout << "Positive number" << endl;
    }
    else {
        cout << "Negative number" << endl;
    }

    return 0;
}

In this case:

  • If the condition is true, the first message is printed.
  • Otherwise, the code inside the else block is executed.

Logical Operators in C++

Logical operators allow us to combine multiple conditions in a single statement.

Operator: &&Meaning: Logical AND

Operator: !` `Meaning: Logical NOT

These operators are widely used with conditional statements.

Logical AND (&&)

The AND operator requires both conditions to be true.

Example

#include <iostream>
using namespace std;

int main() {

    int age = 25;

    if (age >= 18 && age <= 30) {
        cout << "You are in the young adult category." << endl;
    }

    return 0;
}

The message appears only if both conditions are satisfied.

Logical OR (||)

The OR operator requires at least one condition to be true.

Example

#include <iostream>
using namespace std;

int main() {

    int number = 0;

    if (number == 0 || number == 10) {
        cout << "The number is either 0 or 10." << endl;
    }

    return 0;
}

If one of the conditions is true, the statement will execute.

Logical NOT (!)

The NOT operator reverses the logical value of a condition.

Example

#include <iostream>
using namespace std;

int main() {
    bool isLoggedIn = false;

    if (!isLoggedIn) {
        cout << "User is not logged in." << endl;
    }

    return 0;
}

If isLoggedIn is false, the condition becomes true after applying !.

Loops in C++

Loops are used to repeat a block of code multiple times until a condition is no longer satisfied.

The while Loop

The while loop repeats code as long as a condition remains true.

Example

#include <iostream>
using namespace std;

int main() {

    int i = 1;

    while (i <= 5) {
        cout << i << endl;
        i++;
    }

    return 0;
}

This program prints numbers from 1 to 5.

The do-while Loop

The do-while loop is similar to the while loop, but the code executes at least once before checking the condition.

Example

#include <iostream>
using namespace std;

int main() {

    int i = 1;

    do {
        cout << i << endl;
        i++;
    } while (i <= 5);

    return 0;
}

The loop prints numbers from 1 to 5, but the condition is checked after execution.

The for Loop

The for loop is typically used when the number of iterations is known.

Example

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {
        cout << i << endl;
    }

    return 0;
}

This loop also prints numbers from 1 to 5, but it combines initialization, condition, and increment in a single line.

The switch Statement

The switch statement is used when a variable can have multiple specific values. It is often more organized than using many if-else statements.

Example

#include <iostream>
using namespace std;

int main() {

    int day = 3;

    switch(day) {

        case 1:
            cout << "Monday" << endl;
            break;

        case 2:
            cout << "Tuesday" << endl;
            break;

        case 3:
            cout << "Wednesday" << endl;
            break;

        case 4:
            cout << "Thursday" << endl;
            break;

        case 5:
            cout << "Friday" << endl;
            break;

        default:
            cout << "Weekend or invalid day" << endl;
    }

    return 0;
}

Explanation

  • The switch statement evaluates the variable day.
  • Each case represents a possible value.
  • The break statement stops the execution after a match is found.
  • The default case runs if none of the cases match.

Conclusion

Conditions and control statements are essential components of programming in C++. They allow developers to create programs that can make decisions, repeat actions, and respond to different situations.

By mastering structures such as if, if-else, logical operators, loops, and switch statements, programmers can build more flexible and powerful applications.

Understanding these concepts is an important step toward writing efficient and well-structured programs in C++.


메타데이터
post_id
7bd2c4d64c8d
slug
conditions-and-control-statements-in-c-7bd2c4d64c8d
url
https://medium.com/@abdallhr649/conditions-and-control-statements-in-c-7bd2c4d64c8d
canonical_url
https://medium.com/@abdallhr649/conditions-and-control-statements-in-c-7bd2c4d64c8d
author_url
https://medium.com/@abdallhr649
status
ok
fetched_at
2026-06-21 15:33:18