Structured programming is a programming paradigm that emphasizes breaking down a program into smaller, manageable blocks of code. It encourages the use of control structures like if, if...else, while loops, and nested control statements to create readable, efficient, and error-free code. These control statements allow a program to make decisions, repeat actions, and manage complexity.
In C, structured program development often involves using control structures to determine the flow of execution. Let’s dive into the key control statements: if, if...else, while, and nested control structures.
if StatementThe if statement in C allows you to execute a block of code if a condition is true. If the condition is false, the code inside the if block is skipped.
if (condition) {
// Code to be executed if condition is true
}
#include <stdio.h>
int main() {
int number = 5;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
number > 0 is checked. If it is true (which it is), the message "The number is positive." is printed.The number is positive.
if...else StatementThe if...else statement is an extension of the if statement. It allows you to specify a block of code to be executed when the condition is false, in addition to the block executed when the condition is true.
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
#include <stdio.h>
int main() {
int number = -5;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative or zero.\n");
}
return 0;
}
number > 0 is false, so the program executes the code inside the else block and prints "The number is negative or zero.".The number is negative or zero.
while LoopThe while loop is used for repeating a block of code as long as the condition is true. If the condition is false initially, the code inside the loop is never executed.
while (condition) {
// Code to be executed as long as condition is true
}
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("i is %d\n", i);
i++; // Increment i by 1
}
return 0;
}
while loop runs as long as the condition i <= 5 is true.i is printed and then incremented.i becomes greater than 5, the condition is no longer true, and the loop terminates.i is 1
i is 2
i is 3
i is 4
i is 5
A nested control statement is when you place one control statement (like an if, while, or for loop) inside another control statement. This allows for more complex decision-making and logic flow in your programs.
if Statement:#include <stdio.h>
int main() {
int x = 10, y = 20;
if (x > 0) {
if (y > 0) {
printf("Both x and y are positive.\n");
} else {
printf("x is positive, but y is not.\n");
}
} else {
printf("x is not positive.\n");
}
return 0;
}
if checks if x > 0.if, there is another if statement that checks if y > 0. This creates a nested structure."Both x and y are positive.".Both x and y are positive.
while Loop:#include <stdio.h>
int main() {
int i = 1, j;
while (i <= 3) {
j = 1;
while (j <= 3) {
printf("i = %d, j = %d\n", i, j);
j++; // Increment j
}
i++; // Increment i
}
return 0;
}
while loop iterates with i from 1 to 3.i, the inner while loop iterates with j from 1 to 3.i and j.i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
if...else, while, and Nested Control StatementsLet’s combine if...else, while loops, and nested control statements to solve a simple problem. We will create a program to check if a number is prime.
A prime number is a number greater than 1 that has no divisors other than 1 and itself. The program will check whether a number is prime or not.
#include <stdio.h>
int main() {
int number, i, is_prime = 1;
printf("Enter a number: ");
scanf("%d", &number);
if (number <= 1) {
printf("Number must be greater than 1 to be prime.\n");
} else {
i = 2;
while (i < number) {
if (number % i == 0) {
is_prime = 0; // Set to 0 if divisible
break; // Exit the loop as no need to check further
}
i++;
}
if (is_prime == 1) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
}
return 0;
}
while loop checks for divisibility from i = 2 to number - 1.is_prime is set to 0, and the loop breaks early.number = 7):Enter a number: 7
7 is a prime number.
number = 10):Enter a number: 10
10 is not a prime number.
if Statement: Executes a block of code if the condition is true.if...else Statement: Executes one block of code if the condition is true and another block if it is false.while Loop: Repeats a block of code as long as the condition is true.By using these control statements effectively, you can create structured and logical programs that handle various scenarios, repeat tasks, and make decisions based on conditions.
Open this section to load past papers