Showing posts with label Selection Statements. Show all posts
Showing posts with label Selection Statements. Show all posts

If, Else-If and Else Statements

C++ program for if, if-else and else statements


Let me present to you a program using the selection statements: if, if-else and else statements. These kind of statements are used when there's a condition or set of conditions that needs to be evaluated first in order to proceed and execute certain statements within a program.

/*
If, Else-If and Else Selection
*/
#include <iostream>

using namespace std;

int main(void)
{
 int age;
 char pause;
  
 cout << "Enter your age: ";
 cin >> age;

 //check the age entered   
 if (age == 18)
 {
  cout << "You are 18 years old!" << endl;
 }
 else if (age > 18)
 {
  cout << "You are older than an 18 year old!" << endl;
 }
 else
 {
  cout << "You are below 18 years old!" << endl;
 }

 cin >> pause;

 return 0;

}

Selection statements are very essential and are commonly used within a program because they provide a very important way of implementing an algorithm. They are used for choosing which statements are to be executed or what will the program do or behave within certain sets of conditions.