Skip to main content

Command Palette

Search for a command to run...

Control Structures in C++

Published
2 min read
Control Structures in C++

Control structures are an essential part of any programming language, including C++. They enable you to control the flow of execution within your program by defining the order in which statements are executed, based on certain conditions. In this article, we will explore the most common control structures in C++: if, if-else, switch, for, while, and do-while.

Conditional statements

Conditional statements allow you to execute different parts of your code based on specific conditions:

  • if statement: Executes a block of code if a specified condition is true.

      int age = 18;
    
      if (age >= 18) {
          std::cout << "You are an adult." << std::endl;
      }
    
  • if-else statement: Executes one block of code if a condition is true, and another block of code if the condition is false.

      int age = 18;
    
      if (age >= 18) {
          std::cout << "You are an adult." << std::endl;
      } else {
          std::cout << "You are a minor." << std::endl;
      }
    
  • switch statement: Selects one of many code blocks to be executed, based on the value of a specified variable or expression.

      int day = 3;
    
      switch (day) {
          case 1:
              std::cout << "Monday" << std::endl;
              break;
          case 2:
              std::cout << "Tuesday" << std::endl;
              break;
          case 3:
              std::cout << "Wednesday" << std::endl;
              break;
          // ...
          default:
              std::cout << "Invalid day" << std::endl;
      }
    

Loops

Loops are used to repeatedly execute a block of code until a specified condition is met:

  • for loop: Executes a block of code a specified number of times.

      for (int i = 0; i < 10; i++) {
          std::cout << "Iteration " << i << std::endl;
      }
    
  • while loop: Executes a block of code as long as a specified condition is true.

      int counter = 0;
    
      while (counter < 10) {
          std::cout << "Iteration " << counter << std::endl;
          counter++;
      }
    
  • do-while loop: Executes a block of code at least once, and then repeats the loop as long as a specified condition is true.

      int counter = 0;
    
      do {
          std::cout << "Iteration " << counter << std::endl;
          counter++;
      } while (counter < 10);
    

Conclusion

Control structures in C++ allow you to manipulate the flow of execution within your program, making it more dynamic and adaptable. In this article, we covered the most common control structures, such as conditional statements (if, if-else, switch) and loops (for, while, do-while). Mastering control structures is crucial for writing efficient and organized C++ code. In future articles, we will delve into more advanced topics, such as functions, classes, and inheritance.

More from this blog

DarleaNews

32 posts

I want to teach people what I learn over time, learning things myself during the process.