# If…else…if vs switch…case, difference and usage?

`if...else...if` and `switch...case` both programming constructs has ability to take decision based on conditions. Both are almost similar in nature. However, there is always a debate among beginners which to use and when to use what?

In this post, I will compare both on various grounds. So let us begin.

# Nested if…else statement in C

Simple `if` and `if...else...if` statements provide a great support to control programs flow. Simple `if` is single condition based task i.e. "if some condition is true, then do the task". In contrast `if...else...if` statement provides multiple condition checks i.e. "if some condition is true, then do some task. If the condition is false, then check some other condition and do some task. If all conditions fails, then do some default task."

# If…else and if…else…if statement in C

In previous post, I explained to control programs flow of execution using simple `if` statement.

Simple `if` works as "if some condition is true then do some tasks". It doesn't specifies what to do if condition is `false`. A good program must think both ways. For example - if user inputs correct account number and pin, then allow money withdrawal otherwise show error message to user.

# If statement in C

So far in our journey of learning C programming, we learned to write simple C programs, that executes in the sequence we write. A program that always executes in same sequence is worthless. The ability to control program flow and statements execution is worth.

`if` statement allows us to select an action based on some condition. It gives programmer to take control over a piece of code. Programmer can control the execution of code based on some condition or user input. For example - if user inputs valid account number and pin, then allow money withdrawal.

# C program to find all roots of a quadratic equation

Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Logic to find roots of quadratic equation in C programming.

Example
Input

```Input a: 8
Input b: -4
Input c: -2```

Output

```Root1: 0.80
Root2: -0.30```

# C program to check whether a character is Uppercase or Lowercase

Write a C program to input character from user and check whether character is uppercase or lowercase alphabet using if else. How to check uppercase and lowercase using if else in C programming. Logic to check uppercase and lowercase alphabets in C program.

Example
Input

`Input character: C`

Output

`'C' is uppercase alphabet`

# C program to count total number of notes in given amount

Write a C program to input amount from user and print minimum number of notes (Rs. 500, 100, 50, 20, 10, 5, 2, 1) required for the amount. How to the minimum number of notes required for the given amount in C programming. Program to find minimum number of notes required for the given denomination. Logic to find minimum number of denomination for a given amount in C program.

Example
Input

`Input amount: 575`

Output

```Total number of notes:
500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
2: 0
1: 0```

# If else programming exercises and solutions in C

`if...else` is a branching statement. It is used to take an action based on some condition. For example - if user inputs valid account number and pin, then allow money withdrawal.

If statement works like "If condition is met, then execute the task". It is used to compare things and take some action based on the comparison. Relational and logical operators supports this comparison.

# C program to calculate electricity bill

Write a C program to input electricity unit charge and calculate the total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit

How to calculate electricity bill using if else in C programming. Program to find electricity bill using if else in C. Logic to find net electricity bill in C program.

# C program to enter basic salary and calculate gross salary of an employee

Write a C program to input basic salary of an employee and calculate gross salary according to given conditions.
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary is between 10001 to 20000 : HRA = 25%, DA = 90%
Basic Salary >= 20001 : HRA = 30%, DA = 95%

How to calculate gross salary of an employee using if else in C programming. Program to calculate gross salary of an employee using if else in C program. Logic to find gross salary of employee in C program.

Example
Input

`Input basic salary of an employee: 22000`

Output

`Gross salary = 44000`