If-Else General Structures and Usage
Hello my dear Programmers!!! Let us dive into C again…….
If you have read my last blog post on decision making in C then I am sure about that, You are here to understand the If Else Topic broadly. Even if you are not redirected from the previous post though you can read this thoroughly to understand If-Else General structures and usage precisely.
If-Else General Structures and Usage:
Let’s think the implementation of this General Structure in our real life problem solving program. Think about a problem,
You are told to develop a program that will just distinguish the Adult and Not-Adult human by checking the age they will give entry to the program. The users will enter their age and the system will tell them if they are “Adult” or “Not-Adult” depending on their age.
Enter your Age as an Integer Number.
Output will be the given string “Adult” or “Not-Adult” depending on conditions.
Now let’s solve the problem. First we will have to understand like a human. Think there are three human [Raju, Roben, Maisha] ask you to tell them if he/she is Adult or Not-Adult. As a human you will listen the Age first. Think Raju said he is 25 years old, Roben said he is 20 years old and Maisha said she is 7 years old.
Now, You have a clue. We all know that if any human has aged more that 18 or equal to 18 then he is an Adult, otherwise they won’t be an Adult.
So, Raju’s age is 25 which is greater than 18. Therefore, he is an Adult.
Roben’s age is 20 which is greater than 18. Therefore, he is an adult.
Maisha’s age is 7 only which is less than 18. Therefore, she is Not-Adult.
Now, Let’s try to implement the human thoughts is programming. Here we know the general structure of If-Else. So, the code will be look like,
Copy the code and compile it in your C compiler. See what will happen to you!!
After solving the problem above of telling Adult or Not-Adult by comparing age with 18, some of you or all of you may have one question in your mind that, “If-Else” blocks always will be used together. But this is not true always. Because, sometimes we need to use just the “If” block. When we just want to get the TRUE answer from the program to take decision then we will use only the “If” condition and no “Else” will come after the “if”. If the condition goes FALSE then we really don’t care about the output for these type of problems. This type of problem will have only one condition.
What will we do if we have more than one condition to be checked???
We will have to solve another problem to understand the usage of “If-else” when we will have more than one condition. We will have to write the condition in Ladder. Ladder means scale or we can easily say Ladder means ranking. So, Ladder if-else has a general Structure like scale. See below;
Now Come to the PROBLEM 😉
Think you are a teacher. You have taken the final exam of your students and you have examined the exam paper and summed the scores. Now you will have to determine that who has got A+, who will get B, who will get F or other grades in their mark-sheet according to their marks in the exam paper they got. You will get a grade distribution table here.
Above 85 or equal will be “A+”
80 or equal will be “A”
75 or equal will be “A-“
70 or equal will be “B+”
65 or equal will be “B”
60 or equal will be “B-“
55 or equal will be “C+”
50 or equal will be “C”
45 or equal will be “C-“
40 or equal will Be “D+”
35 or equal will be “D”
30 or equal will be “D-“
and less than 30 will be “F”
Enter an Integer Number not less than 0 and not more than 100. As the standard limit of the marks distribution is 0 to 100.
Output will be A+ or A or any one from the quoted characters with + or minus or without sign.
Now let’s solve the problem. We have the table of marks where it is clearly defined that how a student will get A+, A or any other grades. So, we will have to create the ladder for all given grades according to the marks limit. We will use the relational operators here to compare if the marks are equal or greater than the range of the ladder. If the current pointer fails to satisfy the condition of the ladder then the pointer of the ladder will go one step down. Let’s see the code below.
Let Us C Solutionsmy Blog Blogspot
Copy the code and run it in your compiler and be a Teacher 😉
So, we are doing really really well in programming. Just one step here to finish If-Else part perfectly. Wd my passport for mac will not ejection. We are now going to talk about the Nested If-Else.
We all know about nesting. Nest means an object inside a larger one. Sometimes we need to take decision for several times in one programming flow. It is only possible by using Nested If-Else. It will be better to give a real life example. We will have to solve the last problem of this post to understand the use of Nested If-Else. But before that we will have to know the general structure of the Nested If-Else.
Let Us C Solutionsmy Blog Archive
You will have to write a program to define if a given number is Positive or Negative or Zero.
Input will be any integer number including positive negative or non-negative.
Let Us C Solutionsmy Blog Page
Output will be the string, “Positive ” or “Negative ” or “Zero“
Now let’s try the code below.
Copy the code and compile it and see the magic!!!!
Now we are at the last of this post today. I wish you have understood the terms I have discussed here. Please comment here if you face any kind of difficulties to understand this post. I will try my best to give you the best reply within the minimum time. And please share more and more with your friends. Because, we all know that,
“Sharing is caring”
Let Us C Solutionsmy Blog Builder
So, friends take care and happy coding………