Section

Basic Operations: The Building Blocks of Computation

Part of The Prince Academy's AI & DX engineering stack.

Follow The Prince Academy Inc.

Now that we've touched on variables and data types, let's dive into the fundamental actions our algorithms can perform: basic operations. These are the workhorses of computation, allowing us to manipulate data and make decisions. Think of them as the verbs in our algorithmic language.

The most common operations revolve around numbers, and these are often referred to as arithmetic operations. They allow us to perform calculations, which are central to many algorithmic tasks.

Here are the core arithmetic operations you'll encounter:

Addition: This is straightforward – combining two numbers to get their sum. In most programming languages, it's represented by the '+' symbol.

let sum = 10 + 5;
console.log(sum); // Output: 15

Subtraction: Finding the difference between two numbers. The symbol for subtraction is typically '-'.

let difference = 20 - 7;
console.log(difference); // Output: 13

Multiplication: Calculating the product of two numbers. The '*' symbol is commonly used for this.

let product = 6 * 4;
console.log(product); // Output: 24

Division: Splitting a number by another to find the quotient. The '/' symbol represents division.

let quotient = 30 / 5;
console.log(quotient); // Output: 6

Modulo (or Remainder): This operation gives you the remainder after division. It's incredibly useful for tasks like determining if a number is even or odd, or for cyclical operations. It's often represented by the '%' symbol.

let remainder = 17 % 5;
console.log(remainder); // Output: 2 (because 17 divided by 5 is 3 with a remainder of 2)

Beyond arithmetic, there are comparison operations that allow us to compare values. These are crucial for making decisions within our algorithms. They often result in a boolean value: either 'true' or 'false'.

Key comparison operations include:

Equal to: Checks if two values are the same. Symbols can vary, but '==' or '===' are common.

let isEqual = (10 == 10);
console.log(isEqual); // Output: true
let isNotEqual = (10 == 5);
console.log(isNotEqual); // Output: false

Not equal to: Checks if two values are different. Represented by '!=' or '!=='.

let isDifferent = (10 != 5);
console.log(isDifferent); // Output: true

Greater than: Checks if the first value is larger than the second. Represented by '>'.

let isGreater = (15 > 10);
console.log(isGreater); // Output: true

Less than: Checks if the first value is smaller than the second. Represented by '<'.

let isLess = (8 < 12);
console.log(isLess); // Output: true

Greater than or equal to: Checks if the first value is larger than or equal to the second. Represented by '>='.

let isGreaterOrEqual = (10 >= 10);
console.log(isGreaterOrEqual); // Output: true

Less than or equal to: Checks if the first value is smaller than or equal to the second. Represented by '<='.

let isLessOrEqual = (5 <= 8);
console.log(isLessOrEqual); // Output: true

Logical operations are also vital for combining or modifying boolean values, allowing for more complex decision-making. These operate on true/false values.

The primary logical operations are:

AND (&&): Returns true only if both operands are true.

let bothTrue = (true && true);
console.log(bothTrue); // Output: true
let oneFalse = (true && false);
console.log(oneFalse); // Output: false

OR (||): Returns true if at least one of the operands is true.

let atLeastOneTrue = (true || false);
console.log(atLeastOneTrue); // Output: true
let bothFalse = (false || false);
console.log(bothFalse); // Output: false

NOT (!): Inverts the boolean value of its operand. If it's true, it becomes false, and vice versa.

let isTrue = true;
let notTrue = !isTrue;
console.log(notTrue); // Output: false

These basic operations might seem simple, but when combined, they form the foundation for all the complex logic that algorithms are capable of. We'll see how these are used to control the flow of our programs in later sections.

graph TD
    A[Start]
    B[Get Input]
    C{Perform Operation}
    D[Display Result]
    E[End]

    A --> B
    B --> C
    C --> D
    D --> E