A program is the translation of an algorithm. Before writing code, the problem must be described as an ordered sequence of steps. This lab covers representing algorithms and the first category of statements that change the order of execution: decision statements.
1Lab objectives
- Describing an algorithm through a flowchart and through pseudocode
- Telling expression statements apart from compound ones
- Correctly using the
ifandif-elsestatements - Resolving the
elseambiguity by using braces - Applying the
switchstatement and understanding the role ofbreak
2Algorithms and ways to represent them
An algorithm is a finite sequence of actions that, applied to input data, lead to the desired result. Describing it is an intermediate stage between the mathematical formulation and the program written in a concrete language.
| Method | Advantages | Disadvantages |
|---|---|---|
| Natural language | needs no special training | ambiguous for complex problems |
| Flowchart | visually clear, easy to follow | becomes hard to draw for large algorithms |
| Pseudocode | concise, close to the final code | not standardized |
| Decision tables | ideal for many combined alternatives | unsuited to sequential algorithms |
3Flowchart symbols
4Types of statements
| Type | Form | Notes |
|---|---|---|
| Expression statement | expression; | any expression followed by ; becomes a statement |
| Empty statement | ; | does nothing; useful where the syntax requires a statement |
| Compound statement (block) | { ... } | groups several statements; can contain local declarations |
| Decision statements | if, if-else, switch | choose a branch of execution |
| Repetition statements | while, do-while, for | covered in Laboratory 6 |
| Jump statements | break, continue, return, goto | interrupt the normal flow |
5The if statement
if (expression)
statement; // simple form
if (expression)
statement_1;
else
statement_2; // form with an alternative
if (expression_1) {
...
} else if (expression_2) {
...
} else {
... // cascading alternatives
}
if does not
have to be a comparison. Any nonzero value is considered true, and zero is false. That
is why if (n) is equivalent to if (n != 0).6Decision simulator
Watch step by step how the conditions in an if-else if cascade
are evaluated, and why the order of the tests matters.
grade < 9 seems to also
accept grades under 5, but those cases were already handled by the earlier conditions. In a
cascade, every branch implicitly assumes all the ones before it were false.7The switch statement
switch (expression) {
case c1:
statements;
break;
case c2:
statements;
break;
default:
statements;
}
| Aspect | Rule |
|---|---|
| The expression's type | must be integer or character - not float, not a string |
case labels | must be constants, distinct from each other |
break | stops execution; without it, execution continues into the next case |
default | optional; runs if no case matches |
break makes execution continue into the following cases, regardless of their
value. Sometimes this is intentional - to group several values that get the same treatment -
but most often it is an oversight.8Common mistakes
if (a > b); is syntactically correct: the body of if becomes the
empty statement, and the following block executes always. The compiler flags nothing.else is always associated with the nearest unassociated if,
regardless of indentation. Indentation fools the eye, not the compiler. Always use braces.if (0.1 + 0.2 == 0.3) is false, because of the approximate binary
representation. Correct: if (fabs(a - b) < 1e-9).if (x = 0) assigns and is always false; if (x = 5) assigns and is
always true. Some programmers write if (5 == x) precisely so that the mistake
becomes a compile error.9Source code
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, delta, x1, x2;
printf("Coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
if (b == 0) {
if (c == 0) printf("Identity: any x is a solution\n");
else printf("Impossible equation\n");
} else {
printf("First-degree equation: x = %.4f\n", -c / b);
}
} else {
delta = b * b - 4 * a * c;
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("Two real roots: %.4f and %.4f\n", x1, x2);
} else if (delta == 0) {
printf("Double root: %.4f\n", -b / (2 * a));
} else {
printf("Complex roots: %.4f +/- %.4fi\n",
-b / (2 * a), sqrt(-delta) / (2 * a));
}
}
return 0;
}
#include <stdio.h>
int main(void)
{
double a, b;
char op;
printf("Expression (e.g. 12 * 4): ");
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
case '+':
printf("%.2f\n", a + b);
break;
case '-':
printf("%.2f\n", a - b);
break;
case '*':
case 'x': // intentional grouping: * and x do the same thing
case 'X':
printf("%.2f\n", a * b);
break;
case '/':
if (b == 0) printf("Division by zero!\n");
else printf("%.2f\n", a / b);
break;
default:
printf("Unknown operator: %c\n", op);
}
return 0;
}
#include <stdio.h>
int main(void)
{
int a = 1, b = 2;
// WRONG: the indentation suggests something other than what the compiler does
if (a > 0)
if (b > 5)
printf("b is large\n");
else // binds to the SECOND if, not the first!
printf("This does NOT mean a <= 0\n");
// CORRECT: the braces remove any ambiguity
if (a > 0) {
if (b > 5) {
printf("b is large\n");
}
} else {
printf("a <= 0\n");
}
return 0;
}
10Code workshop
Decisions are best understood by tracing them step by step. Run the programs, then change the input data to force each branch.
1 -4 4 (double root), then
to 1 1 5 (complex roots), and to 0 2 6 (first degree). Each set of
values sends execution down a different branch.11Work tasks
- Draw the flowchart for finding the maximum of three numbers, then implement it.
- Write the complete program for solving the quadratic equation, handling every special case.
- Implement a calculator with
switch, which also handles division by zero. - Reproduce the mistake with
;afterifand explain the observed behavior. - Run the example with the
elseambiguity and check whichifit binds to. - Test
if (0.1 + 0.2 == 0.3)and fix the comparison using a tolerance. - Write a program that prints the number of days in a month, using
switchwith grouped cases.
12Extended application
switch to print the day's name.