4

Operators | Programming - GeeksforGeeks

 8 months ago
source link: https://www.geeksforgeeks.org/operators-programming/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types.

What are operators?

Operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.

Types of Operators:

Here are some common types of operators:

  • Arithmetic Operators: Perform basic arithmetic operations on numeric values. Examples: + (addition), – (subtraction), * (multiplication), / (division), % (modulo).
  • Comparison Operators: Compare two values and return a Boolean result (true or false). Examples: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to).
  • Logical Operators: Perform logical operations on Boolean values. Examples: && (logical AND), || (logical OR), ! (logical NOT).
  • Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=.
  • Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).
  • Bitwise Operators: Perform operations on individual bits of binary representations of numbers. Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).

These operators provide the building blocks for creating complex expressions and performing diverse operations in programming languages. Understanding their usage is crucial for writing efficient and expressive code.

Arithmetic Operators:

Arithmetic operators are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:

  • Addition (+): Combines two numeric values, yielding their sum. Example: result = 5 + 3; (result will be 8).
  • Subtraction (-): Subtracts the right operand from the left operand. Example: difference = 10 – 4; (difference will be 6).
  • Multiplication (*): Multiplies two numeric values, producing their product. Example: product = 3 * 7; (product will be 21).
  • Division (/): Divides the left operand by the right operand, producing a quotient. Example: quotient = 15 / 3; (quotient will be 5).
  • Modulo (%): Returns the remainder after the division of the left operand by the right operand. Example: remainder = 10 % 3; (remainder will be 1).

These operators are foundational for mathematical calculations, financial computations, and various algorithmic implementations. They are commonly used in everyday programming scenarios, providing the tools necessary for handling numerical data and solving mathematical problems within a program. Understanding how to use arithmetic operators is essential for performing precise and efficient calculations in programming.

  • Python3
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 5;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " % " << b << " = " << a % b << endl;
return 0;
}
Output
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
10 % 5 = 0

Comparison Operators:

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:

  • Equal to (==): Checks if the values on both sides are equal. Example: 5 == 5 evaluates to true.
  • Not equal to (!=): Checks if the values on both sides are not equal. Example: 10 != 5 evaluates to true.
  • Less than (<): Tests if the value on the left is less than the value on the right. Example: 3 < 7 evaluates to true.
  • Greater than (>): Tests if the value on the left is greater than the value on the right. Example: 10 > 8 evaluates to true.
  • Less than or equal to (<=): Checks if the value on the left is less than or equal to the value on the right. Example: 5 <= 5 evaluates to true.
  • Greater than or equal to (>=): Checks if the value on the left is greater than or equal to the value on the right. Example: 8 >= 8 evaluates to true.

These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.

  • Python3
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 5, c = 10;
if (a == b)
cout << a << " is equal to " << b << endl;
if (a != c)
cout << a << " is not equal to " << c << endl;
if (a < c)
cout << a << " is less than " << c << endl;
if (a <= b)
cout << a << " is less than or equal to " << b
<< endl;
if (c > a)
cout << c << " is greater than " << a << endl;
if (a >= b)
cout << a << " is greater than or equal to " << b
<< endl;
return 0;
}
Output
5 is equal to 5
5 is not equal to 10
5 is less than 10
5 is less than or equal to 5
10 is greater than 5
5 is greater than or equal to 5

Logical Operators:

Logical operators in programming are used to perform logical operations on Boolean values. These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:

  • Logical AND (&&): Returns true if both operands are true; otherwise, it returns false. Example: true && false evaluates to false.
  • Logical OR (||): Returns true if at least one of the operands is true; otherwise, it returns false. Example: true || false evaluates to true.
  • Logical NOT (!): Returns true if the operand is false and vice versa; it negates the Boolean value. Example: !true evaluates to false.

These logical operators are frequently used in conditional statements (if, else if, else), loops, and decision-making constructs to create complex conditions based on multiple Boolean expressions. Understanding how to use logical operators is essential for designing effective and readable control flow in programming.

  • Python3
#include <iostream>
using namespace std;
int main()
{
// Logical AND (&&)
int x = 5, y = 10;
if (x > 0 && y > 0) {
cout << "Both " << x << " and " << y
<< " are greater than 0." << endl;
}
// Logical OR (||)
x = 15;
y = -5;
if (x > 0 || y > 0) {
cout << "At least one of " << x << " or " << y
<< " is greater than 0." << endl;
}
// Logical NOT (!)
if (!(x == y)) {
cout << x << " is not equal to " << y << endl;
}
return 0;
}
Output
Both 5 and 10 are greater than 0.
At least one of 15 or -5 is greater than 0.
15 is not equal to -5

Assignment Operators:

Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:

  • Assignment (=): Assigns the value on the right to the variable on the left. Example: x = 10; assigns the value 10 to the variable x.
  • Addition Assignment (+=): Adds the value on the right to the current value of the variable on the left and assigns the result to the variable. Example: x += 5; is equivalent to x = x + 5;.
  • Subtraction Assignment (-=): Subtracts the value on the right from the current value of the variable on the left and assigns the result to the variable. Example: y -= 3; is equivalent to y = y – 3;.
  • Multiplication Assignment (*=): Multiplies the current value of the variable on the left by the value on the right and assigns the result to the variable. Example: z *= 2; is equivalent to z = z * 2;.
  • Division Assignment (/=): Divides the current value of the variable on the left by the value on the right and assigns the result to the variable. Example: a /= 4; is equivalent to a = a / 4;.
  • Modulo Assignment (%=): Calculates the modulo of the current value of the variable on the left and the value on the right, then assigns the result to the variable. Example: b %= 3; is equivalent to b = b % 3;.

Assignment operators are fundamental for updating variable values, especially in loops and mathematical computations, contributing to the dynamic nature of programming. Understanding how to use assignment operators is essential for effective variable manipulation in a program.

  • Python3
#include <iostream>
using namespace std;
int main()
{
// Assignment (=)
int x;
x = 10;
cout << "x after assignment: " << x << endl;
// Addition Assignment (+=)
x += 3;
cout << "x after x += 3: " << x << endl;
// Subtraction Assignment (-=)
x -= 2;
cout << "x after x -= 2: " << x << endl;
// Multiplication Assignment (*=)
x *= 4;
cout << "x after x *= 4: " << x << endl;
// Division Assignment (/=)
x /= 2;
cout << "x after x /= 2: " << x << endl;
// Modulo Assignment (%=)
x %= 3;
cout << "x after x %= 3: " << x << endl;
return 0;
}
Output
x after assignment: 10
x after x += 3: 13
x after x -= 2: 11
x after x *= 4: 44
x after x /= 2: 22
x after x %= 3: 1

Increment and Decrement Operators:

Increment and decrement operators are used in programming to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:

  • Increment Operator (++): Increases the value of a variable by 1. Example: x++; is equivalent to x = x + 1; or x += 1;.
  • Decrement Operator (–): Decreases the value of a variable by 1. Example: y–; is equivalent to y = y – 1; or y -= 1;.

These operators are frequently employed in loops, especially for iterating through arrays or performing repetitive tasks. Their concise syntax enhances code readability and expressiveness.

#include <iostream>
using namespace std;
int main()
{
// Increment Operator (++)
int a = 5;
cout << "Value of a: " << a << endl;
a++;
cout << "Value of a after (a++): " << a << endl;
// Decrement Operator (--)
int b = 8;
cout << "Value of b: " << b << endl;
b--;
cout << "Value of b after (b--): " << b << endl;
// Increment Operator in Expression
int x = 10;
int y = x++;
cout << "Value of x: " << x << endl;
cout << "Value of y (post-increment): " << y << endl;
// Decrement Operator in Expression
int m = 15;
int n = --m;
cout << "Value of m: " << m << endl;
cout << "Value of n (pre-decrement): " << n << endl;
return 0;
}
Output
Value of a: 5
Value of a after (a++): 6
Value of b: 8
Value of b after (b--): 7
Value of x: 11
Value of y (post-increment): 10
Value of m: 14
Value of n (pre-decrement): 14

Bitwise Operators:

Bitwise operators in programming perform operations at the bit level, manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:

  • Bitwise AND (&): Performs a bitwise AND operation between corresponding bits of two operands. Example: A & B sets each bit to 1 if both corresponding bits in A and B are 1.
  • Bitwise OR (|): Performs a bitwise OR operation between corresponding bits of two operands. Example: A | B sets each bit to 1 if at least one corresponding bit in A or B is 1.
  • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands. Example: A ^ B sets each bit to 1 if the corresponding bits in A and B are different.
  • Bitwise NOT (~): Inverts the bits of a single operand, turning 0s to 1s and vice versa. Example: ~A inverts all bits of A.
  • Left Shift (<<): Shifts the bits of the left operand to the left by a specified number of positions. Example: A << 2 shifts the bits of A two positions to the left.
  • Right Shift (>>): Shifts the bits of the left operand to the right by a specified number of positions. Example: A >> 3 shifts the bits of A three positions to the right.

Bitwise operators are useful in scenarios where direct manipulation of binary representations or specific bit patterns is required, such as optimizing certain algorithms or working with hardware interfaces. Understanding bitwise operations is essential for low-level programming tasks.

  • Python3
#include <iostream>
using namespace std;
int main()
{
// Bitwise AND (&)
int A = 5, B = 3;
cout << "(" << A << " & " << B << ") = " << (A & B)
<< endl;
// Bitwise OR (|)
cout << "(" << A << " | " << B << ") = " << (A | B)
<< endl;
// Bitwise XOR (^)
cout << "(" << A << " ^ " << B << ") = " << (A ^ B)
<< endl;
// Bitwise NOT (~)
cout << "~" << A << " = " << (~A) << endl;
// Left Shift (<<)
cout << "(" << A << " << 2) = " << (A << 2) << endl;
// Right Shift (>>)
cout << "(" << A << " >> 2) = " << (A >> 2) << endl;
return 0;
}
Output
(5 & 3) = 1
(5 | 3) = 7
(5 ^ 3) = 6
~5 = -6
(5 << 2) = 20
(5 >> 2) = 1

In conclusion, operators in programming are essential tools that enable the manipulation, comparison, and logical operations on variables and values. They form the building blocks of expressions and play a fundamental role in controlling program flow, making decisions, and performing calculations. From arithmetic and comparison operators for numerical tasks to logical operators for decision-making, and bitwise operators for low-level bit manipulation, each type serves specific purposes in diverse programming scenarios.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 05 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK