Types of Unary Operators
- Arithmetic Unary Operators:
++variable
orvariable++
: Increment the value of the variable by 1.--variable
orvariable--
: Decrement the value of the variable by 1.
- Logical Unary Operators:
!expression
: Negate the boolean value of the expression.
- Bitwise Unary Operators:
~variable
: Bitwise negation of the variable.
- Unary Plus and Minus:
+expression
: Indicates a positive value.-expression
: Indicates a negative value.
Below is a table summarizing common unary operators along with their symbols, description, and examples:
Operator | Symbol | Description | Example |
---|---|---|---|
Increment | ++ | Increases the value of a variable by 1 | x = 5; ++x; // x is now 6 |
Decrement | — | Decreases the value of a variable by 1 | y = 8; --y; // y is now 7 |
Unary Plus | + | Indicates a positive value | a = -3; b = +a; // b is -3 |
Unary Minus | – | Indicates a negative value | c = 4; d = -c; // d is -4 |
Logical NOT | ! | Negates the truth value of a boolean expression | flag = true; result = !flag; // result is false |
Bitwise NOT | ~ | Bitwise negation, flips the bits of an integer | num = 5; result = ~num; // result is -6 |
Unary Operators in Programming
In programming, operators act as powerful tools for manipulating data and performing various operations. Among these, unary operators stand out, functioning on a single operand to transform or evaluate data in different ways. This post explains the types, implementations, and best practices associated with unary operators across several programming languages.
Table of Content
- What are Unary Operators?
- Types of Unary Operators
- Unary Operators in C
- Unary Operators in C++
- Unary Operators in Java
- Unary Operators in Python
- Unary Operators in C#
- Unary Operators in JavaScript
- Examples and Use Cases of Unary Operator
- Best Practices of Unary Operator
Contact Us