if else statement in c++
- The if statement lets you do something if a condition is true. If it isn’t true, nothing happens. But suppose we want to do one thing if a condition is true, and do something else if it’s false.
- That’s where the if...else statement comes in. It consists of an if statement, followed by a statement or block of statements, followed by the keyword else, followed by another statement or block of statements.
Syntax
if (condition)
{
statement;
}
else
{
statement;
}
If the condition is true, the first statement is executed. If it is false, the second statement is executed.
Flow Diagram
Example
// ifelse.cpp
// demonstrates IF...ELSE statememt
#include using namespace std;
int main()
{
int x;
cout > x;
if( x > 100 )
cout << “That number is greater than 100 ”;
else
cout << “That number is not greater than 100 ”;
return 0;
}
Output
Enter a number: 300
That number is greater than 100
Enter a number: 3
That number is not greater than 100