Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

# Understanding Conditional Statements: if else

In this tutorial, we'll uncover something much interesting which would simplify our work as a programer and help us to check if a given condition is true or not. We'll study about the Conditional statements like if - else , if-else if-else, nested if -else and switch (continued in next tutorial)

Understanding if else statements :

To make the control of the program use conditional flow we need to use conditional statements, as the name suggests, this type of flow of control depends upon a particular condition ie either if it is true or it is false etc.

We'll first look at an if statement by considering this example :-

``````#include<stdio.h>
int main()
{
int num;
printf("Enter a number n");
scanf("%d",&num);
if(num == 5)
{
printf("Number entered is 5");
}
return 0;
}
``````

The above program would simply accept a value from user, and if the value entered is equal to 5 ie of course 5, then we would get a message printed on console "Number entered is 5" , if value entered is not 5, nothing would happen.

Note: whenever you write an if statement, you should have an opening and closing parenthesis as shown (in case you need to have multiple statements to be executed for an if statement) else, the program with just single statement in if block should not necessarily consist a block, so the above program would work if

``````if(num == 5)
{
printf("Number entered is 5");
}
``````

is written as

``````if(num == 5)
printf("Number entered is 5");
``````

So, we just instructed the computer to print a message, if a given condition happens to be true (which is entered value is 5 in this case).

Now, consider, we wanted program to print a message if the value entered is not 5, ie, if value entered is 5, it would print the message "Number entered is 5" otherwise, it would print "Number entered is not 5" . Let's see how this can be done with the help of if-else

``````#include<stdio.h>
int main()
{
int num;
printf("Enter a number n");
scanf("%d",&num);
if(num == 5)
{
printf("Number entered is 5");
}
else
{
printf("Number entered is not 5");
}
return 0;
}
``````

Let's say if we need to print message for if a number is 2, it should print "Number entered is 2" , if it's 5, it should print "Number entered is 5" otherwise, it should print "Number is neither 2 nor 5. Let's see how it can be done with if-else statements

``````#include<stdio.h>
int main()
{
int num;
printf("Enter a number n");
scanf("%d",&num);
if(num == 5)
{
printf("Number entered is 5");
}
else if(num == 2)
{
printf("Number entered is 2");
}
else
{
printf("Number entered is not 5");
}
return 0;
}
``````

The above program could include any no of if - else if - else statements (which means no. of else if statements in between if and else can be increased )

To understand nested if-else let's see the following program:

``````#include<stdio.h>
int main()
{
int num;
printf("Enter a number n");
scanf("%d",&num);
if(num % 2 == 0)
{
printf("Number entered is even");
if(num == 2)
printf("Number entered is 2");
else
printf("Number entered is not 2");
}
else
printf("Number entered is not even");
return 0;
}
``````

The above program first checks if a number is even or not, if it's even then it checks if number is 2 or not.

Note : To check number is even or not, we used modulus operator as discussed in Tutorial on Operators . Here we checked, if the number when divided by 2 leaves remainder 0 ie, it is entirely divisible by 2, then it's even else it's not even.

In the next tutorial, we'll know about another conditional statement, switch statement.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.