Lesson 7 C Conditional Statements
Lesson 7 C Conditional Statements
#include <iostream>
using namespace std;
int main()
{
int age; cin >> age;
if(age == 21)
{
cout << "I can vote on the next election";
}
return 0;
}
2. If …Else Statement
Sometimes you have a condition and you want to execute
a block of code if condition is true and execute another
piece of code if the same condition is false. This can be
achieved in C++ using if-else statement.
SYNTAX:
if (condition)
It means:
{
// Executes this block if If the condition is TRUE, the code inside the body of
If is executed.
// condition is true The code inside the body of else is skipped from
execution.
}
If the condition evaluates False, the code inside the
else body of else is executed.
The code inside the vbody of If is skipped from
{ execution
// Executes this block if
// condition is false
}
Find the number whether it is greater than or
SAMPLE CODE 1: less than.
#include <iostream> using namespace std;
int main()
{
int num=900; I
if( num < 95 ){