← Back to C++ Course | Chapter 3: Operators | Lesson 1 of 9

C++ Operators

Operators are symbols that act on one or more operands to produce a new value, and C++ groups them into categories -- arithmetic, relational, and logical -- based on the kind of result they produce.

What are Operators?

An operator is a special symbol that performs an operation on one or more values, called operands, producing a new value -- the + symbol, for example, adds two numeric operands together.

Example: What are Operators?

cpp
#include <iostream>

int main() {
	int result = 5 + 3; // + is the operator, 5 and 3 are operands
	std::cout << result << std::endl;
	return 0;
}

Categories of Operators in C++

C++ groups operators into categories based on what kind of result they produce: arithmetic operators compute numbers, relational operators compare values and produce booleans, and logical operators combine boolean values.

Example: Categories of Operators in C++

cpp
#include <iostream>

int main() {
	int sum = 5 + 3;        // arithmetic
	bool isGreater = 5 > 3;  // relational
	bool bothTrue = true && isGreater; // logical
	std::cout << sum << " " << isGreater << " " << bothTrue << std::endl;
	return 0;
}

Operators and Operands

Every operator acts on operands -- most operators like + and * are binary and need two operands, while a few like ++ and unary - are unary and act on just a single operand.

Example: Operators and Operands

cpp
#include <iostream>

int main() {
	int a = 5, b = 3;
	int sum = a + b;  // binary: two operands
	int negative = -a; // unary: one operand
	std::cout << sum << " " << negative << std::endl;
	return 0;
}

Combining Operators in Expressions

Operators of different categories are frequently combined in a single expression, such as mixing arithmetic and relational operators to build a condition used inside an if statement.

Example: Combining Operators in Expressions

cpp
#include <iostream>

int main() {
	int price = 50, quantity = 3;
	if (price * quantity > 100) { // arithmetic + relational combined
		std::cout << "Order qualifies for discount" << std::endl;
	}
	return 0;
}

Operator Precedence Preview

When an expression mixes multiple operators, C++ evaluates them according to a fixed precedence order -- multiplication and division run before addition and subtraction by default, and parentheses can always override that default order.

Example: Operator Precedence Preview

cpp
#include <iostream>

int main() {
	int result = 2 + 3 * 4;   // multiplication runs first: 14
	int overridden = (2 + 3) * 4; // parentheses override: 20
	std::cout << result << " " << overridden << std::endl;
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.