A recipe lists its steps in the order in which they must be done. Chopping comes before frying, and serving comes last. A program is similar: unless told otherwise, the computer carries out instructions one after another, from top to bottom.
Structured Programming
Definition: Structured programming. A method of writing programs using only three control structures, namely sequence, selection and repetition, each having one entry point and one exit point.
Any algorithm, however large, can be built from these three structures. Because each structure has a single entry and a single exit, a structured program can be read from top to bottom and understood one block at a time.
Sequence
Definition: Sequence. A control structure in which instructions are carried out one after another in the order in which they are written.
Most simple programs follow the pattern input, processing, output. Data is read first, calculations are performed next, and results are displayed last. The order matters, because a later step can use only the values produced by earlier steps.
The Three Basic Statements
A sequence is made of three kinds of statement, summarized in Table 2.1.
An input statement reads a value from the user and stores it in a variable. An output statement displays a value or a message. An assignment statement calculates a value and stores it in a variable.
Table 2.1: Basic statements in pseudocode and in C
| Statement | Pseudocode | C |
|---|---|---|
| Input | Read price | scanf("%f", &price); |
| Assignment | total = price * qty | total = price * qty; |
| Output | Display total | printf("%f", total); |
Assignment Is Not Equality
In an assignment such as total = price * qty, the expression on the right is calculated first, and the result is stored in the variable on the left. The statement count = count + 1 is therefore meaningful: it takes the present value of count, adds one and stores the result back in count. As a statement of equality in mathematics it would be false, but as an assignment it is an instruction to update a value.
Example 2.1: Computing a bill amount
Problem: Read the price of an item and the quantity bought, and display the total amount.
Solution:
- Read the two inputs before any calculation.
- Multiply price by quantity and store the result in total.
- Display total.
#include <stdio.h>
int main(void)
{
float price, total;
int qty;
printf("Enter price and quantity: ");
scanf("%f %d", &price, &qty);
total = price * qty;
printf("\nTotal = %.2f\n", total);
return 0;
}
Input:
12.50 4
Output:
Enter price and quantity:
Total = 50.00
Result: For a price of 12.50 and a quantity of 4, the total is 50.00. If the multiplication were placed before the input statement, the variables would not yet hold the entered values.
Common mistakes
- Using a variable in a calculation before a value has been read into it.
- Reading
x = x + 1as an equation instead of an instruction to update x. - Displaying a result before the statement that calculates it.
Key takeaways
- Structured programs use only sequence, selection and repetition.
- In a sequence, statements run in the order written.
- The usual order of a simple program is input, processing, output.
- Assignment evaluates the right-hand side and stores it in the left-hand variable.
Practice questions
- Name the three control structures of structured programming.
- What does the statement count = count + 1 do?
- Why must input statements come before the calculation that uses the values?
Answers to the practice questions
- Sequence, selection and repetition.
- It adds one to the present value of count and stores the result back in count.
- A calculation can use only values that already exist; before input, the variables do not hold the entered values.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II
