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 score=100score = 100, the variable named scorescore stores the numeric value 100100.

  • 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: apples=8apples = 8 stores the value of 88 inside the variable applesapples.

    • Example 2: score=score+10score = score + 10 takes the current value of scorescore, adds 1010 to it, and stores this new result back into the variable scorescore.

  • Expressions: An expression is any segment of code that evaluates to a single value.

    • Examples: x+1x + 1, 5×45 \times 4, or price×quantity\text{price} \times \text{quantity}.

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 (00-99), 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., totalScoretotalScore).

    • Underscore Style: Words are separated by underscores (e.g., total_scoretotal\_score).

Operator Precedence and Development Strategies

  • Operator Precedence: When evaluating expressions, the computer follows a specific hierarchy of operations:

    1. Parentheses: ()()

    2. Unary Minus: -

    3. Multiplication, Division, and Modulo: ×\times, //, %\%%

    4. 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: truetrue or falsefalse.

    • Array: A collection or list of values.

  • Functions: Reusable blocks of code designed to perform specific tasks. Common Coral functions include:

    • SquareRoot()SquareRoot()

    • RaiseToPower()RaiseToPower()

    • AbsoluteValue()AbsoluteValue()

    • RandomNumber()RandomNumber()

  • Random Numbers: The function RandomNumber(low,high)RandomNumber(\text{low}, \text{high}) 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 ALL_CAPSALL\_CAPS (e.g., PIPI or MAX_SPEEDMAX\_SPEED).

  • 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 truetrue, the computer follows one path.

    • If the condition is falsefalse, 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 truetrue or falsefalse. 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)

    • \le (Less than or equal to)

    • \ge (Greater than or equal to)

  • Logical Operators:

    • AND: Requires both conditions involved to be truetrue for the whole expression to be truetrue.

    • OR: Requires at least one condition to be truetrue for the whole expression to be truetrue.

    • NOT: Reverses the Boolean value (truetrue becomes falsefalse; falsefalse becomes truetrue).

  • Full Order of Precedence:

    1. Parentheses

    2. NOT

    3. Arithmetic Operators

    4. Relational Operators

    5. Equality Operators (==,!===, !=)

    6. AND

    7. 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.