If-Else Condition in C Programming Part - II

Hey Friend, till now we have studied about only if statement. If you do not know about IF statement please read the previous tutorial before reading this. In if statement whenever the condition is true a set of instructions will proceed running. But every coin has two faces, till now we only learned about one condition. If the condition is true then the code will execute. In high-level programming, you have to code both situations such as if the first condition is true then a set of instructions will execute & if the condition is not true then the second set of instructions execute. This work is done using if-else statements.

Syntax of IF-Else Condition 

If (condition)


Statement 1;

Statement 2;

So on…;




Statement 1;

Statement 2 and so on…;


Note that:-

1. The Statement inside if and else keyword and generally known as if block and else block respectively.

2. The parenthesis inside the if-else block can be dropped if there is only one condition exists. 

3. You should not use any condition in the else keyword. The statements under else will be executed only if the condition with if statement is false. So it is treated as the default choice.

4.  Notice that the else part or statement of the program is placed just below the if part. And the statement of those blocks has indented to the right. This is a formatting convention that means just to write a program easier or understandable way. Look at the below program to understand in a better way. 

Program to find that the given number is a positive number or a negative number. 

#include <stdio.h>

void main()


 int number;

 printf("Enter a number to check if it is negative or positive :");




  printf("Number is negative"); 




  printf("Number is positive");



Output :- 

Explanation of the Program

I have already told the meaning of these (include, printf, int main) functions earlier. If you don’t know please check the previous post. In this program, we declare a variable name (number) as an integer value given by the user. 

Now we are going to check the number whether it is a positive number or a negative number.  So in the if block we check, is this number is smaller than zero? If yes then the program of (if block) executes and gives us the output. 

Or if the number is greater than zero then the else block starts to execute and give us the output.

Hope you all understand the program and if you have any query then comment below I will definitely try to explain with another example.  

1 comment:

  1. Their personalized care for each project and quick response time continues to stand out.
    San Francisco design companies


Like us on facebook

blogger templatesblogger widgets

Follow on Twitter



Contact Form


Email *

Message *

Mad About Computer. Powered by Blogger.