Ad Code

Responsive Advertisement

Decision making statement in C

 A decision-Making statement like If else and switch case.

if-else statement :

Decision-making specified conditions when we write a program. When we apply any logic and if they have two or more possibilities in it, So what should we do in this situation. To solve such type of situation we use the If else statement.

Let's understand with an example -
Suppose, we have to check given number is even or odd, we have to handle two situations.
the number is even or odd.
e.g -
Condition ( number%2 == 0 )
print the number is even.

or ( condition is wrong )
print the number is odd.

Flowchart of given example -



I am pretty sure you understand this flowchart 😊

Source code :
#include <stdio.h>   // standard input output header file
int main() {        
    int num = 10;   // Number is 5 body of else part will executed
    if(num%2==0)   // Condition
    {
        printf("It is  an even number\n");
    }
      else
      {
        printf("It is  an odd number\n");
      }
    return 0;
}


Output :
It is an even number


Switch case statement :

In the switch case statement, we learn how to handle multiple cases/operations in code.

How does the switch statement work?

  • If we have to execute several cases, we have to put that cases in switch case syntax.
  • One by one case will be executed, for example, if the given expression is equal to case 1 it will execute until a break statement found.
  • if the given expression is not matched default statement will be executed.
Note :
  • break statement is used to terminate the switch cases, which avoids the execution of other cases in the switch statement.
  • default statement is used to handle the exception ( it will be executed when all expression are false )
Let's understand the flowchart - 



 Syntax -
switch (expression)

        { 

               case 1:

       //statement 

      break;

          

      case 2:

                //statement

      break;
      .
      .
      .
      default :

      //default statement

    }

 Example :

Source code :
#include <stdio.h>

int main() {
    int a,b,ch;
    printf("Enter the two number\n");
    scanf("%d %d",&a,&b);
    printf("Enter the choice\n");
    printf("1.addition\n 2.subtraction\n 3.multiplication\n 4.Division\n");
    scanf("%d",&ch);
    
    switch(ch){
        
        case 1 :
        printf("Sum of %d and %d is %d\n",a,b,a+b);
        break;
        
        case 2 :
        printf("Subtraction %d and %d is %d\n",a,b,a-b);
        break;
        
        case 3 :
        printf("Multiplication %d and %d is %d\n",a,b,a*b);
        break;
        
        case 4 :
        printf("Division of %d and %d is %d\n",a,b,a/b);
        break;
        
        default:
        printf("Wrong choice\n");
    }
    
    return 0;
}

Output :
Enter the two number
4
6
Enter the choice
 1.addition
 2.subtraction
 3.multiplication
 4.Division
2
Subtraction 4 and 6 is -2

So that's all about Decision Making statement, I hope you all understand very well.
If you have any doubt, please let me know Thank you .....

Post a Comment

0 Comments

Ad Code

Responsive Advertisement