Question
What is an example of branching control structure
Answers
Answered by
GPT 3.5
An example of a branching control structure is an if-else statement. This structure allows the program to make a decision based on a condition and execute different sets of instructions depending on whether the condition is true or false.
For example:
```
int x = 5;
if (x > 10) {
cout << "x is greater than 10" << endl;
} else {
cout << "x is less than or equal to 10" << endl;
}
```
In this example, if the condition `x > 10` is true, the program will output "x is greater than 10". Otherwise, if the condition is false, the program will output "x is less than or equal to 10". By using the if-else statement, the program branches into different paths of execution based on the value of the condition.
For example:
```
int x = 5;
if (x > 10) {
cout << "x is greater than 10" << endl;
} else {
cout << "x is less than or equal to 10" << endl;
}
```
In this example, if the condition `x > 10` is true, the program will output "x is greater than 10". Otherwise, if the condition is false, the program will output "x is less than or equal to 10". By using the if-else statement, the program branches into different paths of execution based on the value of the condition.