Arithmetic, Logical & Boolean Operators

Arithmetic Operators

  • Addition (+): Adds two values together
  • Subtraction (-): Subtracts one value from another
  • Division (/): Divides one value by another
  • Multiplication (*): Multiplies two values together
  • Exponentiation (^): Raises a number to a power
  • Modulo (MOD): Returns the remainder of a division operation
  • Integer Division (DIV): Returns the whole number of a division operation

Pseudocode example:

a ← 5

b ← 3

c ← a + b

d ← a - b

e ← a * b

f ← a / b

g ← a MOD b

h ← a ^ b

i ← a DIV b

Python example:

a = 5

b = 3

c = a + b

d = a - b

e = a * b

f = a / b

g = a % b

h = a ** b

i = a // b

Java example:

int a = 5;

int b = 3;

int c = a + b;

int d = a - b;

int e = a * b;

double f = (double) a / b;

int g = a % b;

double h = Math.pow(a, b);

int i = a / b;

Visual Basic example:

Dim a As Integer = 5

Dim b As Integer = 3

Dim c As Integer = a + b

Dim d As Integer = a - b

Dim e As Integer = a * b

Dim f As Double = a / b

Dim g As Integer = a Mod b

Dim h As Double = Math.Pow(a, b)

Dim i As Integer = a \ b

Logical Operators

  • Equal to (=): Returns true if two values are equal
  • Less than (<): Returns true if the first value is less than the second value
  • Less than or equal to (<=): Returns true if the first value is less than or equal to the second value
  • Greater than (>): Returns true if the first value is greater than the second value
  • Greater than or equal to (>=): Returns true if the first value is greater than or equal to the second value
  • Not equal to (<>): Returns true if two values are not equal

Pseudocode example:

a ← 5

b ← 3

c ← (a = b)

d ← (a < b)

e ← (a <= b)

f ← (a > b)

g ← (a >= b)

h ← (a <> b)

Python example:

a = 5

b = 3

c = (a == b)

d = (a < b)

e = (a <= b)

f = (a > b)

g = (a >= b)

h = (a != b)

Java example:

int a = 5;

int b = 3;

boolean c = (a == b);

boolean d = (a < b);

boolean e = (a <= b);

boolean f = (a > b);

boolean g = (a >= b);

boolean h = (a != b);

Visual Basic example:

Dim a As Integer = 5

Dim b As Integer = 3

Dim c As Boolean = (a = b)

Dim d As Boolean = (a < b)

Dim e As Boolean = (a <= b)

Dim f As Boolean = (a > b)

Dim g As Boolean = (a >= b)

Dim h As Boolean = (a <> b)

Boolean Operators

Boolean operators are logical operators that can be used to compare two or more values and return a Boolean value (True or False) based on the comparison. There are 3 you need to know:

  • AND: Returns True if both conditions are True
  • OR: Returns True if one or both conditions are True
  • NOT: Returns the opposite of the condition (True if False, False if True)

Boolean Operators in Pseudocode:

IF (condition1 AND condition2) THEN

// code to execute if both conditions are True

END IF

IF (condition1 OR condition2) THEN

// code to execute if one or both conditions are True

END IF

IF NOT(condition) THEN

// code to execute if the condition is False

END IF

Boolean Operators in Python:

if condition1 and condition2:

# code to execute if both conditions are True

if condition1 or condition2:

# code to execute if one or both conditions are True

if not condition:

# code to execute if the condition is False

Boolean Operators in Java:

if (condition1 && condition2) {

// code to execute if both conditions are True

}

if (condition1 || condition2) {

// code to execute if one or both conditions are True

}

if (!condition) {

// code to execute if the condition is False

}

Boolean Operators in Visual Basic:

If condition1 And condition2 Then

' code to execute if both conditions are True

End If

If condition1 Or condition2 Then

' code to execute if one or both conditions are True

End If

If Not condition Then

' code to execute if the condition is False

End If

Loading

error: Content is protected !!