Variables, Constants, and Operators
Variables
Variable
A named storage location in memory that can hold a value, which can be changed during the execution of a program.
Variables are used to store data that may vary, such as user input or intermediate results.
- Consider a variable score in a game algorithm.
- As the player earns points, the value of the score increases.
When it comes to declaring data types of variables, there are two types of programming languages:
- Strongly Typed Languages
- A variable’s data type is strictly enforced.
- You cannot use a value of one type as if it were another without explicit conversion.
- Weakly Typed Languages
- The language allows more flexibility in how types are used.
- Implicit conversions between types are often performed automatically.
- In Python, a variable can be defined as x = 10, where x is the variable name and 10 is its value without specifying its data type.
- But in Java, you need to say that x is an integer explicitly:
int x = 10;In the official IB pseudocode that you will use in the examination, variable names ought to be in all capitals and a value is assigned via the = sign.
X = 10
output X- Use meaningful variable names to improve readability, for example, use totalPrice instead of tp.
- Avoid using the same variable for multiple purposes, as it can lead to confusion and errors.
Constants
Constant
A named storage location in memory that holds a value, which cannot be changed during the execution of a program.
Variables and constants are both used to store data, but constants cannot be changed after they are defined, for instance, mathematical constants or configuration settings.
- In a physics simulation, the constant GRAVITY might be set to 9.8 m/s².
- This value remains unchanged throughout the simulation.
- In a weather forecasting algorithm, variables store real-time data such as temperature and humidity.
- Constants represent fixed values like the freezing point of water.
- Usually, for clarity, constants are given names in all capitals.
- Use constants to avoid hardcoding values.
- Using constants makes your algorithm easier to maintain and reduces the risk of errors, as otherwise, this can lead to unintended changes and bugs.
In Java, a constant can be defined by adding the keyword final to the declaration of a variable.
final int MAX_SCORE = 100;Operators
Operator
A function that performs an operation on one or more operands.
- Operators are used to perform operations on variables and values.
- Types of operators include:
- Arithmetic Operators: Perform mathematical operations such as addition, subtraction, multiplication, and division.
- Common notation: +, -, *, /, %
- Relational Operators: Compare two values and return a boolean result (true or false)
- Common notation: ==, !=, >, <, >=, <=
- Logical Operators: Combine boolean expressions
- Common notation: && (AND), || (OR), ! (NOT)
- Arithmetic Operators: Perform mathematical operations such as addition, subtraction, multiplication, and division.
Comparison and logical operators are often used in conditional statements to control the flow of an algorithm.
In the expression $a + b$, $+$ is an arithmetic operator that adds the values of a and b.
For instance, official IB pseudocode, apart from basic arithmetic operators, includes:
| Symbol | Definition | Example |
|---|---|---|
| = | is equal to | 5 = 5 is true |
| > | is greater than | 6 > 2 is true |
| >= | is greater than or equal to | 7 >= 8 is false |
| < | is less than | 3 < 5 is true |
| <= | is less than or equal to | 4 <= 4 is true |
| $\neq$ | not equal to | 3 $\neq$ 4 is true |
| AND | logical AND | |
| OR | logical OR | |
| NOT | logical NOT | |
| mod | modulo (returns the remainder when one number is divided by another) | 10 mod 3 = 1 (because $10$ divided by $3$ leaves a remainder of $1$) |
| div | integer part of quotient (returns the quotient of a division, ignoring the remainder) | 10 div 3 = 3$ (because $10$ divided by $3$ is $3$ with a remainder of $1$) |
- Don't confuse the mod operator with regular division.
- Mod returns the remainder, not the quotient.
The mod and div operators are particularly useful in algorithms that involve looping or partitioning data.
To determine if a number is even or odd, you can use the mod operator:
X = 5
if X mod 2 = 0 then
output "X is even"
else
output "X is odd"
end ifHow Variables, Constants, and Operators Work Together
- Variables, constants, and operators work together to perform calculations and make decisions in algorithms.
- They enable algorithms to process data dynamically and adapt to different inputs.
Consider an algorithm that calculates the final price of a product after applying a discount:
- Define a constant DISCOUNT_RATE to store the discount percentage.
- Use a variable price to store the original price of the product.
- Apply the arithmetic operator * to calculate the discount amount.
- Use the arithmetic operator - to subtract the discount from the original price.
DISCOUNT_RATE = 0.15 # 15%
original_price = 10
new_price = original_price - original_price*DISCOUNT_RATE- Break down complex expressions into smaller steps using intermediate variables.
- This makes your algorithm easier to understand and debug.
- Can you explain the difference between a variable and a constant?
- How do operators enable calculations and comparisons in algorithms?
- Why is it essential to use constants for values that do not change?
- Can you explain the difference between the mod and div operators?
- How would you use comparison operators in a sorting algorithm?