Introduce basic conditional statements (if...else
) and show how to check simple conditions in a program.
In this sub-episode, we’ll focus on the simplest form of conditional logic using if...else
statements. This allows a program to make decisions based on a single condition. If the condition is true, it runs one block of code; if false, it runs another.
Imagine you are deciding whether to go outside. If it’s raining, you stay inside; if not, you go outside. This is the basic idea behind an if...else
statement.
Leo Movie example !
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is not positive (it’s either negative or zero)." << endl;
}
return 0;
}
Explanation: If the input number is greater than zero, it prints that the number is positive. Otherwise, it assumes the number is either negative or zero.