About Lesson
Conditional statements allow you to execute different blocks of code based on certain conditions. In C++, the if
, else if
, and else
keywords are used for this purpose.
int num = 10;
if (num > 0) {
cout << “Number is positive”;
} else if (num == 0) {
cout << “Number is zero”;
} else {
cout << “Number is negative”;
}
In the above example, if num
is greater than 0, the first block of code executes. If num
is equal to 0, the second block executes. Otherwise, the third block executes.