Program Logic Formulation - Chapter 1 Part 2 Notes (Variables, Data Types, and Naming Conventions)
Data Types
- Numeric vs String
- Numeric: can hold digits and participate in mathematical operations.
- String: holds text, letters, and other characters (punctuation, etc.).
- Distinguishing numeric types
- Languages like C++, C#, Visual Basic, and Java distinguish between integer (whole numbers) and floating-point (fractional) numeric variables that contain a decimal point.
- Type-safety
- Type-safety is a feature that prevents assigning values of an incorrect data type.
- Typical declarations (examples)
- dataType variableName; e.g.,
- int age;
- String name;
- with initialization, e.g.:
- int score = 100; // Declares 'score' as an int and initializes it to 100
- String greeting = "Hello"; // Declares 'greeting' as a String and initializes it to "Hello"
Data Types: Constants
- Numeric Constant
- A specific numeric value.
- String Constant
- A specific text value, or string of characters; string values are also called alphanumeric values.
- Note: constants are similar to variables but are assigned once and do not change.
Working with Variables
- Variables are named memory locations whose contents can vary over time.
- Example workflow:
- input myNumber
- set myAnswer = myNumber * 2
- output myAnswer
- Etymology: the term "variable" comes from the Latin variabilis meaning "changeable".
Declarations and Identifiers
- A declaration provides a data type and an identifier for a variable.
- An identifier is the program component’s name (the variable’s name).
Data Type, Storage, and Operations
- A data item’s data type classifications describe:
- What values can be held by the item
- How the item is stored in memory
- What operations can be performed on the item
Initializing Variables
- Initializing a variable means declaring a starting value.
- An unknown value is often called garbage.
- Examples:
- num mySalary
- num yourSalary = 14.55
- string myName
- string yourName = "Juanita"
Naming Variables
- Programmers should choose reasonable and descriptive names.
- The language translator (interpreter or compiler) associates names with memory addresses.
- Keywords are not allowed as variable names because they are part of the language syntax.
- Example names from the transcript: num, string (these appear as data types in examples, not as names per se).
Variable Naming Conventions (overview)
- Camel case (lower camel case): starts with a lowercase letter; subsequent words start with uppercase.
- Examples: hourlyWage, lastName
- Languages commonly using: Java, C# (Visual Basic may use other styles in some contexts)
- Pascal casing (upper camel case): first letter uppercase.
- Examples: HourlyWage, LastName
- Hungarian notation: a form where the variable’s data type is embedded in the identifier (a variation of camel case).
- Snake casing: words separated by underscores.
- Examples: hourlywage, lastname
- Mixed case with underscores: similar to snake casing but new words start with an uppercase letter.
- Kebob/case: uses dashes to separate parts (looks like skewers of words);
- Examples: hourly-name, last-name
- Language usage notes (from the table):
- Visual Basic: hourlyWage, hourly wage
- C for Windows API: stringLastName, last name
- C, C++, Python, Ruby: HourlyWage, LastName, hourly-wage, last-name
- Ada: HourlyWage
- Lisp (lowercase), COBOL (uppercase): HourlyWage / LastName
Camel Case in Java
- Camel Case visualization:
- Before capitalization: MyVariableName (example with capitalized words showing what the identifiers would look like in other naming styles)
- In Java-style camelCase: myVariableName, calculateTotalAmount, processUserData
Rules in Naming Variables
1) Variable names must be one word.
2) Variable names must start with a letter.
3) Variable names should have some appropriate meaning.
Assigning Values to Variables
- Assignment statement uses the = symbol as the assignment operator.
- Example:
- set myAnswer = myNumber * 2
- The assignment operator is a binary operator (needs two operands).
- It is right-associative (right-to-left): the evaluation groups from right to left.
Assignment Statements: Valid Examples
- Examples:
- set someNumber = 2
- set someNumber = 3 + 7
- set someOtherNumber = someNumber
- set someOtherNumber = someNumber * 5
lvalue and Assignment Details
- lvalue: the left-hand side of an assignment operator (the location being assigned to).
- Examples of invalid assignments include trying to assign to a non-variable expression:
- set 2 + 4 = someNumber (invalid)
- set someOtherNumber * 10 = someNumber (invalid)
- set someNumber + someOtherNumber = 10 (invalid)
Assignment Statements in Simpler Form
- Equivalent simpler syntax:
- someNumber = 2
- someOtherNumber = someNumber
More Examples of Assignment Statements
- Examples:
- valid taxRate = 2.5
- inventoryItem = "monitor"
- Invalid examples:
- taxRate = "2.5" (type mismatch: numeric vs string)
- inventoryItem = 2.5 (type mismatch)
- inventoryItem = taxRate (type mismatch unless types align)
- taxRate = inventoryItem (type mismatch)
Declaring Named Constants
- Named constant: similar to a variable but can be assigned a value only once.
- Example:
- num SALESTAXRATE = 0.06
- Example usage:
- taxAmount = price * 0.06
- taxAmount = price * SALESTAXRATE
Java Variable Declarations (examples)
- // Declaring an integer variable named 'age'
- int age;
- // Declaring and initializing an integer variable named 'score' and assign value 100
- int score = 100;
- // Declaring and initializing a double variable named 'temperature' and assign value 98.6
- double temperature = 98.6;
- // Declaring and initializing a character variable named 'X'
- char X; // note: in proper Java syntax this would be: char X = 'X';
Review Questions (from the transcript)
1) Which of the following is a valid declaration of an integer variable named count with an initial value of 10?
- a) int count = 10;
- b) count int = 10;
- c) integer count = 10;
- d) declare int count = 10;
- Answer: a) int count = 10; // The syntax is dataType variableName = value;
2) Which of the following is a valid variable name in Java?
- a) 1stName
- b) _lastName
- c) my-variable
- d) public
- Answer: _lastName
- Notes: Variable names can start with a letter, underscore (_) or dollar sign ($). They cannot start with a digit, and hyphens are not allowed. 'public' is a reserved keyword and cannot be used as a variable name.
3) Which data type would you use to store a single character, such as 'A' or 'Z'?
- Answer: the char data type
4) Declare a double variable named price and assign it the value 19.99.
- Answer: double price = 19.99;