Let us dive into C Part-6.1 [IF-Else]

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:

if(  CONDITION ){

//If Body --->This block will be activated if the CONDITION becomes TRUE

}else{

//Else Body ---> This block will be activated if the CONDITION becomes FALSE

}
C programming if else

Let’s think the implementation of this General Structure in our real life problem solving program. Think about a problem,

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.

INPUT:
Enter your Age as an Integer Number.


OUTPUT:
Output will be the given string “Adult” or “Not-Adult” depending on conditions.


EXAMPLE

SAMPLE INPUT:
3
12
21
17

SAMPLE OUTPUT:
Not-Adult
Not-Adult
Adult
Not-A


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,

#include<stdio.h>

int main(){
int age;
printf("Age: ");
scanf("%d", &age);

if(age>=18){
printf("Adult");
}else{
printf("Not-Adult");
}
}

Copy the code and compile it in your C compiler. See what will happen to you!!

Excited??

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;

if( CONDITION 1){
This block will be activated if the condition_1 is TRUE

}else if( CONDITION 2){
This block will be activated if the condition_1 is TRUE

}else if( CONDITION 3){
This block will be activated if the condition_1 is TRUE

}else{
This block will be activated if the No Condition is TRUE

}
C programming if elseif else

 
Now Come to the PROBLEM ๐Ÿ˜‰

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.

Grade Table: 

            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”  

INPUT:
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:
Output will be A+ or A or any one from the quoted characters with + or minus or without sign. 


EXAMPLE:

SAMPLE INPUT:
69
76
83

SAMPLE OUTPUT:
B
A-
A

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.

#include<stdio.h>

int main(){
int marks;
printf("Marks: ");
scanf("%d",&marks);

if(marks >= 85){
printf("A+");
}else if(marks >= 80){
printf("A");
}else if(marks >= 75){
printf("A-");
}else if(marks >= 70){
printf("B+");
}else if(marks >= 65){
printf("B");
}else if(marks >= 60){
printf("B-");
}else if(marks >= 55){
printf("C+");
}else if(marks >= 50){
printf("C");
}else if(marks >= 45){
printf("C-");
}else if(marks >= 40){
printf("D+");
}else if(marks >= 35){
printf("D");
}else if(marks >= 30){
printf("D-");
}else if(marks <= 29){
printf("F");
}
}

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. We are now going to talk about the Nested If-Else. 

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.

if( CONDITION ){
            //First if Body
 if( CONDITION ){
        //Nested if Body
   } else{
//Nested else Body

   }
}else{
//First else Body
}
C programming nested if else

Problem 

You will have to write a program to define if a given number is Positive or Negative or Zero. 

Input:

Input will be any integer number including positive negative or non-negative. 

Output:

Output will be the string, “Positive ” or “Negative ” or “Zero

Example

SAMPLE INPUT

12
-23
0
2

SAMPLE OUTPUT

Positive
Negative
Zero
Positive

Now let’s try the code below..

#include<stdio.h>
int main(){
int number;
printf("Enter the Number: ");
scanf("%d",&number);

if(number >= 0){
if(number == 0){
printf("Zero");
}else{
printf("Positive");
}
}else{
printf("Negative");
}

}

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”

So, friends take care and happy coding……… 

Spread it to the Techies

1 thought on “Let us dive into C Part-6.1 [IF-Else]”

Leave a comment