C++ if else(How if else Statement Work?)



C++ if...else

The if else executes the codes inside the body of if statement if the test expression is true and skips the codes inside the body of else.
If the test expression is false, it executes the codes inside the body of else statement and skips the codes inside the body of if.

How the if...else statement works?

Working of if else statement in C++ Programming

Flowchart of if...else

Flowchart of if...else statement in C++ Programming

Example 2: C++ if...else Statement

  1. // Program to check whether an integer is positive or negative
  2. // This program considers 0 as positive number
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. int number;
  8. cout << "Enter an integer: ";
  9. cin >> number;
  10. if ( number >= 0)
  11. {
  12. cout << "You entered a positive integer: " << number << endl;
  13. }
  14. else
  15. {
  16. cout << "You entered a negative integer: " << number << endl;
  17. }
  18. cout << "This line is always printed.";
  19. return 0;
  20. }
Output
Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.

Comments

Popular posts from this blog

C++ Templates ?

Function Overriding in C++ [With Example]

C++ Program to Print Number Entered by User