C++ if Statement(How if statement Work)

C++ if Statement

if (testExpression) 
{
   // statements
}

How if statement works?

Working of if statement in C++ Programming

Flowchart of if Statement

Flowchat of if statement in C++ Programming

Example 1: C++ if Statement

  1. // Program to print positive number entered by the user
  2. // If user enters negative number, it is skipped
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. int number;
  8. cout << "Enter an integer: ";
  9. cin >> number;
  10. // checks if the number is positive
  11. if ( number > 0)
  12. {
  13. cout << "You entered a positive integer: " << number << endl;
  14. }
  15. cout << "This statement is always executed.";
  16. return 0;
  17. }
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

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website