C if Statement
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
if (x = 5)instead ofx == 5, which assigns and is always true. - Putting a semicolon after the condition,
if (x > 5);, so the block always runs. - Omitting braces around several statements so only the first belongs to the
if.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: