if statement in C++

by Nideesh C on February 17, 2011 · 0 comments

in C++




The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. Below you will find sample conditional statements and a list of relational operators to use with the statements.
Relational Operators:
equal: ==
not equal: !=
less than: <
greater than: >
less than or equal to: <=
greater than or equal to: >=
not: !
and: &&
or: ||
If statements:
if () {

}

if/else statement:
if () {

}
else if () {

}
else {

}
Note: If you’re testing whether two things are equal or not, use ==, not =. Using = will set the first variable to the second.
example,
if (Age == 20) {
cout << “I am twenty years old.”
}

Example:
#include

using namespace std;

int main()
{
int age;

cout<<”Please input your age: “;
cin>> age;
cin.ignore();
if ( age < 100 ) {
cout<<”You are pretty young!\n”;
}
else if ( age == 100 ) {
cout<<”You are old\n”;
}
else {
cout<<”You are really old\n”;
}
cin.get();
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

No related posts.

Leave a Comment

Previous post:

Next post: