Introduction to Structured Programming
A paradigm known as “structured programming” encourages a methodical and organised approach to computer program design. It is nearly synonymous with “goto elimination” in its most basic form and aids in creating programs that are easy to understand and maintain. Structured programs are more likely to be free of bugs and are simpler to debug and alter.
Any computing problem can be solved by carrying out a sequence of steps in a particular order. We refer to this method of planned solution as an algorithm. An algorithm is a process for resolving an issue that specifies the steps to take and the sequence in which they should be taken. An informal, artificial language called pseudocode can be used to aid in the development of algorithms.
Basic programming building components called control structures dictate the sequence in which statements are executed. Sequential execution is the process by which C language statements are typically run one after the other in the order that they are written. Transfer of control is the ability to express that the next statement to execute may not be the next one in sequence using a variety of C statements.
Böhm and Jacopini, computer scientists, showed that only three basic control structures are needed to write any program:
- Sequence: In the order that they occur, statements are performed one after the other. The computer automatically runs C commands sequentially until instructed otherwise.
- Selection: Based on a circumstance, this structure enables the software to decide which actions to carry out. C offers a number of selection statements:
- The if statement (single selection).
- The if else statement (double selection).
- The switch statement (multiple selection).
- A straightforward if-else statement can be substituted with the conditional operator (?:).
- Iteration: This structure enables the repeated execution of a set of statements. Looping is another name for iteration. A loop is a set of commands that the computer repeatedly runs as long as a certain loop-continuation condition is met. Three iteration statements are provided by C:
- The while Statement.
- The do-while Statement.
- The for statement. There are two types of iteration: sentinel-controlled, which executes until a particular sentinel value indicates the end of data, and counter-controlled, which counts iterations using a variable.
Complex programs can be created by combining these control statements. These statements are combined in structured programming via nesting (putting one within another) or stacking (arranging them in a certain order).
The employment of sequence, selection (if else), and iteration (while) control structures is illustrated in the following C code example:
#include <stdio.h> // Include standard input/output library
int main(void) { // Main function where program execution begins
int number; // Declare an integer variable
// Sequence: Statements are executed one after the other
printf("Enter an integer (enter 0 to quit): "); // Prompt the user for input
scanf("%d", &number); // Read an integer from the user
// Iteration: The while loop executes repeatedly as long as the condition (number != 0) is true
while (number != 0)
{
// Selection: The if else statement checks a condition and executes code based on whether it's true or false
if (number > 0) { // Condition: Is number greater than 0?
printf("You entered a positive number.\n"); // Sequence: This statement executes if the condition is true
} else { // If the condition (number > 0) is false, the code in the else block executes
printf("You entered a non-positive number.\n"); // Sequence: This statement executes if the condition is false
} // End of if...else selection structure
// Sequence: These statements execute after the selection block within each iteration of the loop
printf("Enter another integer (enter 0 to quit): ");
scanf("%d", &number); // Read the next integer from the user
} // End of while loop. When number is 0, the loop condition (number != 0) becomes false, and execution continues after the loop
// Sequence: This statement executes after the loop terminates
printf("Exiting program.\n");
return 0; // Sequence: Indicates successful program termination
} // End of main function
An explanation of the example code
- Sequential execution is how the software starts: it reads the first integer and asks the user.
- After that, it moves into an iteration control structure called a while loop. As long as the user does not enter the sentinel value 0, this loop will continue.
- An if else statement is used for selection inside the loop. It determines whether the number entered is positive.
- One of two printf commands is run depending on the if condition. Examples of sequence in the selection branches are these printf statements.
- The program proceeds sequentially within the loop after the if else block, asking for and reading the subsequent number.
- After that, the loop repeats the block of code (which contains selection and sequence) until the sentinel value is entered, checking the condition once more.
The software prints the “Exiting” message and ends when the loop condition is false and it resumes the sequence after the loop.
This example shows how these three basic control structures work together to produce a program that can make decisions and flow logically.