Programming Fundamentals: Control Structures, Data Types, Variables, and Expression Evaluation

Fundamental Control Structures

  • Sequencing: A fundamental programming structure where all instructions are performed in order and executed one at a time. The computer executes the code sequentially, line by line, from the top to the bottom of the program.

  • Selection: A generic term for a type of programming statement that uses a Boolean condition to determine whether or not to select a specific behavior or execute a particular block of statements.

  • Iteration: A block of code that repeats instructions to achieve a goal, moving closer to a desired result or repeating until a specified condition is satisfied.

Data Types

Programming structures allow programs to store and manipulate various types of data:

  • Integers / Numbers:

    • Made up of digits from 00 to 99.

    • Can represent whole numbers or decimal numbers.

    • Can hold positive or negative values.

    • Written directly without quotation marks (e.g., 6.786.78 or 3.53.5).

  • Strings:

    • A sequence or string of characters, options, or full sentences joined together.

    • Always enclosed in single quotation marks '...' or double quotation marks "...".

  • Booleans:

    • A data type that can only hold one of two possible values: true or false.

    • Must be written without quotation marks.

Variables and Memory Concepts

  • Conceptual Analogy: Variables act like containers within a program.

  • Storage and Retrieval Mechanism:

    • Inside the container, specific information required by the program is stored.

    • To access or manipulate the stored information later in the program, it is called by its assigned variable name.

  • Variable Rules:

    • Variables can be named virtually anything permitted by the programming language.

    • Each variable can hold exactly one value at any given time.

  • Variable Declaration Examples:

    • Numeric Variables:

    • My Money set to E3.5E3.5

    • Value set to 6.786.78

    • String Variables:

    • Work set to "Hello"

    • Sentence set to "This is fake"

    • Boolean Variables:

    • Flag set to true

Operators and Expression Evaluation

  • Standard Operators: Standard programming languages include fundamental arithmetic operators for addition (++), subtraction (−-), multiplication (×\times), and division (//).

  • Specialized Operators:

    • Modulus (mod): Returns the remainder of a division operation.

    • Logical Operators: Used to evaluate Boolean conditions (AND, OR, NOT).

  • Order of Operations: Mathematical and logical expressions follow standard precedence rules (such as BODMAS/BIDMAS).

  • Assignment Execution Logic:

    • In any assignment statement, the expression on the right-hand side is always evaluated first.

    • Once the right side is fully calculated, the resulting value is stored into the variable specified on the left-hand side.

    • Overwrite Behavior: If the variable on the left already contains an existing value, that old value is dumped out (overwritten), and the newly calculated value takes its place.

String Concatenation

  • Concatenation Operator: The plus sign (++) functions as an operator when applied to strings.

  • Operation:

    • When the ++ operator appears between two strings, or between a string and a number, it smashes them together end-to-end to form a longer string.

  • Example:

    • "fizz" ++ "buzz" evaluates to "fizzbuzz".

Code Tracing and Expression Examples

Below are exact tracing scenarios, variable mutations, and evaluation statements:

  • Basic Variable Trace:

    • x=10x = 10

    • Y=x+5Y = x + 5 (evaluates to 1515)

    • Z=50Z = 50

    • I=1+5I = 1 + 5 (evaluates to 66)

    • I=1/5/3I = 1 / 5 / 3

  • Variable Mutation Sequence (MYNUM):

    • Initial state: MYNUM=2615MYNUM = 2615

    • Update statement: MYNUM=10+MYNUM×2MYNUM = 10 + MYNUM \times 2

    • Evaluation step: 10+(2615×2)=10+5230=524010 + (2615 \times 2) = 10 + 5230 = 5240

    • Final assignment: MYNUM=51MYNUM = 51

  • Complex Statements and Symbolic Traces:

    • A=Z−DA = Z - D

    • B=E×5B = E \times 5

    • DEN=1015DEN = 1015

    • BE=−27BE = -27

    • Fizz=E11Fizz = E11

    • buzz=B5buzz = B5

    • Arithmetic expression: 54/2211−98822.754 / 2211 - 98822.7

    • A=A+OA = A + O

    • x=120x = 120

    • A=Z−A+X−AA = Z - A + X - A

    • K=E×A×XK = E \times A \times X

    • Rational calculations:

    • 2201/20172201 / 2017

    • 10/4010 / 40

    • String variable reference: AXAX