C++ Programming Series: Using Conditional Statements (Part 3)

In the last two previous posts, you got through the if, else and else-if-statements. The uncertain point is the difference between two if-statements and if-statement with an else-if-statement.

Two if-statements are not connected to each other. They don’t have any relation with each other. The compiler have to pass through all of these statements even if anyone of the conditions is true. Example shows three if-statements:

int value = 6;
if(value > 7)
  cout << "value is bigger than 7" << endl;
if(value > 5)
  cout << "value is bigger than 5" << endl;
if(value > 3)
  cout << "value is bigger than 3" << endl;

Output:

value is bigger than 5
value is bigger than 3

An if-statement with an else-if statement are connected to each other. If anyone of the conditions gets true, all others are skipped. Same example below with some corrections:

int value = 6;
if(value > 7)
  cout << "value is bigger than 7" << endl;
else if(value > 5)
  cout << "value is bigger than 5" << endl;
else if(value > 3)
  cout << "value is bigger than 3" << endl;

Output: (Now, there is no unnecessary outputs. It is pretty nonsense to say that value is bigger than 3 when we already said that value is bigger than 5)

value is bigger than 5

This is very important when it comes to getting user input and then, manipulating it for some change in the output. For example, there is a character variable or variable of type, char called key which takes the character (or letter) that you press on the keyboard and in return, print something for you. (Remember, put single inverted commas and not double, for a character or their variables only)

if(key == 'a')
  cout << "Alpha!" << endl;
else if(key == 'b')
  cout << "Beta!" << endl;
else if(key == 'c')
  cout << "Charlie!" << endl;
else if(key == 'd')
  cout << "Delta!" << endl;
else
  cout << "Unknown input..." << endl;

For such cases, it is better to use another conditional statement called as switch-statement. It is only good for comparison done with equality operator (i.e, ==) and numbers.

switch(key)
{
case 'a':
  cout << "Alpha!" << endl;
  break;
 
case 'b':
  cout << "Beta!" << endl;
  break;
 
case 'c':
  cout << "Charlie!" << endl;
  break;
 
case 'd':
  cout << "Delta!" << endl;
  break;
 
default:
  cout << "Unknown input..." << endl;
  break;
}

This code do the same thing as the above one. You may have figured out some of the things by yourself. But let me explain this.

After switch, we have to type in the variable in the round brackets unlike if-statement. Then, we need to put a case with the value that is to be compared with the variable. At the end of case-value pair, we need to put a colon. Then, put one to many lines of code before the ending flower bracket. What? Did we put a starting flower(or curly) bracket? No! So, that means… there is some other way to put an ending to it. The break keyword is only for switch and loop statements. It is used to skip all the codes below it and is used to skip the whole loop, not just a block (You will understand this later if not now). The default keyword is another case which acts like the else-statement and works only when all the above cases are false.

Want some shortcut to understand switch-statement? Here is the format:

switch( [variable] )
{
case [value]:
  // to be executed when variable's value is equal to that of case's value
  break;
  // ...
// [more cases]
  // ...
default:
  // to be executed when variable's value is not equal to any of the above case's value
  break;
}

Pretty long but looks useless! Nah! These are the basis of all beautiful things that you can imagine to do on the screen…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s