C Decision and Control

Decision Making structures allows a program to take a course when certain conditions occur. C programming allows decision making through if statement, switch-case statements. These statements can be nested to form more complex structures.

if Statement

if(condition) executes when the condition is true.

Code

#include<stdio.h>
int main(void)
{
    int var = 1;
    
    if(var == 1)
    {
        printf("Value of var = %d\n",var);
    }
    return 0;
}

Output

Value of var = 1


if else-if else Chain

if(condition) executes when the condition is true, else if(condition) executes when the if(condition) fails and else if(condition) returns true. Else condition is executed when none of the if's have executed

Code

#include<stdio.h>
int main(void)
{
    int var = 3;
    
    if(var == 1)
    {
        printf("Value of var = %d\n",var);
    }
    else if(var == 2)
    {
        printf("Value of var = %d\n",var);    
    }
    else
    {
        printf("Not expected\n");    
    }
    
    return 0;
}

Output

Not Expected


Switch-Case Statements

Switch case statements are used to run a block of code when the case statement is satisfied. Every case statement is ended with a break. If the break is not applied then the next following case will get executed regardless of being false. A default case allows a block of code to run if no condition is satisfied (similar to else statement in if-else).

Code

#include<stdio.h>
int main(void)
{
    int var = 2;
    
    switch(var)
    {
        case 1:
          printf("Value of var = %d\n",var);
          break;
        
        case 2:
          printf("Value of var = %d\n",var);
          break;
        
        default:
          printf("Not expected\n");
          break;    //Can be avoided in default
    }
    
    return 0;
}

Output

Value of var = 2

Limitations of Switch Statements

  • Cannot use floating point constants.
  • Cannot use variable expressions.
  • Cannot use same constant in two or more cases.
  • Cannot use Relational expressions in case.

Messy Code

#include<stdio.h>
int main(void)
{
    char var = '*';
    int a = 15;
    int b = 10;
    
    switch(var)
    {
        case '*':
          printf("Multiplication: %d\n",a*b);
          //break missing
        
        case '+':
          printf("Addition: %d\n",a+b);
          break;
          
        case '-':
          printf("Subtraction: %d\n",a-b);
          break;
        
        default:
          printf("Not expected\n");
    }
    
    return 0;
}

Output

Multiplication: 150
Addition: 25

Nested if and switch Statements

Nesting of if or switch statements allows programmers to write complex code structures which requires multiple condition checks. Nested ifs is similar to joining two conditions inside a single if statement with an AND operator.

Nested if example

#include<stdio.h>
int main(void)
{
    int lucky,age;
    printf("Enter your lucky Number: ");
    scanf("%d",&lucky);
    printf("Enter your age: ");
    scanf("%d",&age);
    
    if((lucky%2) == 0 && age > 50)
    {
        if((age%2) == 0)
            printf("You love cats\n");
        else
            printf("You love dogs\n");
    }
    else
    {
        printf("You love both cats and dogs\n");
    }
    
    return 0;
    
}

Output

Enter your lucky Number: 6
Enter your age: 54
You love cats

Nested switch example

#include<stdio.h>
int main(void)
{
    int satisfaction;
    char recommend;
    
    printf("Rate your satisfaction on the scale of 1-5 (integer): ");
    scanf("%d",&satisfaction);
    
    switch(satisfaction)
    {
        case 1:
          printf("Terrible\n");
          break;
          
        case 2:
          printf("Bad\n");
          break;
          
        case 3:
          printf("Not Bad\n");
          break;
          
        case 4:
          printf("Nice\n");
          break;
          
        case 5:
          printf("Amazing\n");
          printf("Do you want to recommend this product? : ");
          scanf(" %c",&recommend);    
          //A space before %c is required to skip \n from the previous input
          switch(recommend)
          {
              case 'y':
                printf("Thank you\n");
                break;
              case 'n':
                printf("We respect your choice\n");
                break;
              default:
                printf("Could not rate\n");
          }
          break;
          
        default:
          printf("Could not rate\n");
    }
    return 0;  
}

Output

Rate your satisfaction on the scale of 1-5 (integer): 5
Amazing
Do you want to recommend this product? : y
Thank you

Trending

C Programming

Remember to follow community guidelines and don't go off topic.