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!

Advertisements

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

Conditional statements in a program are such statements which controls the progression of our program. We must understand the fact that our code is always executed from the start to the end linearly.

Following is a conditional statement:

#include <iostream>
using namespace std;
int main()
{
 int a = 5;
 
 if(a < 10)
 {
  cout << "a is smaller." << endl;
 }
 
 return 0;
}

Here, if shows the condition that is typed after it in round brackets. In the curly brackets or the block just after the if statement, there is the code which will be executed only if the condition is true.

> (bigger than), >= (bigger than or equal to), == (equals to). The last one i.e == is quite mismatched with =. == checks the equality between two things whereas = copies the value of the right hand side object to the left hand side object.

It is not necessary to use a block below the if-statement. If there is only one line of code, then just have it. This is true for all kinds of conditional statements. For example, for the above if statement, you can also type:

if(a < 10)
 cout << "a is smaller." << endl;

You may have noticed the spaces that are left at the start of a statement in a block or after the conditional statements. This is called formatting. We need to format the code so that it is more readable. The spaces or tabs indicates that in which block a particular line of code is present. Of course, it will be helpful in future when we make some medium size projects.

Now, lets see a better conditional statement. Instead of the previous if-statement, we can also type in:

if(a < 10)
 cout << "a is smaller." << endl;
else
 cout << "a is bigger." << endl;

With a equals to 5, we can see no change in the output. But if a is, lets say, 15, it will execute the line of code below the else. You can read it as:

If a is smaller than 10, print “a is smaller” else print “a is bigger”.

The else-statement is always typed with an if-statement. Lets type in another if-statement.

if(a < 10)
 cout << "a is smaller." << endl;
if(!(a < 10))
 cout << "a is bigger." << endl;

The ! (or NOT) operator is the opposite of the conditional state. For example, if a is not smaller than 10, it is false. But putting NOT operator at the back of it makes it true. To check whether two things are not equal to each other, we use != which is another comparing operator.

For the else-statement that we typed, its condition is quite similar to !(a < 10). Now, you may have understood the else-statement.

Don’t try to understand it or go deep in the code; just type the code and make it run!