TECHNOLOGY 

Published on

KembaraXtra - Computer Science - Math in High-Level Languages

1. Common Math Operators

  • High-level languages use symbols for math operations, similar across languages like C and Python.
Operation Operator
Addition +
Subtraction -
Multiplication *
Division /

2. Assignment vs. Equality

  • The equals sign = typically represents assignment (setting a variable's value), not equality (comparison).
  • x = 5 means "set the value of x to 5."

3. Integer Math vs. Floating-Point Arithmetic

  • Integers: Whole numbers (no fractions).
  • Floating-Point: Numbers that can represent fractions.

4. Type Declaration

  • C: Requires explicit type declaration for variables.
  • int x = 5; // Declares an integer variable x
  • double price = 1.99; // Declares a floating-point variable price
  • Python: Infers type automatically.
  • year = 2020 # year is an int
  • price = 1.99 # price is a float

5. Integer Division

  • C: Dividing integers results in an integer (fractional part is truncated).
  • int x = 5; int y = 2; int z = x / y; // z will be 2
  • If the result is assigned to a float, the integer result is converted to a float, but the fractional part is already lost.
  • int x = 5; int y = 2; float z = x / y; // z will be 2.0
  • Python: Dividing integers results in a float (fractional part is preserved).
  • x = 5; y = 2; z = x / y # z will be 2.5

6. Abbreviated Math Operators

  • Increment/Decrement (C):
  • x++; // Increment x (add 1)
  • x--; // Decrement x (subtract 1)
  • Add and Assign / Subtract and Assign (C and Python):
  • cats += 3 // Equivalent to cats = cats + 3
  • cats -= 3 // Equivalent to cats = cats - 3
Picture
0 Comments