← Back to C Course | Chapter 4: Control Flow | Lesson 1 of 11

C if Statement

An if statement runs code only when a condition is true, like taking a coat only if it is cold. If it is false, C skips it.
Syntax
c
if (condition) {
  // runs when condition is true
}

What is an if Statement?

An if statement evaluates a condition and runs its block of code only when that condition is true -- if it's false, the block is skipped entirely and execution continues with whatever comes after it.

Example: What is an if Statement?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Check whether age >= 18
	if (age >= 18) {
		// Print formatted text to the screen
		printf("Adult");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Simple Positive Check

You can use a single if statement to inspect a value and react to it, such as checking if (number > 0) to identify positive numbers, without needing an else branch when there's nothing specific to do for the false case.

Example: Simple Positive Check

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare number and set it to 5
	int number = 5;
	// Check whether number > 0
	if (number > 0) {
		// Print formatted text to the screen
		printf("Positive");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Checking Even Numbers

Combining the modulo operator with an if condition -- if (n % 2 == 0) -- lets a program detect even numbers by checking whether dividing by 2 leaves no remainder.

Example: Checking Even Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n % 2 == 0
	if (n % 2 == 0) {
		// Print formatted text to the screen
		printf("Even");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Single if Checks

Writing several independent if statements in sequence means each condition is checked on its own, regardless of whether an earlier one was true -- unlike an else-if ladder, none of them are skipped just because a previous check already matched.

Example: Multiple Single if Checks

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n > 0
	if (n > 0) {
		// Print formatted text to the screen
		printf("Positive ");
	}
	// Check whether n % 2 == 0
	if (n % 2 == 0) {
		// Print formatted text to the screen
		printf("Even");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested if

An if statement placed inside another if's block only runs when both the outer and inner conditions are true, letting you build layered logic like first checking a number is positive, then separately checking if it's also even.

Example: Nested if

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n > 0
	if (n > 0) {
		// Check whether n % 2 == 0
		if (n % 2 == 0) {
			// Print formatted text to the screen
			printf("Positive and even");
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing if (x = 5) instead of x == 5, which assigns and is always true.
  2. Putting a semicolon after the condition, if (x > 5);, so the block always runs.
  3. Omitting braces around several statements so only the first belongs to the if.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.