Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Basic Operators in an Expression

User image

Published by

sanya sanya

Published at: 26th Jul, 2023
2.03 mins read

Basic Operators in a Expression

Basic operators are fundamental building blocks in programming that allow you to perform operations on variables and values.

Unary - [ +, - ]

The unary operators ‘+’ and ‘-’ are used to represent positive and negative signs, respectively. They operate on a single operand.

int number = 5;

int positiveNumber = +number; // positiveNumber is 5

int negativeNumber = -number; // negativeNumber is -5

Arithmetic [ +, -, /, *, % ]

Arithmetic operators are used for performing mathematical calculations.

int a = 10;

int b = 3;

int sum = a + b; // sum is 13

int difference = a - b; // difference is 7

int product = a * b; // product is 30

int division = a / b; // division is 3

int modulus = a % b; // modulus is 1 (remainder of 10 divided by 3)

Brackets - [ () ]

Parentheses are used to group expressions and specify the order of evaluation. They are used to control the precedence of operators in complex expressions.

int result = (a + b) * (a - b); // result is 49 (multiplication is performed before addition and subtraction)

Assignment [ = ]

The assignment operator is used to assign a value to a variable.

int x = 5; // Assigns the value 5 to x

int y = x; // Assigns the value of x to y

Relational [ ==, !=, >, <, >=, <=]

Relational operators compare values and return a Boolean result (‘true’ or ‘false’).

int p = 10;

int q = 5;

bool isEqual = (p == q); // isEqual is false

bool isNotEqual = (p != q); // isNotEqual is true

bool isGreater = (p > q); // isGreater is true

bool isLess = (p < q); // isLess is false

bool isGreaterOrEqual = (p >= q); // isGreaterOrEqual is true

bool isLessOrEqual = (p <= q); // isLessOrEqual is false

Logical Operators [ &&, ||, ! ]

Logical operators are used to combine and manipulate Boolean expressions.

bool condition1 = true;

bool condition2 = false;

bool logicalAnd = (condition1 && condition2); // logicalAnd is false

bool logicalOr = (condition1 || condition2); // logicalOr is true

bool logicalNot = !condition1; // logicalNot is false

Library

WEB DEVELOPMENT

FAANG QUESTIONS