if,if-else and nested if-else statement in c

if, if..else and Nested if…else Statement

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. In C, decision making can be performed with following two statements.

  1. if…else statement
  2. switch statement

if statement syntax

if (test expression){
       statement/s to be executed if test expression is true;
}

If the test expression is true then, statements for the body if, i.e, statements inside parenthesis are executed. But, if the test expression is false, the execution of the statements for the body of if statements are skipped.

Flowchart of if statement

 

Example of if statement

Write a C program to print the number entered by user only if the number entered is negative.


#include <stdio.h>
      int main(){
      int num;
      printf("Enter a number to check.\n");
      scanf("%d",&num);
      if(num<0)       /* checking whether number is less than 0 or not. */ 
            printf("Number=%d\n",num);   
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
      printf("The if statement in C programming is easy.");
return 0;
}

Output 1

Enter a number to check.
-2
Number=-2
The if statement in C programming is easy.

When user enters -2 then, the test expression (num<0) becomes true. Hence,Number=-2 is displayed in the screen.

Output 2

Enter a number to check.
5
The if statement in C programming is easy.

When the user enters 5 then, the test expression (num<0) becomes false. So, the statement for body of if is skipped and only the statement below it is executed.

if…else statement

The if…else statement is used, if the programmer wants to execute some code, if the test expression is true and execute some other code if the test expression is false.

Syntax of if…else

if (test expression)
     statements to be executed if test expression is true;
else
     statements to be executed if test expression is false;

Flowchart of if…else statement

Example of if…else statement

Write a C program to check whether a number entered by user is even or odd

#include <stdio.h>
int main(){
      int num;
      printf("Enter a number you want to check.\n");
      scanf("%d",&num);
      if((num%2)==0)          //checking whether remainder is 0 or not.
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}

Output 1

Enter a number you want to check.
25
25 is odd.

Output 2

Enter a number you want to check.
2
2 is even.

Nested if…else statement (if…elseif….else Statement)

The if…else statement can be used in nested form when a serious decision are involved.

Syntax of nested if…else statement.

if (test expression)
     statements to be executed if test expression is true;
else
     if(test expression 1)
          statements to be executed if test expressions 1 is true;
       else 
          if (test expression 2)
           .
           .
           .
            else
              statements to be executed if all test expressions are false;

How nested if…else works?

If the test expression is true, it will execute the code before else part but, if it is false, the control of the program jumps to the else part and check test expression 1 and the process continues. If all the test expression are false then, the last statement is executed.

The ANSI standard specifies that 15 levels of nesting may be continued.

Example of nested if else statement

Write a C program to relate two integers entered by user using = or > or < sign.

#include <stdio.h>
int main(){ 
     int numb1, numb2;
     printf("Enter two integers to check".\n);
     scanf("%d %d",&numb1,&numb2); 
     if(numb1==numb2) //checking whether two integers are equal.
          printf("Result: %d=%d",numb1,numb2); 
     else 
        if(numb1>numb2) //checking whether numb1 is greater than numb2. 
          printf("Result: %d>%d",numb1,numb2); 
        else 
          printf("Result: %d>%d",numb2,numb1); 
return 0; 
} 

Output 1

Enter two integers to check.
5
3
Result: 5>3

Output 2

Enter two integers to check.
-4
-4
Result: -4=-4
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