C++ control statements are used to control the flow of execution in a program. Some common control statements include:
C++ also provides the break, continue, and goto statements for additional control over the flow of execution.
The ifstatement in C++ is used to conditionally execute a block of code based on the value of a Boolean expression. The syntax for an if statement is as follows:
if (expression) {
// code to execute if expression is true
}
Example:
Here's an example of an if statement that checks if a variable x is greater than 10:
int x = 15;
if (x > 10) {
std::cout << "x is greater than 10" << std::endl;
}
This code will output "x is greater than 10" to the console because the Boolean expression x > 10 is true.
An if-elsestatement allows for two different blocks of code to be executed based on whether an expression is true or false. The syntax for an if-elsestatement is as follows:
if (expression) {
// code to execute if expression is true
} else {
// code to execute if expression is false
}
Example:
Here's an example of an if-else statement that checks if a variable x is greater than 10:
int x = 15;
if (x > 10) {
std::cout << "x is greater than 10" << std::endl;
} else {
std::cout << "x is not greater than 10" << std::endl;
}
This code will output "x is greater than 10" to the console because the Boolean expression x > 10 is true.
And if x=5, the output will be "x is not greater than 10"
C++ provides several types of loops that can be used to repeatedly execute a block of code. Here are some common types of loops in C++:
for (initialization; condition; increment) {
// code to execute
}
Here's an example of a for loop that prints the numbers from 1 to 10:
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
This code will output the numbers from 1 to 10 to the console.
while (condition) {
// code to execute
}
Here's an example of a while loop that prints the numbers from 1 to 10:
int i = 1;
while (i <= 10) {
std::cout << i << std::endl;
i++;
}
This code will also output the numbers from 1 to 10 to the console.
do {
// code to execute
} while (condition);
Here's an example of a do-while loop that prints the numbers from 1 to 10:
int i = 1;
do {
std::cout << i << std::endl;
i++;
} while (i <= 10);
This code will also output the numbers from 1 to 10 to the console.
for (type variable : container) {
// code to execute
}
Here's an example of a range-based for loop that prints the elements of an array:
int arr[] = {1, 2, 3, 4, 5};
for (int x : arr) {
std::cout << x << std::endl;
}
This code will output the numbers 1 to 5 in the console.
All the above loops are used to control the flow of the program based on different criteria, and they are useful in different situations. The choice of which loop to use depends on the specific requirements of the task at hand.
In C++, the break, continue, and goto statements provide additional control over the flow of execution in loops and other control structures.
goto label;
...
label: statement;
The gotostatement is not recommended to use in modern programming, because it can make the code hard to read and reason about.
Here's an example of using break and continue in a for loop that prints the numbers from 1 to 10, but skips the number 5:
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
std::cout << i << std::endl;
}
This code will output the numbers from 1 to 4, 6 to 10 to the console.
Here's an example of using break statement in a while loop that prints the numbers from 1 to 10, but stops when it reaches the number 5:
int i = 1;
while (i <= 10) {
std::cout << i << std::endl;
if (i == 5) {
break;
}
i++;
}
This code will output the numbers from 1 to 5 to the console and stop the execution of the loop.
It is important to use them judiciously, since overusing break and gotostatements can make code harder to understand and maintain.
0 Comments
If You Have Any Doubts, Please tell me know