Computer Programming/Variables

From Wikiversity
Jump to navigation Jump to search
Flowchart displaying variables

This lesson introduces variables, constants, data types, expressions, statements, and order of operations.

Objectives and Skills[edit]

Objectives and skills for this lesson include:

  • Understand variables and constants.
  • Use integer, floating-point, and string data types appropriately.
  • Use expressions and statements to assign values to variables.
  • Understand the order of operations for arithmetic and logical operators.

Readings[edit]

  1. Wikipedia: Variable (computer science)
  2. Wikipedia: Constant (computer programming)
  3. Wikipedia: Data type
  4. Wikipedia: Expression (computer science)
  5. Wikipedia: Statement (computer science)
  6. Wikipedia: Assignment (computer science)
  7. Wikipedia: Order of operations
  8. Wikipedia: Input/output
  9. Wikipedia: Self-documenting code

Multimedia[edit]

  1. YouTube: Elements of High-Quality Programs
  2. YouTube: Programming/Scripting Concepts Explained (Variables, Arrays, Strings, & Length)
  3. YouTube: Programming For Beginners - Variables
  4. YouTube: Programming For Beginners - Data Types
  5. YouTube: Introduction to Programs Data Types and Variables
  6. YouTube: Introduction to Programming - Basics
  7. YouTube: Declaring and using variables and constants
  8. YouTube: Performing arithmetic operations
  9. YouTube: Introduction to order of operations
  10. YouTube: Python3 Input & Output

Examples[edit]

Activities[edit]

Complete the following activities using a flowchart tool, pseudocode, or your selected programming language.

  1. Review MathsIsFun: Order of Operations. Create a program that demonstrates the order of operations. Include parentheses, exponents, multiplication, division, addition, and subtraction in your program. Use variables for the calculations and label the output. For example, part of the program might display:
        1 + 2 * 3 = 7
        (1 + 2) * 3 = 9
        ...
  2. Review Wikipedia: Data type. Create a program that demonstrates integer, floating point, and character or string data, and demonstrate converting between data types. For example, user input is always a string, but adding string values of "1" + "1" is typically "11", whereas, adding numeric values of 1 + 1 is 2. Use variables for the calculations and label the output.
  3. Create a program to prompt the user for hours and rate per hour and then compute gross pay (hours * rate).[1]
  4. Create a program that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds. For example, a person 1 year old is 12 months old, 365 days old, etc.
  5. Review MathsIsFun: US Standard Lengths. Create a program that asks the user for a distance in miles, and then calculate and display the distance in yards, feet, and inches, or ask the user for a distance in miles, and then calculate and display the distance in kilometers, meters, and centimeters.
  6. Review MathsIsFun: Area of Plane Shapes. Create a program that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes. Do not include shape choices. That will come later. Just include multiple shape calculations in sequence.
  7. Review Wikipedia: Zeller's congruence. Create a program that asks the user for their birthday (year, month, and day) and then calculate and display the day of the week on which they were born.

Lesson Summary[edit]

Key Terms[edit]

assignment
Sets the value saved in the storage location denoted by a given variable name.[2]
Boolean
A data type having two values, typically denoted true and false.[3]
constant
A value that cannot be altered by the program during normal execution.[4]
data type
A classification of data which tells the compiler or interpreter how the programmer intends to use the data.[5]
declaration
A language construct that specifies the properties of a given identifier.[6]
expression
A combination of one or more explicit values, constants, variables, operators, and functions that a programming language interprets and computes to produce another value.[7]
floating point
The formulaic representation that approximates a real number to a fixed amount of significant digits.[8]
integer
A number that can be written without a fractional component.[9]
modulus
The remainder after division of one number by another.[10]
operator
A programming language construct that performs a calculation from zero or more input values to an output value.[11]
order of operations
A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[12]
real number
a value that represents a quantity along a line, including integers, fractions, and irrational numbers.[13]
statement
The smallest standalone element of an imperative programming language that expresses some action to be carried out.[14]
string
A sequence of characters, either as a literal constant or as some kind of variable.[15]
variable
A storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.[16]

Assessments[edit]

See Also[edit]

References[edit]