Nested if and else
Focus on nest if and else
Video content of Introduction to C Programming Season 1, Chapter 5 Can we found at Amazon, YouTube.
I hope you have executed some examples of the previous chapter.
In this chapter, we will go through the more complex form of “if” and “else” statement.
Now imagine you have launched a new scheme for a customer, if you buy a product worth Rs.2000, you will get 20% DISCOUNT and if your product is above Rs.1000 you get a 10% discount.
Well if the amount is less than 1000, the discount will be zero.
Let’s open the program from the last chapter again. I hope you remember this. It’s a simple program which gives the discount when the price is greater than 1000.
As usual, here we have main, variables, and printf, input statement, then condition and finally discount calculation.
Now we will modify just the condition part, everything else is the same. If you have a concern or queries with this program, please go through the previous chapter once again.
In this program we had only one condition, that bill amount should be greater than 1000, but now we have two conditions for discount i.e. Rs.2000 and Rs.1000.
How to handle this. Let’s look at the code. First, we will write the condition for bill amount >= 2000. If this condition is true, we will set the discount at 20 %.
Well, our one condition is handled. Now let’s look at the else part. If the condition is false computer will look for else part.
In else statement, we will write one more if and else condition. In the “if” we write condition for amount > 1000, else discount is 0. We will do detailed discussion
On the program, but let’s execute it first. Build and Run.
printf(“Calculate Discount Price”);
printf(“\nEnter Bill Amount : “);
if(bill_amount >= 2000)
else if(bill_amount >= 1000)
actual_discount = bill_amount * discount/100;
printf(“\nDiscount on Bill Amount is %f is %f.\nFinal Bill is %f “,bill_amount, actual_discount, final_bill);
Let’s give an input of 4000, observe the output, the final bill is Rs.3200. It is right
Let’s give an input of Rs.1000, now the output is 900. Now give input of Rs.500, well the output is the same. Final Amount is unchanged, of course, because the discount is zero.
Let’s try to understand this program in detail.
When you entered the input of Rs.4000 in scanf line, the system went to if statement. Now the system checks whether bill amount of 4000 is greater than 2000 or not. The answer is true.
Since the answer is true only “if” part is executed and the else part is being skipped.
The system directly goes to the acutal_discount statement, then calculates the final amount and then output is displayed.
The second time you entered Rs.1000, it checks whether bill_amount > 2000 or not. The answer is false. So it enters the else condition. Inside else it encounters another if statement. “If” statement checks whether the bill amount is greater than 2000 or not. Here answer is correct. So the discount value becomes 10 and the next else statement is ignored.
In this use case, we observed that the first “else” is executed and the second is not. All depends on the result of the condition that you have added. If the result of “if” statement is true, the statement will be run else it will go to the “else” part.
Now, let’s run the more complex if and else. In layman language, if you have given two Conditions. Like if have more than 60% in exams and your age is more than 18 you can apply for the Job. Here we have two conditions. For result to be true both the conditions should be true.
We call it “and” conditions. In C it’s represented by &&
Similar to “AND” we also have “OR” in real life. Like you can have a job if you have 60% or experience of 2 Years. If any of the condition is true, you will get the job. It’s called “OR”, it’s represented by || in C Language.
We also have “NOT” operator, we represent it by symbol “!” (Exclamation mark).
We will discuss this with an example later in this chapter.
we have 3 operators “AND“, “OR” and “NOT“.
Consider an example, you will get First Division if you score more than 60% of marks, if you get less than 60 and greater than 50 %, you get second division. If you have less than 50% and greater than 40% you get third division. If you get less than 40% then you are failed
We will write a program to understand this condition in details. So we have “main”, we will declare a float variable as “percentage” to take an input from the user.
We will have printf statements, followed by the scanf statement to accept user input.
Let’s write the program, if percentage >= 60, its first class.
We are not putting any curly brackets because we have just one statement. Remember we don’t require curly brackets if we just have one line under “if” and “else” conditions.
Next condition is important for the second division because it has two parameters, greater than 50 and less than 60. Here we will use “AND” operator. Inside this “if”, we will write its second division.
Here we have used “AND” operator. And this operator requires both the conditions to be true.
Next line we will write the condition for 3rd division i.e. percentage greater than 40 and less than 50.
Now we have the final condition, i.e. less than 40, It’s failed.
Let’s execute the program and see the output.
printf(“\nEnter percentage : “);
if (percentage >= 60)
printf(“\nyou will get First division “);
if (percentage >= 49 && percentage <= 60)
printf(“\nyou will get Second division “);
if (percentage >= 39 && percentage <= 50)
printf(“\nyou will get Third division “);
if (percentage <= 40)
printf(“\nyou are Failed “);
If you want to copy paste the code please refer our Newtum Github repository link over here.
We enter 55. It shows second division. We enter 75, it shows First Division.
Enter percentage : 75
you will get First division
Well, let’s iterate through the program. You entered the value 55 and Computer reaches the first condition. It checks whether the percentage is greater than 60 or not. The answer is negative, hence this statement of printf is not executed.
In the next line we will check two conditions. First, it’s greater than 50, yes it’s true. Next is less than 60, it’s also true. When we put “AND” to join the conditions, the result of both the conditions should be true, then only it is considered as true
Well, the statement “Second Division” is printed. But the program won’t stop here. Since there are few more lines yet to be executed, it will go to the next line.
Here it will check percentage greater than 50, the answer is true, Next condition in the same “IF” statement is percentage less than 50. The result is false. So in this, “IF” statement we have one condition as true and one condition is false. So the final result is false since we have an AND operator.
I hope you understand this part. If we have both conditions true in “IF” ‘s && statement, then it’s completely considered as true.
Well, the program doesn’t stop here, it goes to the next line again. Here the condition is percentage less than 40. The answer is false. Now it’s the last line, end of the function.
Well, this is how we handle multiple conditions in “if and else” using operators. There is more to be learned about IF and ELSE statement.
Before moving further you need to understand that conditions are key to any programming language. As a software developer, you will have to handle if and else condition depending upon the real-life scenarios.
Like giving a discount if the purchase amount is very high, shortlisting a candidate based on his marks and experience. Calculating salary slips depending upon the Government norms etc. We just want to ensure that you understand each and every part of if and else statement.
Let’s execute a few more example
Before writing any source code its important for you to understand the business problems. We also call it the use case. If you don’t understand the basic problems and its solutions it’s not possible for us to write the program.
Here is our example, you have to calculate tax on our income. You know we pay taxes to the government and they, in return, provide us with roads and other facilities.
Now the government has some tax rules. Before developing the program, Lets understand the business problem. Our policies say
- If your income is Greater than 100,000 then you will have to pay a tax of 20%.
- Income is Greater than 100,000 and your age is greater than 60, then you pay 10% tax. Some relief to senior citizens.
- Income is greater than (>) 100,000 and if your gender is female then you have to pay a tax of 10%.
- Income > 200,000, then you will have to pay taxes of 30%.
- If your income > 500,000, then pay tax of 40%.
We will study how to write a program for this in the next chapter using AND and OR Operator. Till then please give a thought on this and in the meantime, don’t forget to execute the example of earlier chapters.
Now that you have entered our program, you are definitely going to get a desired output. So keep learning.
C you in the next chapter.