Programming Basics and Branches Study Guide
Chapter 2: Programming Basics and Definitions
Programming Defined: Programming is the act of writing step-by-step instructions that dictate exactly what a computer must do.
Computational Logic: Unlike humans, a computer cannot guess or infer intent; every instruction must be perfectly clear and delivered in the specific, correct order.
Pseudocode: This serves as a rough draft for a program.
It is written in plain English rather than a formal programming language.
Purpose: It allows programmers to plan logic and steps without being hindered by complex syntax rules.
Variable Fundamentals: A variable functions like a labeled storage box.
Label: The name assigned to the variable.
Value: The specific information stored inside the box.
Example: In the expression , the variable named stores the numeric value .
Variable Declaration: This process creates the variable in computer memory. It informs the computer of the specific type of information the variable is permitted to store, such as an integer or a float.
Assignment Statements: These use the assignment operator () to store the value on the right-hand side into the variable located on the left-hand side.
Distinction from Mathematics: In programming, the symbol does NOT mean "is equal to." It is a command to store data.
Example 1: stores the value of inside the variable .
Example 2: takes the current value of , adds to it, and stores this new result back into the variable .
Expressions: An expression is any segment of code that evaluates to a single value.
Examples: , , or .
Identifiers and Naming Conventions
Identifiers: These are the names given to variables and functions. They must follow strict rules:
Must start with a letter.
May contain letters, digits (-), and underscores ().
Cannot contain any spaces.
Cannot be reserved words (words the programming language itself uses for commands).
Case-sensitivity: The computer treats uppercase and lowercase letters as distinct characters.
Naming Conventions: Programs should use meaningful names to clarify intent. Standard styles include:
Camel Case: The first word is lowercase and subsequent words start with a capital letter (e.g., ).
Underscore Style: Words are separated by underscores (e.g., ).
Operator Precedence and Development Strategies
Operator Precedence: When evaluating expressions, the computer follows a specific hierarchy of operations:
Parentheses:
Unary Minus:
Multiplication, Division, and Modulo: , ,
Addition and Subtraction: ,
Tie-Breaking: If operators have the same level of precedence, they are evaluated from left to right.
Incremental Development: This is the process of writing a small amount of code, testing it immediately, and fixing any errors before moving forward. This strategy simplifies the debugging process.
Data Types, Constants, and Functions
Core Data Types:
Integer: Used for counting whole numbers.
Float: Used for decimal numbers and measurements.
Character: Represents a single symbol.
String: Represents a sequence of text characters.
Boolean: Represents one of two states: or .
Array: A collection or list of values.
Functions: Reusable blocks of code designed to perform specific tasks. Common Coral functions include:
Random Numbers: The function returns a pseudo-random integer.
Pseudo-random: The numbers appear random but are actually produced using mathematical formulas.
SeedRandomNumbers(): This function ensures the same sequence of values is generated, which is crucial for testing purposes.
Type Conversion: The process of changing one data type into another.
Integer to Float: Adds a decimal point to the number.
Float to Integer: Truncates (removes) the decimal portion entirely.
Type Casting: This is an explicit command to force a data type conversion.
Constants: These store values that remain unchanged throughout the entire program execution. They are conventionally written in (e.g., or ).
Memory Tricks for Beginners:
Variable = Storage Box.
Function = Recipe.
Count = Integer.
Measure = Float.
Pseudocode = Rough Draft.
Chapter 3: Branches and Decisions
Branches: A branch allows the computer to choose between different execution paths based on a specific condition.
If the condition is , the computer follows one path.
If the condition is , the computer may follow a different path or skip the block entirely.
Branching Structures:
if: Used when there is only one decision point.
if-else: Used when there are exactly two mutually exclusive actions.
if-elseif: Used to check multiple conditions in a sequence. Only the first branch with a true condition will execute; all others in the chain are ignored.
Nested Branches: This occurs when an if statement is placed inside the body of another if statement.
Multiple if Statements: These are independent of one another. Unlike an if-elseif chain, multiple separate if statements can all execute if their individual conditions are true.
Booleans, Operators, and Logic Precautions
Boolean Data: A Boolean variable can only hold or . Meta-metaphor: Think of a light switch that is either ON or OFF.
Equality Operators:
: Means "equal to." Used to compare two values.
: Means "not equal to." Used to check if two values are different.
Warning: Never confuse (comparison) with (assignment). One checks for a match, while the other stores a value.
Relational Operators: Used to compare the relative size of two values:
< (Less than)
> (Greater than)
(Less than or equal to)
(Greater than or equal to)
Logical Operators:
AND: Requires both conditions involved to be for the whole expression to be .
OR: Requires at least one condition to be for the whole expression to be .
NOT: Reverses the Boolean value ( becomes ; becomes ).
Full Order of Precedence:
Parentheses
NOT
Arithmetic Operators
Relational Operators
Equality Operators ()
AND
OR
Floating-Point Comparisons: Avoid using the equality operator () with floating-point values. Because computer memory may not represent decimal numbers with exact precision, two seemingly identical floats might evaluate as not equal.
Memory Tricks for Logic:
Boolean = Light Switch.
AND = Both.
OR = Either.
NOT = Opposite.