Variables and Assignments Flashcards
(20 cards)
Variable Declaration
What is a variable declaration?
A variable declaration is a statement that declares a new variable, specifying the variable’s name and type.
For example, int userAge; declares a new variable named userAge that can hold an integer value.
Variable
What is a variable in programming?
A variable is a named item used to hold a value, such as x or numPeople.
Memory Allocation
What does memory allocation mean in the context of variables?
Memory allocation is the process of determining a suitable memory location to store data like variables. Each variable has a memory address that stores its value.
Assignment Statement
What is an assignment statement?
An assignment statement assigns a value to a
variable.
Example: x = 5; assigns the value 5 to x. The variable retains this value until it is assigned again.
Assignment with Expressions
What can the right side of an assignment statement contain?
The right side of an assignment can be an expression, such as a number (5), a variable (x), or a calculation (x + 2).
Assignment with Same Variable on Both Sides
What happens when a variable appears on both sides of an assignment statement? Give an example.
The variable on the left is updated using the value on the right.
Example: If x = 6, then x = x + 1; updates x to 7. The original value is overwritten.
Incrementing a Variable
What does it mean to increment a variable?
Incrementing a variable means increasing its value by 1.
Example: x = x + 1; increments x.
Expression
What is an expression in programming?
An expression is a combination of values, variables, and operators that evaluates to a single value.
Examples: 80, numApples, x + 2.
Integer Literal
What is an integer literal?
An integer literal is a whole number directly written in code, such as 80.
Note: Commas are not allowed in an integer literal, so 1,333,555 is written as 1333555.