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 :

Conditional statements in C : if else, nested if-else

Conditional statements in C : if else, nested if-else

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 :-

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

is written as

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

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

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:

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.





  1. Really like this article, this has become one of my favorite blogs, as I enter into the world of programming late in life, I am learning a lot from you guys..thanks…

    • Thanks for those inspiring words.
      We’ll be constantly uploading more tutorials, stay tuned.
      If you have any questions, feel free to ask in comments section šŸ™‚

  2. It’s better if you put that if statement into a try catch exception. šŸ™‚

    • Hi GPX, Thanks for taking time to give your valuable suggestion, but, since it is a beginner tutorial, I’m concentrating on moving on slowly. Introducing exception handling would lead to trouble for the ones who are in early stages of learning programming.
      I hope you understand.

Leave a Reply

Your email address will not be published. Required fields are marked *