Operators
Common Pseudocode provides a variety of operators for performing calculations and comparisons.
Arithmetic Operators
Section titled “Arithmetic Operators”Basic Operations
Section titled “Basic Operations”sum = 1 + 1 // Addition: 2difference = 10 - 5 // Subtraction: 5product = 3 * 4 // Multiplication: 12quotient = 10 / 2 // Division: 5Advanced Operations
Section titled “Advanced Operations”Exponentiation (to the power of):
result = 2 ^ 3 // 8result = 2 ** 3 // 8result = 2 to the power of 3 // 8Modulus (remainder):
remainder = 10 % 3 // 1remainder = 10 modulus 3 // 1remainder = remainder after 10 / 3 // 1Compound Assignment
Section titled “Compound Assignment”Shorthand for updating variables:
num = 5num++ // Same as num = num + 1 (now 6)num-- // Same as num = num - 1 (now 5)
num += 3 // Same as num = num + 3 (now 8)num -= 2 // Same as num = num - 2 (now 6)num *= 4 // Same as num = num * 4 (now 24)num /= 3 // Same as num = num / 3 (now 8)num %= 5 // Same as num = num % 5 (now 3)num ^= 2 // Same as num = num ^ 2 (now 9)Example
Section titled “Example”num = 8ans = num / 3 // ans = 2.666...num %= 3 // num = 2Trigonometric Functions
Section titled “Trigonometric Functions”Common Pseudocode includes built-in trigonometric functions:
// Basic trig functionsresult = sin of 30result = cos of 60result = tan of 20result = csc of 50result = sec of 70result = cot of 40
// Inverse trig functionsresult = sin^-1 of 0.5result = cos^-1 of 0.5result = tan^-1 of 1result = csc^-1 of 2result = sec^-1 of 2result = cot^-1 of 1Note: For inverse functions, use ^-1 or **-1 (not to the power of -1).
Comparison Operators
Section titled “Comparison Operators”Compare values and return boolean results:
Natural Language Style
Section titled “Natural Language Style”if x equals yif x is equal to yif x does not equal yif x is not equal to yif x is greater than yif x is less than yif x is greater than or equal to yif x is less than or equal to ySymbolic Style
Section titled “Symbolic Style”if x == y // Equal toif x != y // Not equal toif x > y // Greater thanif x < y // Less thanif x >= y // Greater than or equal toif x <= y // Less than or equal toExample
Section titled “Example”age = 20if age >= 18 output "You are an adult"else output "You are a minor"Logical Operators
Section titled “Logical Operators”Combine boolean conditions:
AND Operator
Section titled “AND Operator”All conditions must be true:
if age >= 18 AND age < 65if age >= 18 and age < 65if age >= 18 && age < 65OR Operator
Section titled “OR Operator”At least one condition must be true:
if day equals "Saturday" OR day equals "Sunday"if day equals "Saturday" or day equals "Sunday"if day equals "Saturday" || day equals "Sunday"NOT Operator
Section titled “NOT Operator”Inverts a boolean value:
if NOT isLoggedInif not isLoggedInif !isLoggedInComplex Expressions
Section titled “Complex Expressions”Combine multiple logical operators:
if (age >= 18 AND age < 65) OR isStudent output "Eligible for discount"
if NOT (temperature < 0 OR temperature > 100) output "Temperature is in normal range"Operator Precedence
Section titled “Operator Precedence”Operators are evaluated in this order (highest to lowest):
- Parentheses
() - Exponentiation
^,** - Multiplication
*, Division/, Modulus% - Addition
+, Subtraction- - Comparison operators
>,<,>=,<=,==,!= - NOT
!,not,NOT - AND
&&,and,AND - OR
||,or,OR
Tip: Use parentheses to make your expressions clearer:
// Less clearresult = a + b * c
// More clearresult = a + (b * c)