Resources | Subject Notes | Computer Science
This section explains how to assign values to variables using pseudo-code.
A variable is a named storage location in a computer's memory that holds a value. This value can change during the execution of a program.
The assignment operator is typically represented by the equals sign (=). It is used to assign a value to a variable.
Here are examples of pseudo-code statements demonstrating the assignment of values to variables:
variable_name = value
Example: age = 25
assigns the value 25 to the variable age
.
variable_name = expression
Example: total_price = price_per_item * quantity
assigns the result of the calculation to total_price
.
variable_name = get_input("Prompt message")
Example: username = get_input("Enter your name: ")
assigns the user's input to the username
variable.
variable_name = new_value
Example: score = 0
followed by score = score + 10
re-assigns the score
variable.
Pseudo-code | Description |
---|---|
temperature = 22 |
Assigns the value 22 to the variable temperature . |
area = pi * radius * radius |
Calculates the area of a circle and assigns the result to area . |
name = input("Enter your name: ") |
Prompts the user for their name and assigns the input to name . |
count = count + 1 |
Increments the value of count by 1. |
In many programming languages, the assignment operator (=) does not imply equality comparison. It simply assigns the value on the right-hand side to the variable on the left-hand side. Understanding this distinction is crucial for writing correct programs.