if Statement 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 in c++ 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

equals to ==
not equals to !=
less than <
greater than >
less than or equal to <=
greater than or equal to >=
not !
and &&
or ||

The if Statement

if (Boolean Expression) {

statement(s) to execute.

}

if…else Statement

if (Boolean Expression) {

statement(s) to execute if condition is true.

} else {

statement(s) to execute if condition is false.

}

if Statement Example

if (age == 20) {

cout << “I am twenty years old.”;

}

if…else Statement Example

#include <iostream>

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();

}

Leave a Reply

Your email address will not be published. Required fields are marked *