C Decision making

Decision making are play important role in programming. there are also known as flow control of program. Because they are capable to change the flow execution of instruction.

Decision making

Decision making statements

Following type of decision making statements.

1) if statements
2) if else statements
3) switch case statements

If statements

This statement are work only when given expression are not null or zero. that are accept boolean expression (conditional statements). if there are valid then executing block of statements.

 if(expression){
  //code-statements
  }

Flowchart

if statement flow chart
/* 
  Example of if statements
*/
/*Header file*/
#include <stdio.h>
int main(){
  
  if(10>5){
    printf("10 is greater than 5\n");
  }
  if(0){
    printf("This is 0\n");
  }
  // expression 100<200 && 200 >150
  if(100<200 && 200 >150){
    printf("100 is less then 200 and 200 id greater than 150\n");
  }
  
}

Note that in this program if we are passing 0 to (if statement) then it will not execute.

Output

10 is greater than 5
100 is less then 200 and 200 id greater than 150

Nested if

using of one statement inside another statement, this process is called nested statement. there is no limit to any number of nested statement in c.

Syntax of nested if

if(expression){
  //code
  if(condition){
    //code
    if(condition){
      //code
    }
  }

}

There are declare three nested loop.

Example :

/* 
  Example of Nested if statements
*/
/*Header file*/
#include <stdio.h>
int main(){
  int value=10;
  if(value>5){
    printf("10>5 are true\n");

    if(value==11){
      printf("11==11 are true\n");
    }
    if(value==10){
      printf("10==10 are true\n");
    }
  }
  
}
10>5 are true
10==10 are true

If Else statements

If statement after we are use else statement. if statement are false in case. then it will work on else part statements.

if else statement flow chart

Syntax

 
if(condition){
  //code
}else{
  //code
}

Example

/* 
  Example of if else statements
*/
/*Header file*/
#include <stdio.h>
int main(){
  int status=7;

  if(status<0){
    //if statemnts 
    printf("Negative value");
  }else{
    //else statements
    printf("Positive value");
  }
  
}

Output

Positive value

switch case

switch case are suitable for when we are check multiple condition on single variable. for example suppose we are have few condition then suitable for (if else or else if) statements. but if multiple test cases. then better to use of switch statements.There are faster or( if else) and easier to read and understand.

switch case flow chart

Syntax

switch(expression){
  case constant-condition1:
    //statements
  break;
  case constant-condition2:
    //statements
  break;
  case constant-condition3:
    //statements
  break;
  //cases
  ..
  ..
  ..
  ..
  ..
  ..
  default:
  //statements
}

Example

/* 
  Example of switch case
*/
/*Header file*/
#include <stdio.h>
int message();
//user message
int message(){
  int value;
  printf("\n Choose Your Best Programming Language :");
  printf("\n1 : C Programming");
  printf("\n2 : C++ Programming");
  printf("\n3 : Java");
  printf("\n4 : Python");
  printf("\n Enter Choose :");
  scanf("%d",&value);
  return value;
}
int main(){
  int status;
  status=message();
  //switch 
  switch(status){
    case 1:
      printf("\nYou are choose to learn c Programming");  
    break;
    case 2:
      printf("\nYou are choose to learn c++ Programming");  
    break;
    case 3:
      printf("\nYou are choose to learn java Programming");
    break;
    case 4:
      printf("\nYou are choose to learn python Programming");
    break;
    default:
    printf("\n Wrong input choose (1-4) Try again..");
  }

}

Output

Choose Your Best Programming Language :
1 : C Programming
2 : C++ Programming
3 : Java
4 : Python
 Enter Choose :1

You are choose to learn c Programming

Following points are important to switch case

1) default is optional in switch statement. they are executed when not satisfied given switch case expression.

/* 
  Example of switch case
*/
/*Header file*/
#include <stdio.h>

int main(){

  int number=1;
  //switch without default
  switch(number){
    case 2:
      printf("Two\n" );
    break;
    case 3:
    printf("Tree\n");
    break;
    //not use default
  }
  //switch with default
  switch(number){
    case 4:
      printf("Four\n" );
    break;
    case 5:
    printf("five\n");
    break;
    //use default
    default:
    printf("Missing test case \n");
  }
}

We are not using default statement in first switch case. in this program not satisfied given test case expression. and here not defined default statements. so they are not produce any information or result. And second switch case use in default statement. In given program not stratified given test expression. then this are execute default statements.

Output

Missing test case

2) In case break statement are not found. then compiler will be execute another switch case until not found break statements.

/* 
  Example of switch case
*/
/*Header file*/
#include <stdio.h>

int main(){

  int number=1;
  //switch statements
  switch(number){
    case 1:
      printf("One\n");
    case 2:
      printf("Two\n" );
    case 3:
      printf("Tree\n");
    printf("Invalid statements\n");
  } 
}

Note that In this program we are not give break statements to every switch case. and test expression are match in first case. so they are execute all test case in switch statement including default statements also.

Output

One
Two
Tree
Invalid statements

3) We are use any numbers of nested switch in c programming. Syntax of nested switch.

switch(expression1){
  case constant-condition1:
    //statements
    switch(expression2){
      case constant-expression1 :
      break;
      case constant-expression2:
      break;
      default:
      //statements
    }
  break;
  case constant-condition2:
    //statements
  break;
  default:
  //default statements

}

For example

/* 
  Example of nested switch case
*/
/*Header file*/
#include <stdio.h>

int main(){
  int num1=10;
  int num2=20;
  //outer switch
  switch(num1){
    case 10:
      printf("\n Outer switch execute num1 : %d",num1);
      //inner switch
      switch(num2){
        case 20 :
        printf("\n Inner switch execute num2 : %d",num2);
        break;
        default:
        printf("\n default statements of inner switch");
    }
      break;
    default:
    printf("\n default statements of outer switch");
  }

  
}

Output

 Outer switch execute num1 : 10
 Inner switch execute num2 : 20

© 2019, regularcodes.com, All rights reserved