Structured Programming
• Structured programming concepts focus on writing clear, logical programs by dividing
problems into manageable pieces and following a top-down approach.
• It is based on three basic control structures: Sequence, Selection, and Iteration.
1. Sequence
• A sequence is the simplest programming structure where instructions are executed one
after another in order.
• The program follows a top-to-bottom ow, without skipping or repeating steps.
• Execution Flow: Input → Process (Assignment) → Output.
• So, the most basic building block of structured programming is the Sequence structure,
which includes input, output, and assignment statements that are executed in a linear
order.
fl
#include <stdio.h>
int main()
int a, b, sum;
printf("Enter two numbers: "); // Output (prompt)
scanf("%d %d", &a, &b); // Input
sum = a + b; // Assignment
printf("Sum = %d\n", sum); // Output
return 0;
2. Selection:(Decision -Making)
• In C, programs can choose which part of the code to execute based on some condition.
• Selection refers to executing blocks of code based on the outcome of a condition.
• This ability is called decision making and the statements used for it are called conditional
statements.
• The if else in C is an extension of the if statement which not only allows the program to
execute one block of code if a condition is true, but also a different block if the condition
is false.
Types of Conditional statements in C
example: enter age of a student and print if he/she is eligible to vote or not.
3. Iteration (looping)
• There may be a situation when you need to execute a block of code several number of times.
LOOP TYPE DESCRIPTION
(i) For loop
• Used when the number of repetitions is known.
(ii) while loop
• Repeats a statement or group of statements until a given condition is true.
• It tests the condition before executing the loop body.
(iii) do…while loop
• Like a while statement, except that it tests the condition at the end of the loop body.
Nested loops:
You can use one or more loop inside any another while, for or do..while loop.
CONTROL STATEMENT DESCRIPTION
break statement
Terminates the loop or switch statement and transfers execution to the statement immediately
following the loop or switch.
Example:
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
Output:
continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.
Example:
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
Output:
10
Combining Break and Continue
Example:
int i;
for (i = 0; i < 6; i++) {
if (i == 2) {
continue;
}
if (i == 4) {
break;
}
printf("%d\n", i);
}
Output:
goto statement
Transfers control to the labelled statement. Though it is not advised to use goto statement in your
program.