Branching control structures are used when we want the machine to choose an alternative based on a binary true / false assertion. If the statement is true, the computer executes an action. Alternatively, if the statement is false, it may execute a different action. The branching control structure depends on the bool data type; all branching statements are conditioned upon an assertion that ultimately evaluates to the Boolean value of either true or false.
We declare variables of type bool as we declare variables of other types, by writing the name of the data type and then the variable name.
#include <iostream>
using namespace std;
int main(void){
bool a = true;
bool b = false;
cout << "bool a is " << a << endl;
cout << "bool b is " << b << endl;
return 0;
}
Note that unlike in regular C, in C++ bool is a fundamental data type. As in C, false corresponds to 0 and true corresponds to a non-zero value, typically 1.
Boolean expressions are made up of logical values and operands. Every logical expression ultimately evaluates to either true or false. Relational operators enables us to compare two mathematical expressions and derive a logical value from it.
#include <iostream>
using namespace std;
int main(void){
int x,y;
bool returnValue;
cout << "Enter a number: ";
cin >> x;
cout << "Enter another number: ";
cin >> y;
returnValue = x > y;
cout << x << " > " << y << " : " << returnValue << endl;
returnValue = x < y;
cout << x << " < " << y << " : " << returnValue << endl;
return 0;
}
The fundamental control structure that allows branches in the flow of control is the if statement. By using the if statement, we can ask a question in the form of a logical assertion and choose a course of action depending on how the assertion is evaluated. The if statement consists of the if keyword followed by a logical expression contained in parentheses, follow by the statement or statement block to be conditionally executed. Optionally, we can follow this statement or statement block with the keyword else, followed by a statement or statement block to be executed if the expression evaluated to false.
#include <iostream>
using namespace std;
int main(void){
bool trueVal = true;
if(trueVal){
cout << "This statement will be displayed." << endl;
} else {
cout << "This statement will not be displayed." << endl;
}
if(trueVal==0){
cout << "This statement will not be displayed." << endl;
} else {
cout << "But this one will, as Boolean true does not equal 0." << endl;
}
return 0;
}
Compound statements must be enclosed in curly braces. As a matter of style, it is considered best practice to include any statements whose execution is conditional in curly braces, even if there is only one statement.
Be aware that the else portion of an if-else statement is optional.
#include <iostream>
using namespace std;
int main(void){
bool val;
int a, b;
a = 404;
b = 8086;
if(a!=b){
cout << a << " does not equal " << b << endl;
}
if(a<b){
cout << a << " is less than " << b << endl;
}
return 0;
}
It is possible to place and if statement within another if statement. By nesting our if statements, we can implement complex logic that enables our computer to deal in a nuanced manner with varying input. Nested if statements enable us to create multiway branches, were the flow of execution has more than two possible paths.
#include <iostream>
using namespace std;
int main(void){
int temp = 40;
if(temp > 20){
if(temp < 33){
cout << "Let's go for a hike " << endl;
} else {
cout << "Let's go swimming " << endl;
}
} else {
if(temp > 0){
cout << "Let's stay inside " << endl;
} else {
cout << "Let's go skiing " << endl;
}
}
return 0;
}
Note that is important to consider human readability as well when designing branching structures.