Operators

Operators are constructs that are defined within programming languages which behave similar to Functions, but differ syntacticaly in that you don't have to predefine the function.

Basic Examples

Common examples of operators include:

  • Arithmetic operations: (+, -, /, *, etc …)
  • Comparison operations: (>, <, etc …)
  • Logical operations: (AND, OR, NOT, etc …)

These basic operators should be available to some extent in any programming language.

Assignment Operators

Assignment operators are used to assign values to variables. The left operand is variable, and the right is a value. Here are some of the standard assignment operators and their symbols:

Symbol Operation Explanation
= Assignment (a = b) This operator assigns the right operand to the left
+= Add and assign (a += b) This operator adds assigns the right operand
-= Subtract and assign (a -= b) This operator subtracts and assigns
*= Multiply and assign (a /= b) This operator multiplies and assigns
/= Divide and assign (a /= b) This operator divides and assigns
%= Modulus and assign (a %= b) This operator performs modulus and assigns

Not all of the listed assignment operators exist in every programming language, but most at least have += and -=, while all have some form of basic assignment.

Arithmetic Operators

In computer programming we often need to manipulate data doing mathematical calculations, or to put it more simple we use arithmetic operators to alter data. Here are the basic forms of arithmetic operators:

Operator Description Example Output
+ Adds to opperands 5 + 2 7
- Subtracts second operrand from the first 5 - 2 3
* Multiples both opperands 5 * 2 10
/ Divides numerator by de-numerator 5 / 2 2.5
% Gives the reminder of division 5 % 2 1

Almost all programming languages support these basic arithmetic operators and will treat them the same as these are basic math concepts. Here is an example of these operators in C:

int a = 10;
int b = 20;

int c = a + b;   
printf( "Value of c = %d\n", c);
// Prints: 30

c = a - b;   
printf( "Value of c = %d\n", c);
// Prints: -10

c = a * b;   
printf( "Value of c = %d\n", c);
// Prints: 200

c = b / a;   
printf( "Value of c = %d\n", c);
// Prints: 2

c = b % a;   
printf( "Value of c = %d\n", c);
// Prints: 0

Comparison Operators

Comparison operators are used to compare the contents of two values to put it simply. Comparison operators are most commonly used when we are working with conditionals to determine what step we want to take next with our program based on the contents of our values. Comparison operators behind the scenes give an output of a boolean data type. Here are the most basic forms of comparison operators:

Operator Description Example Output
== Checks if the values are equal (5 == 2) false
!= Checks if the values are not equal (5 != 2) true
> Checks if left value is greater than right (5 > 2) true
< Checks if left value is than right (5 < 2) false
>= Greater than or equal to (5 >= 2) true
<= Less than or equal to (5 <= 2) false

Almost all programming languages have some support for these basic comparison operators, although some may use different syntax. Here is an example of these being used in C, however we will be using conditionals in this example so if you are unfamiliar with the concept be sure to check that guide:

int a = 10;
int b = 20;

/* Here we check whether a is equal to 10 or not */
if( a == 10 ) {
   /* if a is equal to 10 then this body will be executed */
   printf( "a is equal to 10\n");
}

/* Here we check whether a and b are not equal */
if( a != b ) {
   /* if a is not equal to b then this body will be executed */
   printf( "a is not equal to b\n");
}

/* Here we check whether b is equal to 10 or not */
if( b == 10 ) {
   /* if b is equal to 10 then this body will be executed */
   printf( "b is equal to 10\n");
}

/* Here we check if a is greater then b or not */
if( a > b ) {
   /* if a is greater than b then this body will be executed */
   printf( "a is greater than b\n");
}

/* Here we check if a is less than b or not */
if( a < b ) {
   /* if a is less than b then this body will be executed */
   printf( "a is less than b\n");
}

/* Here we check if a is greater than or equal to b or not */
if( a >= b ) {
   /* if a is greater than or equal to b then this body will be executed */
   printf( "a is greater than or equal to b\n");
}

/* Here we check if a is less than or equal to b or not */
if( a <= b ) {
   /* if a is less than or equal to b then this body will be executed */
   printf( "a is less than or equal to b\n");
}

Logical Operators

Suppose we want to check for more than one condition in our conditionals. That is where logical operators come into play in our program. Logical operators also come into play when we only want to execute our conditionals when our condition isn't true, this is the NOT logical operator. Let's look at some examples of logical operators:

Operator Description Example Output
&& Checks if both opperands are true (5 > 2)&&(5 == 2) false
|| Checks if one opperands is true (5 > 2)||(5 == 2) true
! Negates logic of condition !(5 == 2) true

Almost all programming languages support these logical operators and will almost always use the same syntax. Let's take a look at an example of these logical operators in C, we will be using conditionals in this example so if you are unfamiliar with the concept check out that guide:

int a = 1;
int b = 0;

if ( a && b ) {
   printf("This will never print because condition is false\n" );
}

if ( a || b ) {
   printf("This will be printed print because condition is true\n" );
}

if ( !(a && b) ) {
   printf("This will be printed print because condition is true\n" );
}

TODO Advanced Examples

Some of the more advanced examples of operators are:

  • Bitwise Operators
  • Field access in record or object: (.)
  • Scope resolution operator: (::, ., etc …)
This page was last updated: 2024-10-31 Thu 18:29. Source