Beginning C++: Condition Analysis and Branching (If, Switch)

Home / Beginning C++: Condition Analysis and Branching (If, Switch)

In more complex situation, there is a need for application to run a certain codes in regards of situation. Thus we need to analyze what condition to execute what codes. It is called condition analysis, but widely known as branching. Condition analysis or branching is no more than selecting statement which will be executed. The selection is based on some conditions – represented by one or more boolean expression – applied.

C++ has two type structure to implement branching: if structure and switch structure.

Statements inside of branching block will be executed only if the condition is satisfied / fulfilled. Which in turn, if condition is not achieved, then the statements / codes won’t be executed.

Let’s see a case in real world. Note this sentence:

If Andy passed the exam then Andy will get a prize

This simple sentence tells us what will happen to Andy. As demonstrated here we know that Andy will get prize if he pass the exam. Otherwise, he won’t. The action defined here is Andy will get a prize. But to do the action, there is a condition, which is Andy must pass the exam.

In programming world, similar case applied, of course in some notion. Programming world use a formalized form in which we will discuss in this article.

If Structure

The first structure we will discuss is IF. This structure is the simplest of all things. Let’s see following snippet:

// If there are more than 1 statement
if (condition) {
   statement1;
   statement2;
}

// If there is only 1 statement
if (condition) statement;

There is the simplest structure we have, if with only one condition analysis. Yes, the statement(s) will be execute if the condition is satisfied. Let’s see how we can use in the actual program.

#include <iostream>
using namespace std;

int main() {
   int number;

   // Input an integer number from user
   cout << "Please input number: " ;
   cin >> number;

   if (number > 10)
      cout << "You give me " << number << ", it's more than 10 :D" << endl;

   return 0;
}

As you can see, the condition is consist of comparison operations. In general, condition can be formed by one or more comparison expression merged by logical operation such as: && (and), || (or). This we call as complex condition structure. Let’s see this example:

#include <iostream>
using namespace std;

int main() {
char c;

// Input a character from user
cout << "Please input a character: " ;
cin >> c;

if ( (c == 'A') || (c == 'a') ||
(c == 'I') || (c == 'i') ||
(c == 'U') || (c == 'u') ||
(c == 'E') || (c == 'e') ||
(c == 'O') || (c == 'o'))
cout << "You give me a vowel" << endl;

return 0;
}

With a simple analysis, we can see that the statement cout << “You give me a vowel” << endl can be can be executed if one of those multiple condition is satisfied (you can see why).

One or more condition can be made to this structure. In fact, it is easy to write. For a starter, let see how we can analyze two condition:

if (condition) {
   statement_if_condition_is_satisfied
} else {
   statement_if_condition_is_not_satisfied
}

There we have two situation: a situation if condition is satisfied, and situation where condition is not satisfied. If the application failed to satisfied the condition, the execution will fall to block following else word. In here we will see how we the usage in the real world programming:

#include <iostream>
using namespace std;

int main() {
   int number;

// Input an integer number from user
   cout << "Please input a number: " ;
   cin >> number;

   if ( number % 2 == 0 ) {
      cout << "You give me an even number" << endl;
   } else {
      cout << "You give me an odd number" << endl;
   }

   return 0;
}

There we analyze whether the given number is an even or an odd number. Wee that a number which is not an even number must be an odd number, so we just applied one condition to check and if the condition is not satisfied it will fall to the else block of statement.

Now how if we want to inspect more than that? Simple, we could write something like this:

if (condition-1) {
   statement_if_condition_1_satisfied
} else if (condition-2) {
   statement_if_condition_2_satisfied
} else if (condition-3) {
   statement_if_condition_3_satisfied
} ...

} else {
   statement_if_no_condition_is_satisfied
}

Yes, we can use else if to analyze other condition that might happen.

Switch Structure

Aside from if structure, C++ offer us another condition analysis structure, switch.

Switch is like an actual switch. Imagine there is an electronic switch which connecting two endpoint. There are two condition for this switch at a time: on or off. On an observation, we could see that if the switch is on, the two endpoint will be connected. Otherwise the two endpoint is disconnected. Therefore we can see there are two different action: connected and disconnect, which is drived by two different condition: on or off.

Similar to that, switch need a variable in which will be observed. Switch then compare the value to the list of value might equal to it.

If we transform the analogy to a C++ snippet, we will get something like this:

switch ( electronic_switch) {
case on:
   connected;
   break;

case off:
   disconnected;
   break;

default:
}

As we can see, the variable electronic_switch is being inspected and then it will be compared to two value: on and off. Once it is matched, it will begin to execute whatever code later. Therefore, we must set a stop / break point. This break statement then stop the execution and then finishing the switch statement.

Oh, and don’t forget that the value compared using case keyword must be a constant. You can’t compare the value which is being inspected to a variable.

What if we forgot to give break there? Switch will continue executing codes until it found break or until it reach end of switch statement. Be careful!

Now let see the actual use:

#include <iostream>
using namespace std;

int main() {
   int number;

   cout << "Input a number (1..7): ";
   cin >> number;

   switch(number) {
      case 1: cout << "You give me one" << endl; break;
      case 2: cout << "You give me two" << endl; break;
      case 3: cout << "You give me three" << endl; break;
      case 4: cout << "You give me four" << endl; break;
      case 5: cout << "You give me five" << endl; break;
      case 6: cout << "You give me six" << endl; break;
      case 7: cout << "You give me seven" << endl; break;
   }

   return 0;
}

Nested Condition Analysis

Now suppose we want to analyze something after some condition is satisfied. This common situation is called as nested branching / nested condition analysis. You can use if of switch inside of another if or switch structure.

How to do it? Well, I will let you figure it out 😛

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial