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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>