if, if....else Statement in C programming - Ocean of Programming

Sunday 23 July 2017

if, if....else Statement in C programming

        In the previous article we have seen How to Download and Install Code::Blocks and How To Create and Run Project In Code::Blocks if you did not read the previous article you read it.        
          The if, if....else and nested if ... else statements are used for decision purpose. In simple word you can say if this condition satisfies then execute this otherwise execute other. E.g If we entered one integer and we want to find it is even number or odd number. This decision can be made using if as well as if...else statement. if statement needs two condition as num%2==0 and num%2==1. If first condition is satisfies it returns num%2==0 statement otherwise it returns n%2==1 statement. In if .... else statement we need only one condition num%2==0. If even number is provided then it returns the first statement otherwise it returns the second statement. In the case of nested if ... else statement there are multiple statements are checked and returns satisfied condition. Let us see each one by one.

 if Statement

         The if statement is decision making statement used to control the flow of execution of a statement. If the condition whatever it is, statement execute the body of if, if and only if the condition is true. If the condition is not true, then the statement is not executed and the computer goes directly to the next statement. 
The general form of if statement is
if(test condition)
{
 body of if statement;
}
        The test condition must be always enclosed within the pair of brackets( ). The test condition should not end with semicolon it indicates that end condition it comes out of the loop without further checking condition. Let us understand the working of if statement with the example.
#include<stdio.h>
int main()
{
        int x=50,y=70;
        if(x<y)
   {
      printf("y is greater than x\n");
   }
} 
The output for the above program is
       In the above program, it prints "y is greater than x" because the condition is true.See another program.
#include<stdio.h>
int main()
{
        int x=50,y=70;
   if(x>y);
   {
      printf("y is greater than x\n");
   }
} 
The output for the above program is
      In the image, you can see that the output for both programs is the same. But there is a difference in both program output. In the first statement, the printf statement is executed because the condition is true. In the second program, the condition is false still it is printing statement. This happened because we provided semicolon at the end of if statement. Due to semicolon if statement comes out of the loop without checking condition and printf statement print like a simple print statement. There is no relation with if the condition is true or false.

if ... else Statement

        Similar to if statement, if .. else is also a decision making statement. if...else statement has both true and false condition. If the condition is true then if part is executed otherwise else part is executed.

The general form of if .. else statement is

#include<stdio.h>
int main()
{
        if(test condition)
        {
                body of if statement
        }
        else
        {
                body of else statement
        }

}
         The condition of if part is true then if part is executed and else part is skipped and if the else part is executed when the if condition is skipped. Let us see the following program for better understanding.
#include<stdio.h>
int main()
{
        int x=10,y=20;
        if(x<y)
        {
                printf("x is less than y\n");
        }
        else
        {
                printf("y is less than x\n");
        }
}
The output for the above program is
       
         In the above program the condition of if statement is true so if part is executed and the else part is skipped. For non zero value if part is executed and for zero else part is executed. Let us consider the following example for better understanding.
#include<stdio.h>
int main()
{
        if(1)
        {
                printf("This is if part\n");
        }
        else
        {
                printf("This is else part\n");
        }

}
The output for above program is
       In above program, if condition contains 1 that is non-zero value so the if part is executed and else part is skipped. Let us consider if condition contains zero value.
#include<stdio.h>
int main()
{
        if(0)
        {
                printf("This is if part\n");
        }
        else
        {
                printf("This is else part\n");
        }

}
The output for above program is
          In above image you can see that for if condition zero the else part is executed. This is important to know when if part is executed and when else part is executed. There are some chances of asking a question in C aptitude test.
        Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.

No comments:

Post a Comment