If, Else-If and Else Statements

Thursday, June 23, 2011 2 Comments A+ a-

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.



2 comments

Write comments
suman
AUTHOR
March 21, 2019 at 7:24 AM delete

nice article for beginners.thank you.
welookups C++
javacodegeeks

Reply
avatar
Daniel Pawski
AUTHOR
October 14, 2019 at 11:35 AM delete

From what I know, such functions in programming languages are the absolute basis. Anyway, I'm very happy to use applications that work in the cloud because it seems to me the most comprehensive solution. I value programs from the company grapeup.com/ and I am of the opinion that such solutions in the cloud are very good.

Reply
avatar