Skip to content

Latest commit

 

History

History
67 lines (50 loc) · 2.6 KB

File metadata and controls

67 lines (50 loc) · 2.6 KB

Understanding Operators in Programming

Operators are symbols in programming that perform operations on variables and values. They are the building blocks of any programming language, allowing you to perform various kinds of operations, such as arithmetic, comparison, logical, assignment, and more.

Types of Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

  • + : Addition (e.g., a + b)
  • - : Subtraction (e.g., a - b)
  • * : Multiplication (e.g., a * b)
  • / : Division (e.g., a / b)
  • % : Modulus (e.g., a % b returns the remainder of division)
  • ++ : Increment (e.g., a++ increases the value of a by 1)
  • -- : Decrement (e.g., a-- decreases the value of a by 1)

2. Comparison Operators

Comparison operators compare two values and return a boolean value, either true or false.

  • == : Equal to (e.g., a == b)
  • != : Not equal to (e.g., a != b)
  • > : Greater than (e.g., a > b)
  • < : Less than (e.g., a < b)
  • >= : Greater than or equal to (e.g., a >= b)
  • <= : Less than or equal to (e.g., a <= b)

3. Logical Operators

Logical operators are used to combine conditional statements.

  • && : Logical AND (e.g., a && b is true if both a and b are true)
  • || : Logical OR (e.g., a || b is true if either a or b is true)
  • ! : Logical NOT (e.g., !a is true if a is false)

4. Assignment Operators

Assignment operators are used to assign values to variables.

  • = : Assign (e.g., a = b assigns the value of b to a)
  • += : Add and assign (e.g., a += b is equivalent to a = a + b)
  • -= : Subtract and assign (e.g., a -= b is equivalent to a = a - b)
  • *= : Multiply and assign (e.g., a *= b is equivalent to a = a * b)
  • /= : Divide and assign (e.g., a /= b is equivalent to a = a / b)
  • %= : Modulus and assign (e.g., a %= b is equivalent to a = a % b)

Using Operators

Operators are used in programming to perform operations on variables and values. For example, in an if statement, comparison operators can be used to determine the flow of execution based on the comparison results.

int a = 10;
int b = 20;

// Using arithmetic operators
int sum = a + b; // sum is 30

// Using comparison operators
if (a > b) {
    // This block will not execute since a is not greater than b
}

// Using logical operators
if (a < b && a > 0) {
    // This block will execute since both conditions are true
}

Operators allow you to write concise and efficient code, making them a fundamental aspect of programming.