Programming Fundamentals/Variables
Appearance
(Redirected from Computer Programming/Variables)
This lesson introduces variables, constants, data types, expressions, statements, and order of operations.
Objectives and Skills
[edit | edit source]- 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 | edit source]- Pressbooks: Programming Fundamentals
- Wikipedia: Variable (computer science)
- Wikipedia: Constant (computer programming)
- Wikipedia: Data type
- Wikipedia: Expression (computer science)
- Wikipedia: Statement (computer science)
- Wikipedia: Assignment (computer science)
- Wikipedia: Order of operations
- Wikipedia: Input/output
- Wikipedia: Self-documenting code
Multimedia
[edit | edit source]- YouTube: Programming For Beginners - Variables
- YouTube: Programming For Beginners - Data Types
- YouTube: Introduction to Programs Data Types and Variables
- YouTube: Introduction to Programming - Basics
- YouTube: Declaring and using variables and constants
- YouTube: Performing arithmetic operations
- YouTube: Introduction to order of operations
- YouTube: Algorithm using Flowchart and Pseudo code Level 1 Flowchart
- YouTube: Basic program using outputs- example 1
- YouTube: Basic program - random number example 2
- YouTube: Basic program - using inputs - example 3
Practice
[edit | edit source]Examples
[edit | edit source]Activities
[edit | edit source]Complete the following activities using a flowchart tool, pseudocode, or your selected programming language.
- Create a program to prompt the user for hours worked per week and rate per hour and then calculate and display their weekly, monthly, and annual gross pay (hours * rate). Base monthly and annual calculations on 12 months per year and 52 weeks per year.[1]
- 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.
- 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.
- 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. For now, just include multiple shape calculations in sequence.
- Create a program that calculates the area of a room to determine the amount of floor covering required. The room is rectangular with the dimensions measured in feet with decimal fractions. The output needs to be in square yards. There are 3 linear feet (9 square feet) to a yard.[2]
- Create a program that helps the user determine how much paint is required to paint a room and how much it will cost. Ask the user for the length, width, and height of a room, the price of a gallon of paint, and the number of square feet that a gallon of paint will cover. Calculate the total area of the four walls as
2 * length * height + 2 * width * height
Calculate the number of gallons as:total area / square feet per gallon
Note: You must round up to the next full gallon. To round up, add 0.9999 and then convert the resulting value to an integer. Calculate the total cost of the paint as:gallons * price per gallon
.[3] - Review Wikipedia: Aging in dogs. Create a program to prompt the user for the name of their dog and its age in human years. Calculate and display the age of their dog in dog years, based on the popular myth that one human year equals seven dog years. Be sure to include the dog's name in the output, such as:
Spike is 14 years old in dog years.
Lesson Summary
[edit | edit source]- Variables are parts of an equation that can change, thus they typically depend on user input. They should be named to refer to what the input/data represents.[4]
- There are five variable data types. Integer, Floating point, string, Boolean, and nothing data types.[5]
- Integer data types are values that have a mathematical value, or numbers. the range of these numbers can from -2,147,483,647 to 2,147,483,647.[6]
- A Floating points are data types that allow a fractional numerical value. The difference between integer data types and float types is that floats are used when a more precise number value is needed.[7]
- A String data type is a data type that consists of characters that are set to one order.[8]
- A Boolean data type is a data type that simply has two values; true or false. It's usually used to set loops and conditions.[9]
- A Nothing data type is a data type that is used to set a null or zero value.[10]
- Constants are part of an equation that can never change.[11]
- Programs are built on statements and expressions.[12]
- Expressions must be declared in order to be used, and they must have a unique identifier name, as well as a data type.[13]
- Statements contain executable code that modifies the actions of the program.[14]
- Operator precedence sets the rules that govern the 'order of operations' or the order of parsing operators for a particular language as the order may vary from one programming language to another.[15]
- Most programming languages follow the order of operations (PEMDAS) just like in regular math.[16]
- Identifier names follow a set of rules imposed by the language's technical limitations, good programming practices, and common industry standards for language.[17]
- Good programming techniques involve using meaningful and case consistent identifiers. Meaningful identifier helps others to understand your code better. Case consistent practice will help you to avoid errors.[18]
- The data type used for a variable's declaration sets the parameters for the kind of data that the variable can contain.[19]
Key Terms
[edit | edit source]- assignment
- Assigns a variable name to a value of data (or resets the value of data), that will then be stored by the computer.[20]
- Boolean
- A data type having two values, typically denoted true and false.[21]
- constant
- A value that cannot be altered by the program during normal execution.[22]
- data type
- A classification of data which tells the compiler or interpreter how the programmer intends to use the data.[23]
- declaration
- A language construct that specifies the properties of a given identifier.[24]
- double
- The most often used floating-point family data type used.[25]
- 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.[26]
- floating point
- The formulaic representation that approximates a real number to a fixed amount of significant digits.[27]
- integer
- A number that can be written without a fractional component.[28]
- modulus
- The remainder part after the division of one number by another.[29]
- NaN
- Reserved word used to indicate a non-numeric value in a numeric variable.[30]
- null
- Reserved word used to represent a missing value or invalid value.[31]
- operator
- A programming language construct that performs a calculation from zero or more input values to an output value.[32]
- precedence
- Determines the order in which the operators are allowed to manipulate the operands.[33]
- order of operations
- A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[34]
- real number
- a value that represents a quantity along a line, including integers, fractions, and irrational numbers.[35]
- self-documenting code
- Source code and user interfaces that follow naming conventions and structured programming conventions that enable the use of a system without prior specific knowledge.[36]
- statement
- The smallest standalone element of an imperative programming language that expresses some action to be carried out.[37]
- string
- A data type used to represent text rather than numbers.[38]
- truncation
- The fractional part of a floating-point data type that is dropped when converted to an integer.[39]
- 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. In which case, the value can change during the program's execution.[40]
Assessments
[edit | edit source]See Also
[edit | edit source]- Introduction to Programming
- Programming Logic/Variables
- Programming Challenges
- Medium: Case Styles: Camel, Pascal, Snake, and Kebab Case
References
[edit | edit source]- ↑ PythonLearn: Variables, expressions, and statements
- ↑ Wikibooks: Programming Fundamentals/Practice: Data and Operators
- ↑ Wikibooks: Programming Fundamentals/Practice: Data and Operators
- ↑ https://press.rebus.community/programmingfundamentals/chapter/constants-and-variables/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/data-types/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/integer-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/floating-point-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/string-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/boolean-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/nothing-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/constants-and-variables/
- ↑ https://www.webopedia.com/TERM/S/statement.html
- ↑ https://press.rebus.community/programmingfundamentals/chapter/order-of-operations/
- ↑ https://en.wikipedia.org/wiki/Statement_(computer_science)
- ↑ https://press.rebus.community/programmingfundamentals/chapter/order-of-operations/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/order-of-operations/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/identifier-names/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/identifier-names/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/identifier-names/
- ↑ Wikipedia: Assignment (computer science)
- ↑ Wikipedia: Boolean data type
- ↑ Wikipedia: Constant (computer programming)
- ↑ Wikipedia: Data type
- ↑ Wikipedia: Declaration (computer programming)
- ↑ https://press.rebus.community/programmingfundamentals/chapter/floating-point-data-type/
- ↑ Wikipedia: Expression (computer science)
- ↑ Wikipedia: Floating point
- ↑ Wikipedia: Integer
- ↑ Wikipedia: Modulo operation
- ↑ https://press.rebus.community/programmingfundamentals/chapter/nothing-data-type/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/nothing-data-type/
- ↑ Wikipedia: Operation (mathematics)
- ↑ https://press.rebus.community/programmingfundamentals/chapter/order-of-operations/
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Real number
- ↑ "Self-documenting code". Wikipedia. 2019-06-03. https://en.wikipedia.org/w/index.php?title=Self-documenting_code&oldid=900074753.
- ↑ Wikipedia: Statement (computer science)
- ↑ TechTerms: String
- ↑ https://press.rebus.community/programmingfundamentals/chapter/data-type-conversions/
- ↑ Wikipedia: Variable (computer science)