Back to Course

Data Structures and Algorithms with C++

0% Complete
0/0 Steps

Control flow statements are an important part of programming in any language, including C++. They allow a programmer to control the execution of a program based on certain conditions that are set within the code. Control flow statements are used to create loops, make decisions, and perform other tasks. In this article, we will explore the various control flow statements in C++ and learn how to use them effectively.

What is a Control Flow Statement?

A control flow statement is a command in a programming language that tells a program how to behave. Control flow statements are used to create loops, make decisions, and perform other tasks. Control flow statements are used to determine which code will be executed and in what order. The most common control flow statements in C++ are the if statement, the switch statement, the while loop, and the for loop.

The if Statement

The if statement is one of the most commonly used control flow statements in C++. It is used to evaluate a condition, and depending on the result of the evaluation, a certain set of instructions will be executed. Here is an example of an if statement in C++:

if (condition) {
   // code to be executed
}

The condition can be an expression that evaluates to true or false, such as an equality comparison. The code inside the brackets will be executed only if the condition is true.

The switch Statement

The switch statement is another control flow statement in C++. It is used to execute a block of code depending on the value of a variable or expression. The switch statement is similar to the if statement, but it is more efficient in certain situations. Here is an example of a switch statement in C++:

switch (expression) {
   case value1:
      // code to be executed if expression = value1
      break;
   case value2:
      // code to be executed if expression = value2
      break;
   default:
      // code to be executed if expression does not equal any of the cases
}

The expression can be any type of variable, such as an int, char, or string. The switch statement will then evaluate the expression and run the corresponding code for the case that matches the value of the expression.

The while Loop

The while loop is another control flow statement in C++ that is used to execute a block of code multiple times. The while loop will continue to execute the code until the condition evaluates to false. Here is an example of a while loop in C++:

while (condition) {
   // code to be executed
}

The condition can be any expression that evaluates to true or false. The code inside the brackets will be executed only if the condition is true. The while loop will keep executing the code until the condition evaluates to false.

The for Loop

The for loop is another control flow statement in C++ that is similar to the while loop, but it is more efficient in certain situations. The for loop is used to execute a block of code multiple times, but it also allows the programmer to set a specific number of iterations. Here is an example of a for loop in C++:

for (int i = 0; i < 10; i++) {
   // code to be executed
}

The for loop will execute the code inside the brackets 10 times. The int i is a counter that is set to zero and incremented by one each time the loop is executed. The loop will continue to execute until the counter is equal to 10.

Conclusion

In this article, we explored the various control flow statements in C++ and learned how to use them effectively. We examined the if statement, the switch statement, the while loop, and the for loop. Control flow statements are an important part of programming in C++ and are used to create loops, make decisions, and perform other tasks.

Exercises

Write a program that uses a for loop to print out the numbers from 1 to 10.

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << std::endl;
    }

    return 0;
}

Write a program that uses a while loop to print out the numbers from 1 to 10.

#include <iostream>

int main() {
    int i = 1;
    while (i <= 10) {
        std::cout << i << std::endl;
        i++;
    }

    return 0;
}

Write a program that uses an if statement to print out the numbers from 1 to 10 if the variable x is greater than 5.

#include <iostream>

int main() {
    int x = 6;
    if (x > 5) {
        for (int i = 1; i <= 10; i++) {
            std::cout << i << std::endl;
        }
    }

    return 0;
}

Write a program that uses a switch statement to print out the numbers from 1 to 10 depending on the value of the variable x.

#include <iostream>

int main() {
    int x = 5;
    switch (x) {
        case 1:
            for (int i = 1; i <= 10; i++) {
                std::cout << i << std::endl;
            }
            break;
        case 2:
            for (int i = 1; i <= 10; i++) {
                std::cout << i << std::endl;
            }
            break;
        case 3:
            for (int i = 1; i <= 10; i++) {
                std::cout << i << std::endl;
            }
            break;
        case 4:
            for (int i = 1; i <= 10; i++) {
                std::cout << i << std::endl;
            }
            break;
        case 5:
            for (int i = 1; i <= 10; i++) {
                std::cout << i << std::endl;
            }
            break;
    }

    return 0;
}

Write a program that uses a for loop to print out the numbers from 1 to 10, and then prints out “Done” once the loop is finished.

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << std::endl;
    }
    std::cout << "Done" << std::endl;

    return 0;
}