Ready to practice?
Sign up to access interactive coding exercises and track your progress.
If Statements
Learn how to make decisions in your programs using if statements and conditions.
Prerequisites
What are If Statements?
If statements allow your program to make decisions by executing different code based on conditions. They let you write programs that can respond to different situations.
This lesson provides a very simple introduction to if statements to get you started with conditional logic. We'll explore more advanced features like logical operators, nested conditions, and complex decision making in later lessons.
Basic If Statement Structure
The basic syntax of an if statement:
if (condition) {
// Code to run if condition is true
}
Parts of an If Statement
if
keyword - Starts the if statement- Condition in parentheses - A true/false expression
- Braces
{}
- Contain the code to execute - Semicolon after statements - Each statement inside needs a semicolon
if (x > 5) { // if keyword, condition in parentheses
std::cout << "Big!"; // Code inside braces, with semicolon
}
Full Example:
#include <iostream>
int main() {
int age = 18;
// Check: Is age greater or equal to 18?
if (age >= 18) {
// This line will print when the above is true
std::cout << "You are an adult!" << endl;
}
return 0;
}
Simple Conditions
Conditions use comparison operators to compare values:
Operator | Name | Description | Example | Result when true |
---|---|---|---|---|
== |
Equal to | Checks if two values are equal | 5 == 5 |
Both values are the same |
!= |
Not equal to | Checks if two values are different | 5 != 3 |
Values are different |
< |
Less than | Checks if left value is smaller | 3 < 5 |
Left value is smaller |
> |
Greater than | Checks if left value is larger | 8 > 5 |
Left value is larger |
<= |
Less than or equal to | Checks if left value is smaller or same | 5 <= 5 |
Left value is smaller or equal |
>= |
Greater than or equal to | Checks if left value is larger or same | 7 >= 5 |
Left value is larger or equal |
#include <iostream>
int main() {
int score = 85;
// ===== These are TRUE =====
// Check: Is score (85) greater than 80?
if (score > 80) { // TRUE (85 > 80)
// Output: Great job!
std::cout << "Great job!" << std::endl;
}
// Check: Is score (85) not equal to 100?
if (score != 100) { // TRUE (85 != 100)
// Output: Not perfect, but that's okay!
std::cout << "Not perfect, but that's okay!" << std::endl;
}
// Check: Is score (85) less than or equal to 90?
if (score <= 90) { // TRUE (85 <= 90)
// Output: Better luck next time!
std::cout << "Better luck next time!" << std::endl;
}
// ===== These are FALSE =====
// Check: Is score (85) equal to 100?
if (score == 100) { // FALSE (85 ≠100)
// This block won't execute
std::cout << "Perfect score!" << std::endl;
}
// Check: Is score (85) less than 70?
if (score < 70) { // FALSE (85 is not < 70)
// This block won't execute
std::cout << "Need to study more!" << std::endl;
}
// Check: Is score (85) greater than or equal to 90?
if (score >= 90) { // FALSE (85 is not >= 90)
// This block won't execute
std::cout << "Excellent work!" << std::endl;
}
return 0;
}
We'll learn about more if statements in later lessons, this serves as gentle introduction to conditional logic.
Summary
If statements let your programs make decisions:
Basic Structure:
if (condition) { code }
- Run code when condition is true
Key Rules:
- Use braces
{}
even for single statements - Conditions must evaluate to true or false
If statements are fundamental to programming - they let your code respond intelligently to different situations and user input.
If Statements - Quiz
Test your understanding of the lesson.
Practice Exercises
Basic If Statement Practice
Practice using simple if statements to make decisions based on conditions.
Lesson Discussion
Share your thoughts and questions