Operators

Operators

Operators perform certain operations on operands.

Assignment operator

Assignment operator = assigns a value to an operand. It changes the value of an operand.

#include <iostream>
int main(){
	int x;
	char c;
	double d;
	
	x = 123;
	c = 'A';
	d = 456.78;
}

Arithmetic operators

Arithmetic operators +, -, *, / perform arithmetic operations on their operands.

#include <iostream>
int main(){
	int x = 20;
	int y = 10;
	int z;
	z = x + y;
	z = x - y;
	z = x * y;
	z = x / y;
}

Increment and decrement operators

Increment ++ and decrement -- operators increment / decrement the value of an operand by 1. x++ is equivalent of x = x + 1.

#include <iostream>
int main(){
	int x = 1;
	x++;
	x--;
	++x;
	--x;
}

Compound assignment

Compound assignment operator performs an operation and assigns the result to an operand. x += 10 is equivalent to x = x + 10.

#include <iostream>
int main(){
	int x = 20;
	x += 10; // x = x + 10;
	x -= 10; // x = x - 10;
	x *= 10; // x = x * 10;
}

Equality operator

Equality operator == checks the equality of given values / operands. The result is true if the values are equal or false if the values are not equal.

#include <iostream>
int main(){
	int x = 10;
	if (x == 10){
		std::cout << "x is equal to 10.";
	}
	else{
		std::cout << "x is not equal to 10.";
	}
}

Relational operators

Relational operators > < >= <= != compare two expressions and the result of evaluation is either true or false.

#include <iostream>
int main(){
	int x = 10;
	if (x > 10){
		std::cout << "x is greater than 10.";
	}
	else{
		std::cout << "x is not greater than 10.";
	}
}

Logical operators

Logical operator && is equivalent to logical operation AND.

#include <iostream>
int main(){
	int x = 10;
	if (x > 5 && x < 15){
		std::cout << "x is both greater than 5 and less than 15.";
	}
	else{
		std::cout << "x is not greater than 5 and less than 15.";
	}
}

Logical operator || is equivalent to logical operation OR.

#include <iostream>
int main(){
	int x = 10;
	if (x > 5 || x < 15){
		std::cout << "x is greater than 5 or less than 15.";
	}
}