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!

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