← Back to quizzesFree quiz

Fundamentals of Structured Program Design

Structured program design is the foundation of clean, maintainable code. By mastering basic operators, precedence rules, and control structures, you gain the confidence to write programs…

10 questions~5 min
Fundamentals of Structured Program Design — Qwi
0 / 10
Score: 0%
1

What is the result of the expression `15 % 4` in most programming languages?

2

Given `int a = 5; int b = 3; int c = a % b;`, what value does `c` hold?

3

Which operator has higher precedence in the expression `2 + 3 * 4 - 5`?

4

If `x = 7` and the statement `x += 5` is executed, what is the new value of `x`?

5

Consider the Boolean expression `(a > 10) && (b < 5)`. Which of the following statements is true?

6

What does the unary operator `--` do when placed before a variable `n`?

7

Which of the following expressions correctly implements the quadratic formula `x = (-b ± sqrt(b^2 - 4ac)) / (2a)` in code?

8

In a `for` loop `for (int i = 1; i <= 5; i++)`, how many times will the loop body execute?

9

What is the output of the following code snippet? ``` int a = 8; int b = 6; int result = a / b; print(result); ```

10

Which control structure is best suited for executing a block of code only when a variable `score` is greater than 90?

Introduction to Structured Program Design

Structured program design is the foundation of clean, maintainable code. By mastering basic operators, precedence rules, and control structures, you gain the confidence to write programs that are both correct and easy to understand. This course walks through the essential concepts tested in a typical introductory quiz, providing clear explanations, memorable mnemonics, and practical examples.

Understanding the Modulus Operator (%)

What does % do?

The modulus operator returns the remainder after integer division. It is often used for tasks such as determining even/odd status, cycling through array indices, or limiting values within a range.

  • Expression: 15 % 4
  • Division: 15 ÷ 4 = 3 with a remainder of 3
  • Result: 15 % 4 == 3

Mnemonic: “Mod gives you the Minus Out Division” – subtract the largest multiple of the divisor.

Practical Example

Consider the code snippet:

int a = 5;
int b = 3;
int c = a % b; // c becomes 2

Because 5 ÷ 3 = 1 remainder 2, the variable c stores 2. For positive integers, the result always lies between 0 and b‑1.

Operator Precedence and Evaluation Order

Why precedence matters

When an expression contains several operators, the language decides which parts to evaluate first based on a predefined hierarchy called operator precedence. Ignoring precedence can lead to unexpected results.

In the expression 2 + 3 * 4 - 5, multiplication (*) has higher precedence than addition (+) and subtraction (‑). Therefore, the multiplication is performed first:

  • Step 1: 3 * 4 = 12
  • Step 2: 2 + 12 = 14
  • Step 3: 14 - 5 = 9

Mnemonic: “MDAS” – Multiply before Divide, then Add, then Subtract.

Using parentheses for clarity

Whenever you are unsure about the evaluation order, wrap the intended group in parentheses. This not only guarantees the correct calculation but also makes the code easier for others to read.

Compound Assignment Operators

What does += do?

The += operator adds the right‑hand operand to the variable and stores the result back into that variable. It is a shorthand for x = x + value.

Example:

int x = 7;
x += 5; // x becomes 12

After the statement, x holds 12. This operator reduces typing and improves readability, especially in loops or accumulators.

Mnemonic: “Plus‑Equals means add and keep the sum.”

Logical Operators: AND (&&)

Evaluating combined conditions

The logical AND operator (&&) returns true only when both operands are true. If either operand is false, the whole expression evaluates to false.

Consider the Boolean expression (a > 10) && (b < 5):

  • Both comparisons must be true for the entire expression to be true.
  • If a is 12 (true) but b is 7 (false), the result is false.

Mnemonic: **A**ND = **A**ll **N**eeded **D**one – both parts must be satisfied.

Unary Operators: Prefix Decrement (--)

How --n works

The prefix decrement operator (--) reduces a variable’s value by one **before** the variable’s value is used in the surrounding expression.

int n = 10;
int result = --n; // result is 9, n is now 9

This differs from the postfix form (n--), which returns the original value first and then decrements.

Mnemonic: “-- comes before the variable, so it pre‑decrements.”

Writing Complex Expressions: The Quadratic Formula

Translating mathematics to code

The quadratic formula is a classic example of an expression that requires careful use of parentheses and explicit multiplication:

x = (-b + sqrt(b*b - 4*a*c)) / (2*a);

Key points to remember:

  • Wrap the entire numerator in parentheses before the division.
  • Replace exponentiation with multiplication (b*b).
  • Enclose the denominator (2*a) in its own parentheses.

Mnemonic: (‑b + √… ) ÷ (2 × a) – “All the top, then the bottom.”

Control Structures: The for Loop

Counting iterations

A for loop repeats a block of code a specific number of times. The classic form for (int i = 1; i <= 5; i++) works as follows:

  • Initialization: i = 1
  • Condition check: loop continues while i <= 5
  • Increment: i++ adds 1 after each iteration

The values of i during execution are 1, 2, 3, 4, and 5, so the loop body runs five times.

Mnemonic: “Start at 1, stop at 5 – count the numbers: 1‑2‑3‑4‑5 = five steps.”

Summary of Core Concepts

  • Modulus (%) – returns the remainder after division.
  • Operator precedence – multiplication/division before addition/subtraction (MDAS).
  • Compound assignment (+=) – adds and stores the result in one step.
  • Logical AND (&&) – true only when all conditions are true.
  • Prefix decrement (--) – decreases a variable before its value is used.
  • Complex expressions – use parentheses to group numerator and denominator correctly.
  • For loops – count iterations based on initialization, condition, and increment.

By internalizing these fundamentals, you lay a solid groundwork for more advanced programming topics such as data structures, algorithms, and software design patterns.