Conditional statements in C Programming.
Tags: if, switch, programming, cprogramming
Published: 2023-12-14 10:41:54

## if Statement
The if statement in C is a fundamental control structure used to make decisions and execute different blocks of code based on those decisions. It acts like a gatekeeper, letting code through only if a specific condition is met.
```
#include <stdio.h>

int main() {
    
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    
    if(age >= 18){
        printf("You are eligible to vote");
    }
    if(age < 18){
        printf("You are not eligible to vote!");
    }

    return 0;
}
```
if body of if block only has one statement, then we can omit the curly braces. Take a look at the example below.
```
if(age>18)
    printf("You are eligibler to vote");
```

## if/else Statement
This provides additional options beyond the if statement. It allows you to execute one block of code if the condition is true and another block if the condition is false.
```
if (condition) {
  // block of code to execute if the condition is true
} else {
  // block of code to execute if the condition is false
}
```

## if/else if Statement
In C programming, the if/else if statement is used to create a series of conditional statements in a more structured way than using multiple if statements. It allows you to check multiple conditions sequentially, and the code inside the block associated with the first true condition will be executed. If none of the conditions is true, the code inside the else block (if present) will be executed.
```
int main() {
    int age = -16;
    
    if(age > 120 || age < 0){
        printf("invalid age");
    }else if(age >= 18){
        printf("You are eligible to vote");
    }else {
        printf("You are not eligible to vote!");
    }

    return 0;
}
```

## Ternary operator(Conditional operator)
The ternary operator, also known as the conditional operator, is a shorthand way of writing an if-else statement in C. It provides a concise syntax for making decisions based on a condition. The basic syntax of the ternary operator is:
```
test_condition ? expression1 : expression2;
```

Example:
```
int main() {
    
   char operator = '-';
   
   int num1 = 8;
   int num2 = 7;
   
   int result = (operator == '+') ? (num1 + num2) : (num1 - num2);
   printf("%d", result);

    return 0;
}
```
Note: If there are multiple lines of code then we should never replace if else with ternary operator.

## switch Statement
Switch statement is used as an alternative of if/else statement.In switch statement the value of variable or expression is compared with the value of individual cases.If variable or expression is equal to value of a case then that case will be executed. And if noting executed the body of default is executed.
```
switch(variable/expression){
    case value1:
        //body of case 1
        break;
    
    case value2:
        //body of case 2
        break;
    
    case value3:
        //body of case 3
        break;
    
    default:
        //body of case 1         
}
```

Note: if we do not use break; statement then will output
correct result along with executing all other cases after that result.
```
#include <stdio.h>

int main() {
    
   int number;
   
   printf("Enter a number between 1 to 7:");
   scanf("%d", &number);
   
   switch(number){
       case 1:
       printf("Sunday");
       break;
       
       case 2:
       printf("Monday");
       break;
       
       case 3:
       printf("Tuesday");
       break;
       
       case 4:
       printf("Wednesday");
       break;
       
       case 5:
       printf("Thursday");
       break;
       
       case 6:
       printf("Friday");
       break;
       
       case 7:
       printf("Saturday");
       break;
      
       default:
       printf("Invalid number");
   }
    return 0;
}
```

**Switch with multiple cases**
There might be situation where we want to execute multiple cases together. In such situation we can omit break statement.
Example:
```
int main() {
    
   int number;
   
   printf("Enter a number between 1 to 7:");
   scanf("%d", &number);
   
   switch(number){
       case 2:
       case 3:
       case 4:
       case 5:
       case 6:
           printf("Weekday");
           break;
       
       case 1:
       case 7:
           printf("Saturday");
           break;
      
       default:
           printf("Invalid number");
   }
    return 0;
}   
```

Here is the program to create a calculator using switch statement:
```
int main() {
    
    char operator;
    
    printf("Choose an operation from the following, ['+', '-', '/', '*']: ");
    scanf("%c", &operator);
    
    double number1;
    printf("Enter the first number: ");
    scanf("%lf", &number1);
    
    double number2;
    printf("Enter the second number: ");
    scanf("%lf", &number2);
    
    double result;
    
    switch(operator){
        case '+':
        result = number1 + number2;
        break;
        
        case '-':
        result = number1 - number2;
        break;
        
        case '/':
        result = number1 / number2;
        break;
        
        case '*':
        result = number1 * number2;
        break;
    }
    
    printf("%.2lf", result);

    return 0;
}
```