SEARCH THIS BLOG

if-else STATEMENT IN C/C++

An if-else statement in C/C++ is needed whenever the programmer needs to make a decision. This decision is fed into the if-else statement and the flow of control is regulated depending on the result of the decision. All decisions in programming language result in only two possibilities, either the condition is true or it is false. If-else statement deals with this scenario.

 

The if-else statement in C/C++

 

An if statement tests a particular condition; if the condition is true, a course-of-action is followed, i.e. a particular statement or set of statements is executed. Otherwise (if the condition evaluates to false), the course-of-action is ignored.

General syntax for an if statement is :

if(expression)

statement;

where a statement may consist of a single statement, a compound statement, or even an empty (or NULL) statement. The expression must be enclosed within parenthesis(). If the expression evaluates to true, the statement is executed, otherwise ignored. For instance,

The code fragment

         if(ch== ‘ ’)

          spaces ++;

checks whether variable ch stores a space or not. If it does, the value of spaces is incremented by 1.


Let’s look at one example :

int A,B,C;

cin>>A>>B>>C;

if(A > 10 && B < 15)

{

  C= A+B;

  cout<< “The result is ”<<C;

}

The above code uses a block of statements. Also, look at the expression used. Here two relational conditions are joined by logical AND (&&).

 

The expression part is an important aspect of if-else statement. We know all conditions result in either true or false. The compiler takes 0 (zero) as false and 1 (or any other integer other than 0) as true. So now our expression may take the following forms :

1. if(n)

here n can be any number entered by the user or simply a constant. If n is not zero, the expression is taken as true. For example ,

if(23)

statement;

This expression is true and statement will be executed.

 

2. if(n>10)

Here, expression is a single relational expression which is true if n is greater than 10 and false if n is less than or equal to 10.

 

3. if(n>10 && n<20)

Here, expression is a combination of two relational expressions joined by logical AND. So, if n is greater than 10 and it is less than 20 simultaneously, then only the expression will be true, otherwise it will be false.

 

4. if(n>10, n>20, n<15)

Here, expression is combination of relational expressions separated by comma operator. As we know expressions separated by comma are evaluated from left to right and the value of entire expression is the value of rightmost expression. Thus (n>10,n>20,n<15) is actually equivalent to only (n<15). So, if n is less than 15 then the condition as a whole will be true.


What if there is another course of action to be followed if the expression evaluates to false? There is another form of if that allows for this kind of either-or condition by providing an else clause. The syntax of the if-else statement is the following :

if (expression) 

   statement 1;

else

   statement 2;

If the expression evaluates to true, the statement 1 is executed, otherwise, statement 2 is executed. statement 1 and statement 2 can be single statements, block of statements or empty statements.

The above code can be represented in a flowchart as :

if-else statement in C/C++


Now look at a program in C++ to find greater number among the two.

//program to demonstrate working of if-else

//Find greater of two numbers.

#include<iostream>

using namespace std;

int main()

{

   int a,b;

   cin>>a>>b;

   if(a > b)

       cout<< “Greater number is ” <<a;

   else

       cout<< “Greater number is ”<<b;

   return 0;

}

Input : 10 20

Output : Greater number is 20

 

Nested if

 

A nested if is an if that has another if in its if’s body or in its else’s body. The nested ifs can have 3 different forms :

Form 1 : if nested inside if-part

if (expression 1)

{

     if(expression 2)

         statement 1;

     else

         statement 2;

}

else

       statement 3;

//rest of program

 

Explanation : If expression 1 is true, the control moves into the if block and checks expression 2. If it is true then statement 1 is executed and control moves to rest of program. If expression 2 is false, statement 2 is executed and control moves to rest of program. If the expression 1 is false, the control doesn’t enter the if and executes statement 3 and then moves to rest of program.

 

Form 2 : if nested inside else-part

if(expression 1)

   statement 1;

else

{

    if(expression 2)

          statement 2;

    else

         statement 3;

}

 

Form 3 : if nested inside both if-part and else-part

 

if(expression 1)

{

    if(expression 2)

          statement 1;

    else

          statement 2;

}

else

{

     if(expression 3)

          statement 3;

     else

          statement 4;

}

 

Note: All else statements are always optional.

 

Well let us take a test.

#include<iostream>

using namespace std;

int main()

{

int n; cin>>n;

if(n % 2==0)

    {

      if (n % 5==0)

         cout<< “Yes”;

      else

         cout<< “Part yes”;

     }

else

      cout<< “No”;

return 0;

}

 

Assume inputs from your side and comment down the output.

 

The if-else-if Ladder

 

A common programming construct in C++ is the if-else-if ladder, which is often also called the if-else-if staircase because of its appearance.

General form :

if (expression 1) statement 1;

  else

    if(expression 2) statement 2;

       else

          if(expression 3) statement 3;

               ……

                   else statement 4;

 

Although it is technically correct to have indentation in the if-else-if ladder as shown above. But it generally leads to overly deep indentation. For this reason, the if-else-if ladder is generally indented like :

if(expression 1)

    statement 1;

else if (expression 2)

    statement 2;

else if (expression 3)

    statement 3;

……..

else

    statement 4;

 

Few important points about if-else-if ladder :

1.     if statement must be present; and it should be before all else if and else statements.

2.    Any number of else-ifs can be used but they should all be placed between the if and the else statement (if present).

3.    else is not always needed but if present, it should be only one in number and after all else-ifs.

4.    Once the control enters any of the statements (be it if or any of the else-ifs), it ignores all the following statements and control comes out of if-else-if ladder.

 

 

A common Mistake :

While comparing for equality, make sure to use the relational equality operator (==) and not the assignment operator (=).

if(p=4) is always TRUE statement. To check if value in p is equal to 4, we need to use if(p==4).

p=4 actually assigns 4 to p and all assignments other than 0 (zero) are regarded as true. So, if(p=0) will be always false and any other assignment to p will be always true. So, always keep in mind to use the relational operator (==) and not assignment operator (=).

 

 

 

 

 

Did you find the article helpful? Do you want to add something to it? Feel free to comment on this post below.

Do not stop here. To read all about C/C++ click here.

To download pdf format of this article, click here.

If you want to contribute any article or any piece of information you can send it on coding.nkcoder@gmail.com. We will publish it in your name.

 

 

 


Post a Comment