C++ and if statements

By patryksharks321

In the last tutorial describing variables I mentioned the

if

statement in the context of the bool variableFirst let’s show write a example program:

#include <iostream>
using namespace std;
int main()
{int age;
cout<<"To enter this page with erotic contents you have to be adult"<<endl;
cout<<"What is your age<<endl;
cin>>age;
if(age<18)
{
cout<<"Sorry your to young"<<endl;
}
if(age=>18)
{
cout<<"Haha we got you, You pervert"<<endl;
}
getchar();
return 0;}

So now that we have written the code let’s explain what it does:
First it print’s out a question about our age,then it takes input from the keyboard about our age
This should all be clear if you read my previous tutorial titled “Taking input within a program”
Now let’s see the main part of our program that is the

if

What it does is checkes if we are above 18 years old,18 years old or beneath the age of 18 and then according to the if statement prints something

if

statement looks something like this:

if(condition)
{
code to do….
}

The condition can be a comparisson of to variables,comparison of a variable with data of the type of which it is(for example int x; if(x>1) )and many others

Well I hope I explained the problem
greetings patryksharks321

Leave a Reply