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

}

Java Operators

Operators are used to manipulate variables. There are following type of operators in java.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. Following is the list of arithmetic operators in java.

Symbol Operator Example
 + Additive Operator  r =  n + 1;
 – Subtraction Operator  r =  n – 1;
 * Multiplication Operator  r =  n * 1;
 / Division Operator  r = n / 1;
 % Remainder Operator  r = n % 1;

Relational Operators

Relational operators are used to compare two variable and it will return true or false depending on the operator and values are variables being compared.

Symbol Operator Example
 == Equal to  a = = b
 != Not equal to  a ! = b
 > Greater than  a > b
 <  Less than  a < b
 >= Greater than or equal to  a > = b
 <= Less than or equal to  a > = b

Bitwise Operators

The bitwise operators work on bit level. They are used to manipulate the contents of variables at a bit level.

Symbol Operator Example
 ~ Unary bitwise complement ~var1
 & Bitwise AND var1 & var2
 | Bitwise inclusive OR var1 | var2
 ^ Bitwise exclusive OR var1 ^ var2
 << Signed left shift var1 << var2
 >> Signed right sift var1 >> var2
 >>> Unsigned right shift var1 >>> var2

Logical Operators

Logical operators are used to perform logical operations on variables. It return true or false depending on the value of variables and operator used.

Symbol Operator
 & AND
 && Conditional-AND
 | OR
 || Conditional-OR
 ! NOT
 ? : Ternary

Assignment Operators

Assignment operators are used to perform assignment operations on variables.

Symbol Operator Example
 = Simple assignment r = a + b
 += Add and Assignment r += a equivalent r = r + a
 -= Subtract and Assignment r -= a equivalent r = r – a
 *= Multiply and Assignment r *= a equivalent r = r * a
 /= Divide and Assignment r l= a equivalent r = r / a