Tuesday, 26 March 2019

Decision making in C (If else,nested if)


There comes situation when we need to make some decisions based on certain conditions and based on these decision we will perform a particular job.

Decision making statements in programming languages decides the direction of flow of program execution.
Following decision making constructs are available in C:
1.      If statements
2.      If Else statements
3.      Switch Case

In this article, you will learn about if statements, if else statements and nested ifs.

1. if statement
if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition){
  //body of if ; it will be executed if condition in true
}
Example:
#include<stdio.h>
int main(){
 int num=100;
if(num<150){
  printf(“%d is smaller than 150.”);
  }
return 0;
}

2.if else statement
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if(condition){
  //body of if; it will be executed when condition is true
}
else{
  //body of else; it will be executed only when condition is false
}
Example:
//Program to check whether an integer is positive or negative
#include<stdio.h>
int main(){
   int num;
   printf(“Enter an integer value except 0”);
scanf(“%d”,&num);

  if(num>0){
    printf(“%d is a positive integer.”);
  }
  else{
    printf(“%d is a negative integer.”);
   }

return 0;
}

If else ..if else..else statement(if else ladder)
When you have multiple options to check, if else ladder comes into picture.
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:
if(condition 1){

  }
else if(condition 2){

 }
else if(condition 3){

  }
.
.
.
else if(condition n){

}
.
.
.
else{

}
Example:
#include<stdio.h>
int  main()
{
    int num = 14;
    if (num == 10)
         printf(“%d is equal to 10”,num);
    else if (num == 14)
        printf(“%d is equal to 10”,num);
    else if (num == 20)
        printf(“%d is equal to 10”,num);
    else
        printf(“%d is not present",num);
return 0;
}

Nested if statements
Nested if statements means an if statement inside another if statement.
Syntax:
if(condition 1){
   if(condition 2){

    }
  else{

  }
 }
else{


   }
Example
//Program to find smaller number between two numbers
#include<stdio.h>
int main(){
  int num1,num2;
  printf(“Enter two integer numbers\n”);
  scanf(“%d %d”,&num1,&num2);

  if(num1!=num2){
    if(num1<num2)
      printf(“%d is smaller than %d.”,num1,num2);
    else
      printf(“%d is smaller than %d.”,num2,num1);
    }
  else
     printf(“Both numbers are equal”);

 return 0;
}

Please comment if you find anything incorrect, or you want to improve the topic discussed above

No comments:

Post a Comment