' if ' Statements

 ' if ' Statements

    'if' is a keyword used in C++ language to implement decision making instructions. The syntax or the general form in which if statements are written as : -

if(our condition)
    command to be executed;

     Here the condition can be given using various methods but the most common of all are relational operators. These operators allow us to compare values and decide whether situation is true or not. A fact that we should know is "Computer understands only binary i.e. 1 or 0".And that is the reason these relational operators always evaluates to either 1 or 0.1 represents TRUE and 0 represents FALSE.
Confused ?? Where these 1 and 0,TRUE and FALSE came from? Are we diverted from our topic? No. Because 'if' keyword operates only on 1 and 0. We will consider this fact very soon by making a program but first take a look at the following table. It shows us the evaluation of relational operators under different conditions.

    Let us now take a look at a program and see how these concepts work. If you don't understand the program then just don't worry about it because I've given an explanation
right after finishing it. We'll be using wx-Dev C++ 7.4.2 to run our programs. You can download this IDE from here.

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                                      //main function
{
    system("CLS");                        //clears any printed, if any
    int m,n,p;
    cout<<"\nEnter value of n : ";
    cin>>n;
    cout<<"\nEnter value of m : ";
    cin>>m;
    if(n<m)
        cout<<"\nn is small.";
    if(n>=m)
        cout<<"\nn is equal to or greater than m.";
    system("PAUSE");                   // Freeze the screen to show the output
    return 0;
}
    Since you have read the program(I assume so...), its time for us to grab the concept of it. You remember those 1 and 0. You also read that 'if' only operates on 0 and 1. Here's the explanation for this concept. Consider the input provided to n is 8 and that to m is 20. By looking in the table we can easily figure out that the expression 'n<m' will evaluate to 1. That means after evaluation computer will read 'if(8<20)' as 'if(1)' because 8<20 evaluated to 1. I also told you that 1 represent TRUE. So the condition after 'if(n<m)' will be executed. As a result of which 'n is small' will be printed on console(screen).  
    Now consider 2nd 'if' statement i.e. 'if(n>=m)'. Our table tells us that 'if(8>=20)' will evaluate to 'if(0)'. Since 0 represents FALSE, this 'if' will not be executed. Also remember the fact that any number other than 0 is always considered as 1. Let us take a program to understand it.

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                             //main function
{
    system("CLS");                     //clears any printed, if any
    int condition;
    cout<<"\nEnter value of condition : ";
    cin>>condition;
    if(condition)        // 1st if
        cout<<"\nYour condition evaluates to TRUE.";
    if(!condition)        // 2nd if
        cout<<"\nYour condition evaluates to FALSE.";
    system("PAUSE");                  // Freeze the screen to show the output
    return 0;
}
    Oops!! Trouble?? So here's its solution. Consider that we input 1 to condition. So clearly our 1st 'if' i.e. if(condition) evaluates to 'if(1)' and hence 'Your condition evaluates to TRUE' is printed on screen. In 2nd 'if', '!' operator is used. This operator is called logical NOT operator. What it does is to convert TRUE to FALSE and vise versa. So 'if(!condition)' i.e. 'if(!1)' evaluates to 'if(0)' and cout is not executed. Exactly opposite happens in the case when 0 is given as input.
    But, what happens when we give inputs other than 1 and 0? In that case, since its a non zero number, computer always assumes it to be TRUE. Suppose we provide
input, say 2609, then 1st 'if' i.e. 'if(condition)' or 'if(2609)' evaluates to 'if(TRUE)' and cout statement is executed. Same way as explained above, our 2nd 'if'
evaluates to 'if(!TRUE)' or 'if(FALSE)'.

    So these were some of the basic knowledge we need to know to understand how this 'if' stuff works. Now its about time that we should proceed to our next topic i.e. 'if-else pair'.
Written by T-REX

<<Decision Making                                 Home                                  if-else Statements>>

No comments:

Post a Comment