C++ if Statement
if (testExpression)
{
// statements
}
How if statement works?
Flowchart of if Statement
Example 1: C++ if Statement
// Program to print positive number entered by the user
// If user enters negative number, it is skipped
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
Output 2
Enter a number: -5
This statement is always executed.
Comments
Post a Comment