Control Statements in C++: A Comprehensive Guide for 2023

Article by: Manish Methani

Last Updated: October 21, 2021 at 10:04am IST
6 min 6 sec read

Control statements are an essential part of any programming language, and C++ is no exception. In C++, control statements are used to control the flow of execution of a program. They enable us to perform different actions based on certain conditions or loops.

In this tutorial, we'll take a deep dive into C++ control statements, including their syntax, types, and practical examples. So, let's get started!

Types of Control Statements in C++

C++ has three types of control statements:

  1. Conditional statements: These statements are used to execute a particular block of code if a certain condition is true. The two main types of conditional statements in C++ are the if statement and the switch statement.

  2. Looping statements: These statements are used to execute a block of code repeatedly until a particular condition is met. The main types of looping statements in C++ are the for loop, while loop, and do-while loop.

  3. Jump statements: These statements are used to transfer control from one part of the program to another. The main types of jump statements in C++ are the break statement, continue statement, and goto statement.

Syntax of Control Statements in C++

Now, let's take a look at the syntax of each of the control statements in C++:

  1. If statement:
if(condition){
  // code to be executed if the condition is true
}

Example:

int x = 10;
if(x == 10){
  cout << "x is equal to 10" << endl;
}

       2. Switch statement:

switch(expression){
  case constant1:
    // code to be executed if expression is equal to constant1
    break;
  case constant2:
    // code to be executed if expression is equal to constant2
    break;
  default:
    // code to be executed if none of the above cases are true
}

Example:

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

      3. For loop:

for(initialization; condition; increment/decrement){
  // code to be executed in each iteration of the loop
}

Example:

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

4. While loop:

while(condition){
  // code to be executed in each iteration of the loop
}

Example:

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

    5. Do-while loop:

do{
  // code to be executed in each iteration of the loop
}while(condition);

Example:

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

     6. Break statement:

for(int i = 0; i < 5; i++){
  if(i == 3){
    break;
  }
  cout << i << endl;
}

     7. Continue statement:

for(int i = 0; i < 5; i++){
  if(i == 3){
    continue;
  }
  cout << i << endl;
}

    8. goto statement:

goto statement allows the program to jump to a labeled statement, effectively bypassing any statements in between. While goto statements can be a powerful tool, they can also make code difficult to read and maintain if used improperly.

Syntax of Goto Statement in C++

The syntax for a goto statement in C++ is as follows:

goto label;

Where "label" is a user-defined identifier that identifies the statement to which the program should jump. The labeled statement must be within the same function as the goto statement, and the label itself cannot be a keyword.

Creating Labeled Statements

To create a labeled statement, simply add a colon (:) after the identifier of the statement. For example:

int main() {
  int i = 1;
loop:
  cout << i << endl;
  i++;
  if (i <= 10) goto loop;
  return 0;
}

In this example, we create a labeled statement called "loop" that simply prints the value of the variable i to the console. We then use a goto statement to jump back to this labeled statement until the value of i reaches 10.

Using Goto Statements in C++ Programs

While goto statements can be a powerful tool in C++ programming, they should be used with caution. In general, it's best to avoid using them unless there is no other viable option. One common use case for goto statements is to break out of nested loops, as in the following example:

for (int i = 1; i <= 10; i++) {
  for (int j = 1; j <= 10; j++) {
    if (i == 5 && j == 5) goto endloop;
    cout << i << "," << j << endl;
  }
}
endloop:
cout << "Loop exited" << endl;

In this example, we use a nested for loop to iterate through every combination of i and j from 1 to 10. However, if i and j are both equal to 5, we use a goto statement to break out of the loop and print "Loop exited" to the console.

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com