
You might ask:
What is branching and why is it called branching?
For those who have never been to college or learned about algorithms and flowcharts, this may be the first new term you hear.
Isitilah this is actually to describe the flow of the program that is branching.
On the flow chart, logic “jika..maka” is depicted in the form of branches.
Therefore, this is called branching.
In addition to branching, this structure is also called: control flow, decision, condition structure, if structure, etc.
Branching will be able to make the program think and determine actions in accordance with the logic/conditions we provide.
In C++ programming, there are 6 branching forms that we must tell.
What the hell is that?
Let's discuss…
1. Branching if
An if branch is a branch that only has one block of options when the conditions are true.
Take a look at the following flowchart:
We can read the flowchart like this:
“If the total shopping is greater than Rp 100.000, Then show the message Congratulations, you can gift”
If it is below Rp 100,000 how?
Yes, the message will not be displayed.
Let's try in C++ program.
Create a file called if.cpp, then fill it with the following code.
include
using namespace std;
int main(){
cout << "=== Payment Program ===" << endl;
unsigned int total_shop;
cout << "Total shopping input: ";
cin >> total_shop;
// using branching if
if(total_shopping > 100000){
cout << "Congratulations! you can gift" << endl;
}
cout << "Thank you for shopping in our store" << endl;
return 0;
}
Compile the code with the command:
g++ if.cpp -o ifThen execution by command:
./if
Result:
you guys try it yourself
Look at this section:
// using branching if
if(total_shopping > 100000){
cout << "Congratulations! you can gift" << endl;
}This is called program block.
A program block contains a set of expressions and statements for a computer to work on.
Program block on C++, always start with { curly brackets and will end with } curly brackets.
If there is only one line of expression or statement in the block, it can not be written parentheses.
if (total_shopping > 100000)
cout << "Congratulations! you can gift" << endl;2. Branching if/else
The if/else branch is a branch that has two optional blocks.
Block the first option for the correct condition, and the second option for the wrong condition (else).
Check out this flowchart:
3. Branching if/else/if
The if/else/if branch is a branch that has more than two blocks of options.
Take a look at the following flowchart:
Look at the block I'm giving the color to…
This is a block for branching if/else/if. We can add any block we want.
Example of the Program:
include
using namespace std;
int main(){
int value;
grade string;
cout << "=== Program Grade Value ===" << endl;
cout << "Splink final value: ";
cin >> value;
// using branching if/esle/if
if (value >= 90) {
grade = "A";
} else if (value >= 80) {
grade = "B+";
} else if (value >= 70) {
grade = "B";
} else if (value >= 60) {
grade = "C+";
} else if (value >= 50) {
grade = "C";
} else if (value >= 40) {
grade = "D";
} else if (value >= 30) {
grade = "E";
} else {
grade = "F";
}
cout << "Your grade: " << grade << endl;
return 0;
}The result:
4. Branching Switch/Case
Branching switch/case is another form of branching if/else/if.
Structure as this:
switch(variable){
case :
// code block
break;
case :
// code block
break;
default:
// code block
}We can create as many blocks of code (case) as desired inside the switch block.
At , we can fill it with values that will later be compared with the varable.
Each case must end with a break. Especially for the default, there is no need to end with a break because he is located at the end.
Giving a break is intended so that the program stops checking the next case when a case is fulfilled.
Example:
include
using namespace std;
int main(){
char grade;
cout << "Split grade: ";
cin >> grade;
switch (toupper(grade)){
case 'A':
break;
case 'B':
case 'C':
cout << "Good!" << endl;
break;
case 'D':
cout << "You pass" << endl;
break;
case 'E':
case 'F':
cout << "You're remidi" << endl;
break;
default:
cout << "False Grade!" << endl;
}
return 0;
}The result:
Watch the program above…
We use the function toupper() to convert lowercase letters into capital letters or uppercase letters.
switch (toupper(grade)){
...
}Why use toupper()?
This is because we can insert lowercase and upper case letters to check the grade.
Next try to note case 'B' and case 'E'.
Both cases have no contents.
This means.he will follow on the next case.
For example, if we enter B then case C will be selected.
Likewise, when we enter E, then case F will be selected.
5. Branching with Ternary Opertor
Branching using a ternary opreator is another form of if/else branching.
You could say:
Short form of if/else.
Ternary operators are also known as condition operators (conditional operators).
The structure is like this:
(condition) ? true: falseA part of the condition can be filled with expressions that produce true and false values.
Then set a question mark ? it's the choice part.
If the value condition is correct, then true will be chosen. But if it is wrong, then false will be chosen.
For more details, let's try…
Create a new file called ternary.cpp, then fill it with the following code:
include
using namespace std;
int main(){
int answer;
cout << "What is the result of 3+4?" << endl;
cout << "answer> ";
cin >> answer;
result string = (answer == 7) ? "Yes": "Wrong";
cout << "Your answer: " << result << endl;
return 0;
}The result:
6. Nested Branching (Nested)
We can also create branching blocks inside the branching. This is called a sharp branching or nested if.
Example: 📄 nested.cpp
include
using namespace std;
int main(){
string username, password;
cout << "=== Welcome ===" << endl;
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
if (username == "petanikode"){
if (password == "coffee"){
cout << "Welcome boss!" << endl;
} else {
cout << "Password wrong, try again!" << endl;
}
} else {
cout << "You are not registered" << endl;
}
return 0;
}The result:
What Next?
A branching block is a block of code that we must understand.
Because we will use it a lot in making programs.
The six forms of branching above, are the basic forms.
Therefore, you should multiply the exercise with other case examples to understand more.
Next please learn about:
Learn C++ #08: Getting to Know the Repetition Block
This is a flowchart to check the password.
If the password is correct, the message on the green block will be displayed: “Welcome boss!"
But if it is wrong, then the message in the red block will be displayed: “Password is wrong, try again!"
Then, the message that is in the gray block will still be displayed, because it is not part of the if/else branching block.
Pay attention to the direction of the arrow, each if/else block leads to it…
For more details, let's try in the program.
Create a new file called if_else.cpp, then fill it with the following code:
include
using namespace std;
int main(){
password string;
cout << "======== Login =======" == endl;
cout << "Input password: ";
cin >> password;
// if/else branching
if (password == "coffee"){
cout << "Welcome boss!" << endl;
} else {
cout << "Password wrong, try again!" << endl;
}
cout << "Thank you for using this app!" << endl;
return 0;
}The result: