📅 Date: Jan 3, 2026
🔥 Topic: Conditionals (If, Else If, Else)
🤔 What are Conditionals?
Up until now, my code ran line-by-line, from top to bottom. But in real life, we make decisions based on situations.
"If it rains, take an umbrella. Else, wear sunglasses."
In C++, we use Control Flow statements to do this. The program evaluates a condition: if it's true, it runs a specific block of code; if false, it skips it.
🚦 The Syntax
The structure is very similar to English:
if (condition) {
// Runs if condition is TRUE
}
else if (another_condition) {
// Runs if the first was FALSE, but this one is TRUE
}
else {
// Runs if ALL above conditions were FALSE
}
⚖️ Comparison Operators
To make conditions, we need to compare things using these operators:
==: Equals to (Don't confuse with=assignment!)!=: Not Equals to>,<: Greater than, Less than>=,<=: Greater/Less than or Equal to
💻 Day 3 Code: The Grading System
To practice this, I built a simple Grading System. It takes the student's marks as input and decides the grade logic.
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks (0-100): ";
cin >> marks;
// The Logic Ladder
if (marks >= 90) {
cout << "Result: Grade A (Excellent!)" << endl;
}
else if (marks >= 75) {
cout << "Result: Grade B (Good)" << endl;
}
else if (marks >= 40) {
cout << "Result: Grade C (Passed)" << endl;
}
else {
// If none of the above are true
cout << "Result: Fail (Needs Improvement)" << endl;
}
return 0;
}
🧩 Logical Operators (The Combo Moves)
Sometimes we need to check two things at once. I also learned about Logical Operators:
&&(AND): Both conditions must be true.
Example:if (age > 18 && hasID)||(OR): At least one condition must be true.!(NOT): Reverses the result.
💠Thoughts
The power of code lies in logic. Today, I learned how to control the flow of the program. The most common mistake I made was confusing = (assign value) with == (compare value).
Tomorrow, I will tackle Loops (For & While) to repeat tasks without rewriting code.
No comments:
Post a Comment