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

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

In the previous post, we learned some conditional statements. Now, we are going to learn more of them. And later on, we will learn even more…

But for now, just check an example of double if-statements again:

#include <iostream>
using namespace std;

int main()
{
  int a = 3;
 
  if(a < 10)
    cout << "a is smaller" << endl;
  a = 15;
  if(a > 10)
    cout << "a is bigger" << endl;
  a = 5;
  cout << a << endl;
 
  return 0;
}

Everything looks fine in the above code and also compiles fine. It runs fine as well. But the problem is that I don’t intend to do that! I don’t want this output. I want it to do something like this:

If a is smaller than 10, print “a is smaller” and set a equals to 15 else if a is bigger than 10, print “a is bigger” and set a equals to 5. At last, print a which should be equal to 15 or 5.

The output of the above code is:

a is smaller
a is bigger
5

What I think or intend is:

a is smaller
15

Our goal is to understand how to get the output that we intended even for such a simple case. You may have figured out some mistakes. Following are some of them:

  • There are two lines which I intend to execute when respective condition passes. So, we must use block(curly brackets) for both if-statements.
  • I want to use else-statement.

Now, after the correction applied, it may looks like:

if(a < 10)
{
 cout << "a is smaller" << endl;
 a = 15;
}
else
{
 cout << "a is bigger" << endl;
 a = 5;
}
cout << a << endl;

Yeah! We fixed it! But hey, we removed the condition entirely! Where is a > 10? This is another mistake which can be solved like this:

if(a < 10)
{
 cout << "a is smaller" << endl;
 a = 15;
}
else
{
 if(a > 10)
 {
  cout << "a is bigger" << endl;
  a = 5;
 }
}
cout << a << endl;

The question is that, why we need to have that condition? Well, what if a is equal to 10? If it is, it should not pass both of the conditions. But if we use else only, this is no more true. Therefore, it is necessary to have if statement in else statement’s block with the appropriate condition.

From this question, we get another one which is more like a suggestion. What if we want to output something for each and every case? If a is smaller than 10 then do something else if a is bigger than 10, do something, else do something. Ofcourse, the second and last else-statement is only for value of a equal to 10.

Here’s the code: (Remember, else-statement can only be putted with an if-statement, not another else-statement)

if(a < 10)
{
 cout << "a is smaller" << endl;
 a = 15;
}
else
{
 if(a > 10)
 {
  cout << "a is bigger" << endl;
  a = 5;
 }
 else
  cout << "a is ten" << endl;
}
cout << a << endl;

You may have noticed that with the other else-statement, block is not used as the code is single line. It is quite confusing and don’t looks easy. Well, here’s an easy way. We can use else-if-statement.

if(a < 10)
{
 cout << "a is smaller" << endl;
 a = 15;
}
else if(a > 10)
{
 cout << "a is bigger" << endl;
 a = 5;
}
else
 cout << "a is ten" << endl;

cout << a << endl;

Both of the codes are equivalent. Both gives same output for all cases. But with else-if-statement, it’s better. You can use else and else-if-statement, after an else-if-statement.

You see! It is slowly getting complex. But don’t worry! If you read this and then, try to code things right, you are doing all well!