Table of Contents:
Basic Operators
In every programming language, operators are the symbols used to perform operations on values and variables. Think of operators as tools that help your program calculate results, compare values, or make decisions. Just like arithmetic in real life uses +, –, ×, and ÷, programming languages provide their own sets of operators.
Different languages (C, Java, Python) may have slight variations, but the fundamental concepts remain the same. This chapter introduces the most common operator categories you will use in your programs.
What Are Operators?
Operators tell the computer what action to perform. They work on values called operands.
Example:
10 + 20
Here:
10and20→ operands+→ operator
Types of Operators
Most programming languages support the following categories of operators:
Arithmetic Operators
These operators perform basic mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b (remainder) |
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
| = | Assign value | x = 10 |
| += | Add and assign | x += 5 (x = x + 5) |
| -= | Subtract and assign | x -= 3 |
| *= | Multiply and assign | x *= 2 |
| /= | Divide and assign | x /= 2 |
Relational (Comparison) Operators
These operators compare two values and return true or false.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal | a >= b |
| <= | Less or equal | a <= b |
basic operators, programming operators, arithmetic operators, relational operators, logical operators, assignment operators, C operators, Java operators, Python operators, programming tutorial
Logical Operators
Logical operators help combine conditions.
| Operator | Meaning | Example |
|---|---|---|
| && (and) / and | True if both conditions are true | a > 5 && b < 10 |
| || (or) / or | True if at least one condition is true | a == 10 || b == 20 |
| ! (not) | Reverses condition | !false → true |
Unary Operators
These operate on a single operand.
| Operator | Meaning | Example |
|---|---|---|
| ++ | Increment | x++ |
| — | Decrement | x– |
| – | Unary minus | -x |
Example in C
#include <stdio.h>
int main() {
int a = 10, b = 20;
int result = a + b;
printf("Result = %d", result);
return 0;
}
Example in Java
class Main {
public static void main(String[] args) {
int a = 10, b = 20;
int result = a + b;
System.out.println("Result = " + result);
}
}
Example in Python
a = 10
b = 20
result = a + b
print("Result =", result)
When Do You Use Operators?
You will use operators when you need to:
- Perform calculations
- Compare values
- Make decisions using conditions
- Work with loops
- Update variable values
Operators are used in almost every line of code you write.
FAQ
What are basic operators in programming?
Basic operators are symbols used to perform operations such as addition, comparison, or logical evaluation in a program.
Are operators the same in all programming languages?
Most languages share similar operator concepts, but syntax differences may exist.
What is the difference between = and ==?
= assigns a value, while == compares two values.
What does the modulus operator (%) do?
It returns the remainder of a division operation.
Why are operators important in programming?
They allow programs to perform calculations, make decisions, and control the flow of execution.