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 to .
Can represent whole numbers or decimal numbers.
Can hold positive or negative values.
Written directly without quotation marks (e.g., or ).
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:
trueorfalse.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 Moneyset toValueset toString Variables:
Workset to"Hello"Sentenceset to"This is fake"Boolean Variables:
Flagset totrue
Operators and Expression Evaluation
Standard Operators: Standard programming languages include fundamental arithmetic operators for addition (), subtraction (), multiplication (), 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:
(evaluates to )
(evaluates to )
Variable Mutation Sequence (
MYNUM):Initial state:
Update statement:
Evaluation step:
Final assignment:
Complex Statements and Symbolic Traces:
Arithmetic expression:
Rational calculations:
String variable reference: