Skip to main content
Browse topics

If-Else Statement Example in C

2 min read Updated June 30, 2026
Share

About this program

This is an example program in simple example programs. Read the concept first: Hello World - Simple C Program, then study the code and output below.

Definition

  • The if statement executes based test expression inside the braces.
  • If statement expression is to true, If body statements are executed and Else body statements are skipped.
  • If statement expression is to false If body statements are skipped and Else body statements are executed.
  • Simply, Block will execute based on If the condition is true or not.
  • IF conditional statement is a feature of this programming language which performs different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition.

Syntax

c
if (expression) // Body will execute if expression is true or non-zero
{
	//If Body statements
}else
{
	//Else Body statements
}

Syntax Example

c
for example, In c
	if (i == 3) {
		doSomething();
	}
	else	{
		doSomethingElse();
	}

Syntax Explanation

Consider above example syntax,if (i == 3)

  • which means the variable i contains a number that is equal to 3, the statements following the doSomething() block will be executed.
  • Otherwise variable contains a number that is not equal to 3, else block doSomethingElse() will be executed.

Example Program For If..else

c
Example Program For If..else
/* Example Program For If..else In C Programming Language
  little drops @ thiyagaraaj.com
  Coded By:THIYAGARAAJ MP   */

// Header Files
#include<stdio.h>
#include<conio.h>

//Main Function
int main()
{
	// Variable Declaration
	int a;

	//Get Input Value
	printf("Enter the Number :");
	scanf("%d",&a);

	//If Condition Check
	if(a > 10)
	{
	  // Block For Condition Success
	  printf("%d Is Greater than 10",a);
	}
	else
	{
	  // Block For Condition Fail
	  printf("%d Is Less than/Equal to 10",a);
	}

	// Wait For Output Screen
	getch();
	//Main Function return Statement
	return 0;
}

Sample Output:

c
Enter the Number :8
8 Is Less than/Equal to 10

Enter the Number :10
10 Is Less than/Equal to 10

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C program do?
It is a C example program that demonstrates If-Else Statement, including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses conditional logic, reading user input and user-defined functions, illustrating a common pattern in C programming.

Related tutorials

Search tutorials