1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

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.

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com

GSTIN: 27CTSPM6231L1Z0 - Registered under Goods and Services Tax