Operators#

https://i.ytimg.com/vi/1mbgeb4w3wc/maxresdefault.jpg

Types#

Arithmetic operators are used to perform mathematical calculations.

int a = 10;
int b = 5;

int addition = a + b;   // Addition operator
int subtraction = a - b;   // Subtraction operator
int multiplication = a * b;   // Multiplication operator
int division = a / b;   // Division operator
int modulus = a % b;   // Modulus (remainder) operator

Logical operators are used to perform logical operations on boolean values.

bool a = true;
bool b = false;

bool logicalAnd = (a && b);   // Logical AND operator
bool logicalOr = (a || b);   // Logical OR operator
bool logicalNotA = !a;   // Logical NOT operator for a
bool logicalNotB = !b;   // Logical NOT operator for b

Bitwise operators are used to perform operations on individual bits of integer values.

int a = 10;
int b = 5;

int bitwiseAnd = a & b;   // Bitwise AND operator
int bitwiseOr = a | b;   // Bitwise OR operator
int bitwiseXor = a ^ b;   // Bitwise XOR (exclusive OR) operator
int bitwiseNotA = ~a;   // Bitwise NOT operator for a
int leftShift = a << 1;   // Left shift operator
int rightShift = a >> 1;   // Right shift operator

These are just a few examples of the types of operators in C++. It’s important to note that the usage and behavior of operators can vary depending on the data types they are used with.

Relational operators are used to compare values and return a boolean result (true or false).

int a = 10;
int b = 5;

bool equal = (a == b);   // Equal to operator
bool notEqual = (a != b);   // Not equal to operator
bool greaterThan = (a > b);   // Greater than operator
bool lessThan = (a < b);   // Less than operator
bool greaterThanOrEqual = (a >= b);   // Greater than or equal to operator
bool lessThanOrEqual = (a <= b);   // Less than or equal to operator

Assignment operators are used to assign values to variables.

int a = 10;
int b = 5;

a += b;   // Equivalent to: a = a + b
a -= b;   // Equivalent to: a = a - b
a *= b;   // Equivalent to: a = a * b
a /= b;   // Equivalent to: a = a / b
a %= b;   // Equivalent to: a = a % b

Syntax#

input length, width
area = length * width
print area
 1#include <iostream>
 2using namespace std;
 3
 4int main() {
 5    double length, width;
 6    cout << "Enter length: ";
 7    cin >> length;
 8    cout << "Enter width: ";
 9    cin >> width;
10
11    double area = length * width;
12    cout << "Area of the rectangle: " << area << endl;
13
14    return 0;
15}
1length = float(input("Enter length: "))
2width = float(input("Enter width: "))
3
4area = length * width
5print("Area of the rectangle:", area)
 1import java.util.Scanner;
 2
 3public class Main {
 4    public static void main(String[] args) {
 5        Scanner scanner = new Scanner(System.in);
 6        double length, width;
 7        System.out.print("Enter length: ");
 8        length = scanner.nextDouble();
 9        System.out.print("Enter width: ");
10        width = scanner.nextDouble();
11
12        double area = length * width;
13        System.out.println("Area of the rectangle: " + area);
14    }
15}
 1use std::io;
 2
 3fn main() {
 4    let mut input = String::new();
 5    println!("Enter length: ");
 6    io::stdin().read_line(&mut input).expect("Failed to read line");
 7    let length: f64 = input.trim().parse().expect("Please enter a valid number");
 8
 9    input.clear();
10    println!("Enter width: ");
11    io::stdin().read_line(&mut input).expect("Failed to read line");
12    let width: f64 = input.trim().parse().expect("Please enter a valid number");
13
14    let area = length * width;
15    println!("Area of the rectangle: {}", area);
16}
 1package main
 2
 3import (
 4    "fmt"
 5)
 6
 7func main() {
 8    var length, width float64
 9
10    fmt.Print("Enter length: ")
11    fmt.Scan(&length)
12
13    fmt.Print("Enter width: ")
14    fmt.Scan(&width)
15
16    area := length * width
17    fmt.Println("Area of the rectangle:", area)
18}