# What Is Conditional If Statement?

Our today’s C programming tutorial for beginners lesson will be about conditional if statements.

Conditional statements are the most widely used and very very important statements while coding with any language. If you want to learn detail about conditional statements check this post.

There are so many conditional statements. Among them conditional if statements are widely used and much popular among programmers. The reason behind its wide usage are:

• Easy to use.
• Simple.
• Fast execution.
• No complex Parameter is needed.

So, if you are ready to gather detailed knowledge about conditional if statements read the whole post very carefully.

Conditional if statements are usually 4 types. They are:

1. Simple if statement.
2. if…else statement.
3. Nested if…else statement.

Post Contents

### Simple if Statement With Example:

The if statement usually always asks a question and if the answer is true then it can execute otherwise it can not execute. So, if your condition has two or more answers then you have to write two or more if statements for that case.

Syntax:

if(condition)

{

statement block;

}

For example, look at the following code:

Question: Check a number whether it is even or odd or zero (Just consider for this problem zero is neither even nor odd).

#include <stdio.h>
int main ()
{
int x;
scanf(“%d”,&x);
if(x==0)
{
printf(“%d is zeron”,x);
}
if(x%2==0)
{
printf(“%d is an even numbern”,x);
}
if(x%2!=0)
{
printf(“%d is an odd numbern”,x);
}

return 0;
}

Run this problem in your compiler.

Input: 3

Output: 3 is an odd number

Input: 22

Output: 22 is an even number

Now take 0 as input then see the output. Your output should be like this:

0 is zero
0 is an even number

Here you can see for this input we have two output. As 0 is zero by the first condition. So, it is fine but 0 is also shown as an even number. That is because 0%2=0 which is true for the 2nd if statement. But we have to show 0 as zero but not as an even number. So, we have to make a simple change in our code. Try to do this by yourself without looking at the below code. However, if you can’t. Then look at this code.

#include <stdio.h>
int main ()
{
int x;
scanf(“%d”,&x);
if(x==0)
{
printf(“%d is zeron”,x);
}
if(x>0&&x%2==0)
{
printf(“%d is an even numbern”,x);
}
if(x%2!=0)
{
printf(“%d is an odd numbern”,x);
}

return 0;
}

Here you see we make the condition for the second if x>0&&x%2!=0 . This means, to be an even number it should fulfill two conditions. 1st is it should be greater than 0 then it should be divisible by 2. So, for this code. If we take 0 as input. Our output will be:

0 is zero

### if…else Statement With Example:

It is a simpler than if statement. if block executes when the answer is true. If the answer is false then else block execute.

Syntax:

if(condition)

{

true block;

}

else

{

false block;

}

For the previous problem consider 0 as also an even number as usual. So, to check an even or odd number by using the if…else statement the code will be as follow:

#include <stdio.h>
int main ()
{
int x;
scanf(“%d”,&x);

if(x%2==0)
{
printf(“%d is an even numbern”,x);
}
else
{
printf(“%d is an odd numbern”,x);
}

return 0;
}

Input: 56

Output: 56 is an even number

Input: 14555

Output: 14555 is an odd number

Here, first, we check if x is divisible by 2? If yes. Then x is an even number. This means if the block is executed. If not then else block executed.

Remember for all if or else if statement will execute when the answer of the condition is true. Otherwise else block will execute.

### Nested if…else Statement:

When you have to face a problem like first check the condition. If its answer is true then you have to check two more conditions or even if the answer is false then also you need to check another two or more conditions. Then you have to use the Nested if…else statement.

Syntax:

if(condition1)

{

if(condition2)

{

……

}

else

{

……

}

}

else

{

if(condition2)

{

…..

}

else

{

…..

}

Question: Find the maximum number among three numbers.

#include <stdio.h>
int main ()
{
int x,y,z;
scanf(“%d%d%d”,&x,&y,&z);
if(x>=y)
{
if(x>=z)
{
printf(“%d is greatestn”,x);
}
else
{
printf(“%d is greatestn”,z);
}
}
else
{
if(y>=z)
{
printf(“%d is greatestn”,y);
}
else
{
printf(“%d is greatestn”,z);
}
}

return 0;
}

Input: 23 344 56

Output: 344 is greatest

In this way, you can use any number of nested if…else conditional statements.

### else…if Ladder Conditional Statement With Example:

This statement is usually used when multiple decisions are involved. Like one condition can have 2 or more answers then we can use this statement.

Syntax:

if(condition1)

{

statement;

}

else if(condition2)

{

statement;

}

else if(condition3)

{

statement;

}

else

{

statement;

}

Question: You are given 4 numbers and you have to find the smallest one. What’s your code will be for that problem?

You can say that we can solve this using a nested if…else statement right??

Yes, you can. But you have to write a very long code for this problem. But you should solve this problem by using a nested if…else statement first. By doing so, you will be able to learn about nested if…else statement completely. So, write a code for this problem using a nested if…else statement and comment on the code below.

Now, for this problem, we can use else…if ladder to make the code easier and shorter by using logical operator &&. So, let’s have a look at this code:

#include <stdio.h>
int main ()
{
int a,b,c,d;
scanf(“%d%d%d%d”,&a,&b,&c,&d);
if(a<=b&&a<=c&&a<=d)
{
printf(“%d is the smallest numbern”,a);
}
else if(b<=a&&b<=c&&b<=d)
{
printf(“%d is the smallest numbern”,b);
}
else if(c<=b&&c<=a&&c<=d)
{
printf(“%d is the smallest numbern”,c);
}
else
{
printf(“%d is the smallest numbern”,d);
}

return 0;
}

Input: 4647 234 12 1213

Output: 12 is the smallest number

In this way, you can check any number of conditions using the else if statement. And it will be easier and simpler than nested if…else statement. Also, you can use nested if…else and else…if ladder in the same problem. This means in an else if ladder in some cases you need to use a nested if…else statement. No problem you can do that. For that, you have to follow the following format.

…..

else if(condition1)

{

if(condition2)

{

statement;

}

else

{

statement;

}

}

That’s it for today. If you practice more then you will learn more and will be a more efficient programmer. Now I am giving you 3 problems. Solve them using the most efficient conditional if statement. And then paste your code into the comment box.

Problem-1: You are given a number to check whether it is positive or negative or zero.

Problem-2: You are given an alphabet(a-z and A-Z inclusive) check whether it is a capital letter or small letter also check if it Vowel or consonant.

Problem-3: Find the largest number between 4 numbers.

In the next C programming tutorial for beginners tutorial, we will learn and see some examples using the switch and go to conditional statements with examples. Happy coding😊