Statements and Control Flow

12 min read

Statements

A statement in a programming language is a syntactic unit that expresses an action to be carried out by the computer. It is the smallest independent element of a program, functioning much like a complete sentence in a natural language. When a program runs, the execution flow moves from one statement to the next, commanding the system to perform specific operations.

Statements are generally categorized based on the type of action they perform. Assignment statements allocate data to variables, while control flow statements—such as loops and conditionals—determine the order in which other statements execute. There are also declaration statements that introduce new variables or functions to the program, and expression statements that evaluate a value and may trigger side effects.

totalCost = price + salesTax          # Assignment statement
if age >= 18 then print("Access Granted")  # Conditional statement
for i in 1 to 10 do print(i)          # Loop statement
sqrt(16)                              # Expression statement

Expressions

An expression in a programming language is a combination of values, variables, operators, and function calls that the language interprets and evaluates to produce a single value. Unlike a statement, which executes an action or a command, an expression is fundamentally centered around computation. Every expression must result in a value, which can then be assigned to a variable, passed to a function, or used within a larger structure.

Expressions are built using operands, which are the data objects being manipulated, and operators, which dictate the manipulation itself. For instance, combining two numbers with an addition operator forms an arithmetic expression. Expressions can also be relational, comparing two values to yield a boolean result, or logical, combining multiple conditions to determine an ultimate truth value.

price + salesTax  # Arithmetic expression evaluating to a value
age >= 18         # Relational expression evaluating to a boolean value

Conditional Statements

A conditional statement is a programming construct that allows a program to make decisions and execute different blocks of code based on whether a specific condition is true or false. It acts as a gatekeeper for program flow, ensuring that certain actions only occur under the right circumstances. The core of any conditional statement is a boolean expression, which the computer evaluates to determine the path of execution.

The most common form of this construct is the if-then-else structure. If the initial condition evaluates to true, the program executes the code immediately following the conditional check. If the condition evaluates to false, the program skips that code entirely and either moves forward or executes an alternative block of code defined by an else clause. Developers can also chain multiple conditions together using else-if clauses to handle complex, multi-layered scenarios.

score = 85
if score >= 90 then
    print("A")
else if score >= 80 then
    print("B")
else if score >= 70 then
    print("C")
else
    print("F")
end if

Another widespread type of conditional statement is the switch or case statement, which compares a single variable against a list of pre-defined values. This structure is often used as a cleaner, more readable alternative to a long sequence of individual if-else checks when testing a single expression for multiple distinct outcomes.

day = 2
switch day:
    case 1:
        output "Monday"
    case 2:
        output "Tuesday"
    case 3:
        output "Wednesday"
    case 4:
        output "Thursday"
    case 5:
        output "Friday"
    case 6:
        output "Saturday"
    case 7:
        output "Sunday"
    default:
        output "Invalid day"
end switch

Loop Statements

A loop statement is a programming construct that repeatedly executes a specific block of code as long as a specified condition remains true. Instead of writing the same lines of code multiple times, programmers use loops to automate repetitive tasks efficiently. Each repetition of the loop is known as an iteration, and the loop continues until its controlling condition evaluates to false, at which point the program moves on to the next instruction.

There are several primary types of loop statements used across programming languages:

  • while loop checks a boolean condition before executing its code block, meaning the block might not run at all if the initial condition is false.
  • do-while loop executes the code block first and then checks the condition, guaranteeing that the code runs at least once.
  • for loop is typically used when the exact number of iterations is known beforehand, as it neatly bundles initialization, condition testing, and incrementation into a single line.
# while loop
count = 0
while count < 5:
    print(count)
    count = count + 1
end while
# Output: 0, 1, 2, 3, 4

# do-while loop
count = 0
do:
    print(count)
    count = count + 1
while count < 5
# Output: 0, 1, 2, 3, 4

# for loop
for count in 0 to 4:
    print(count)
end for
# Output: 0, 1, 2, 3, 4

Programmers often utilize specific control words, like break and continue, to alter loop behavior on the fly. A break statement terminates the loop entirely and exits immediately, while a continue statement skips the remainder of the current iteration and jumps directly to the next condition check.

for count in 0 to 5:
    if count == 3:
        break  # Exit the loop when count is 3
    print(count)
end for
# Output: 0, 1, 2

for count in 0 to 5:
    if count == 3:
        continue  # Skip the rest of the current iteration when count is 3
    print(count)
end for
# Output: 0, 1, 2, 4, 5

Review

### What is the smallest independent element of a programming language that expresses an action? > Think of it as the equivalent of a complete sentence in a natural language. 1. [x] A statement 1. [ ] An expression 1. [ ] An operand 1. [ ] An operator ### Which type of statement allocates data to variables? > This category specifically handles assigning values to identifiers. 1. [ ] A control flow statement 1. [x] An assignment statement 1. [ ] A declaration statement 1. [ ] An expression statement ### What distinguishes an expression from a statement? > Focus on what the code construct fundamentally produces or evaluates. 1. [ ] An expression cannot contain variables or functions. 1. [ ] An expression alters the sequence of code execution. 1. [ ] An expression does not perform any computation. 1. [x] An expression evaluates to a single value. ### What are the components that form an expression by being manipulated and dictating the manipulation? > They consist of the data objects themselves and the symbols representing the operations. 1. [ ] Statements and conditionals 1. [ ] Iterations and loops 1. [x] Operands and operators 1. [ ] Clauses and conditions ### Which type of expression compares two values and evaluates to a boolean result? > This type of expression specifically checks how values relate to each other. 1. [ ] An assignment statement 1. [ ] An arithmetic expression 1. [x] A relational expression 1. [ ] A neutral expression ### Which type of expression does a conditional statement rely on to make a decision? > It acts as a gatekeeper and must evaluate to either true or false. 1. [ ] An assignment statement 1. [ ] An iteration count 1. [x] A boolean expression 1. [ ] An increment operator ### Which construct can programmers use to chain multiple conditions together? > This clause sits between the initial check and the final fallback. 1. [ ] switch statements 1. [x] else-if clauses 1. [ ] break keywords 1. [ ] do-while blocks ### Which loop checks its boolean condition prior to executing its code block? > The block in this loop might not execute at all if the initial check is false. 1. [x] A while loop 1. [ ] A continue loop 1. [ ] A break loop 1. [ ] An assignment loop ### What is a single repetition of a loop structure called? > This term describes each time the code block is executed again. 1. [ ] A statement 1. [ ] An operand 1. [x] An iteration 1. [ ] A clause ### Which control word terminates a loop entirely and exits it immediately? > This keyword is used to alter loop behavior on the fly by stopping it. 1. [ ] continue 1. [ ] else 1. [ ] switch 1. [x] break