Published by Patrick Mutisya · 14 days ago
By the end of this lesson you will be able to use logical statements (AND, OR, NOT, IF‑THEN, etc.) to clearly define the input, processing, and output sections of an algorithm solution.
if, while, and for loops.| Symbol | English Description | Typical Use in Pseudocode |
|---|---|---|
| \$\cdot\$ | AND – both conditions must be true | IF \$A \cdot B\$ THEN … |
| \$+\$ | OR – at least one condition is true | IF \$A + B\$ THEN … |
| \$\lnot\$ | NOT – negates a condition | IF \$\overline{A}\$ THEN … |
| \$\rightarrow\$ | IF‑THEN – implication | \$A \rightarrow B\$ is read “if \$A\$ then \$B\$” |
| \$\leftrightarrow\$ | IF AND ONLY IF – bi‑conditional | \$A \leftrightarrow B\$ means \$A\$ and \$B\$ have the same truth value |
Identify the data required and any pre‑conditions using logical statements.
Example: “The algorithm requires a positive integer \$n\$ such that \$n > 0\$.”
Use logical expressions to control the flow of operations.
Typical structures:
State the post‑conditions that must hold when the algorithm terminates.
Example: “The algorithm outputs the factorial \$f\$ such that \$f = n!\$ and \$f > 0\$.”
We will define the algorithm using logical statements for each part.
Given two positive integers \$a\$ and \$b\$, find their greatest common divisor \$g\$.
1. INPUT a, b
2. PRE‑CONDITION: \$a > 0 \cdot b > 0\$
3. WHILE \$b \neq 0\$ DO
4. \$temp \leftarrow b\$
5. \$b \leftarrow a \bmod b\$
6. \$a \leftarrow temp\$
7. END WHILE
8. OUTPUT \$a\$ // \$a\$ now holds the GCD
9. POST‑CONDITION: \$a = \gcd(original\a, original\b)\$
\$\forall k \ge 0,\; \gcd(ak, bk) = \gcd(original\a, original\b)\$
where \$ak\$ and \$bk\$ are the values of \$a\$ and \$b\$ after \$k\$ iterations.
Write an algorithm to determine whether a given year \$y\$ is a leap year. Use logical statements to define the input, processing, and output sections.
\$\text{Leap} \leftrightarrow (y \bmod 4 = 0) \cdot \bigl((y \bmod 100 \neq 0) + (y \bmod 400 = 0)\bigr)\$