Article by: Manish Methani
Last Updated: October 21, 2021 at 10:04am IST
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!
C++ has three types of control statements:
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.
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.
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.
Now, let's take a look at the syntax of each of the control statements in C++:
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.
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.
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.
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.
FlutterFlow Course from Basic to Advanced
Flutter App Development Course in Hindi
Contact: teamcodzify@gmail.com
GSTIN: 27CTSPM6231L1Z0 - Registered under Goods and Services Tax