UNIT NAME: STRUCTURED PROGRAMMING
UNIT CODE: CS/C/7154
DATE OF SUBMISSION:05-02-2025
GROUP 3 MEMBERS:
KELVIN THAMU KANIA - 2025CS161668
LANGAT ERICK - 2025CS161367
KIPROP KORIR - 2025CS162222
FAITH CHEPCHIRCHIR- 2025CS162357
FAITH NJERI MWATHI - 2025CS161465
KIPKOECH ENOCK- 2025CS162754
1.Definition of Control Structure in C
A control structure in C is a fundamental concept that determines the flow of
execution of statements in a program. It allows programmers to control the order
in which instructions are executed based on conditions, loops, or jumps.
Types of Control Structures in C
Control structures in C are broadly classified into four categories:
1)Selection (Decision-Making) Control Structure
Allows the program to make decisions and execute different blocks of code based
on conditions.
Example:
if (condition) {
// Code executes if condition is true
} else {
// Code executes if condition is false
Types of Decision/Selection Control Structures in C
1. if Statement
The if statement evaluates a condition and executes a block of code only if the
condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
Example:
#include <stdio.h>
int main() {
int age;
printf("please enter your age?" );
scanf("%d " , &age);
if (age > 18){
printf("you are eligible for voting");
return 0;
2. if-else Statement
The if-else statement provides two possible execution paths: one if the condition
is true and another if it is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
Example:
#include <stdio.h>
int main() {
int age;
printf("please enter your age?\n" );
scanf("%d " , &age \n);
if (age > 18){
printf("you are eligible for voting. \n");
else {
printf("you are not eligible for voting");
return 0;
3. if-else if-else Statement
This structure allows multiple conditions to be checked sequentially.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the above conditions are true}
Example:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: \n ");
scanf("%d", &age);
// Checking age category
if (age < 13) {
printf("You are a child.\n");
} else if (age >= 13 && age <= 19) {
printf("You are a teenager.\n");
} else {
printf("You are an adult.\n");
return 0;
}
Example 2
#include <stdio.h>
int main() {
int age;
printf("please enter your age?" );
scanf("%d " , &age);
if (age >18){
printf("you are eligible for voting");
else if(age <18){
printf("you are not eligible for voting");
else {
printf("Try another time 2027");
}
return 0;
4. Nested if Statement
An if statement inside another if statement is called a nested if.
Syntax:
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
Example:
#include <stdio.h>
int main() {
int num = 15;
if (num > 0) {
if (num % 2 == 0) {
printf("Number is positive and even.\n");
} else {
printf("Number is positive and odd.\n");
}
}
return 0;
#include <stdio.h>
int main() {
int age = 8;
int income = 40000;
// First check: Is the person an adult?
if (age >= 18) {
// Second check: Is the income above a certain threshold?
if (income >= 30000) {
printf("You are an adult with a good income.\n");
} else {
printf("You are an adult but do not have a good income.\n");
} else {
printf("You are a minor.\n");
}
return 0;
5. switch Statement
The switch statement is used when multiple conditions are based on a single
variable.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if no case matches
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
return 0;
Example 2
#include <stdio.h>
int main() {
int day;
// Taking user input
printf("Enter a number (1-7) for the day of the week: ");
scanf("%d", &day);
// Switch statement to determine the day
switch (day) {
case 1:
printf("Monday - It's a weekday.\n");
break;
case 2:
printf("Tuesday - It's a weekday.\n");
break;
case 3:
printf("Wednesday - It's a weekday.\n");
break;
case 4:
printf("Thursday - It's a weekday.\n");
break;
case 5:
printf("Friday - It's a weekday.\n");
break;
case 6:
printf("Saturday - It's a weekend!\n");
break;
case 7:
printf("Sunday - It's a weekend!\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
return 0;
2)Sequential Control Structure
The default flow where statements execute one after another in the order they
appear.
When to Use Sequential Control Structures?
When there is no need for conditions (if, switch).
When there is no need for loops (for, while).
For simple operations like printing, calculations , and assignments.
Characteristics of Sequential Control Structure
Executes statements line by line in a sequential order.
No decision-making (if, switch).
No looping (for, while, do-while).
No jumps (break, continue, goto).
Example of Sequential Control Structure
#include <stdio.h>
int main() {
int a = 10; // Step 1: Declare and initialize a variable
int b = 20; // Step 2: Declare and initialize another variable
int sum = a + b; // Step 3: Perform addition
printf("The sum is: %d\n", sum); // Step 4: Print the result
return 0; // Step 5: End program
Example:
#include <stdio.h>
int main() {
printf("Step 1\n");
printf("Step 2\n");
return 0;
Breakdown of the Sequential Flow in the Example
1. The program starts execution from main().
2. Variables are declared: num1, num2, and sum.
3. Message is printed using printf().
4. Addition operation is performed: sum = num1 + num2.
5. Result is printed using printf().
6. Program exits.
3)Iteration (Looping) Control Structure
Used to repeat a block of code multiple times based on a condition.
Example:
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
4)Jump Control Structure
Jump control structures in C allow the program to transfer control from one part
of the code to another. The main jump statements in C are:
i)Break – Exits a loop or switch statement.
#include <stdio.h>
int main(){
int i;
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exits the loop when i is 3
printf("%d ", i);
return 0;
ii)Continue – Skips the current iteration of a loop and moves to the next iteration.
Example
#include<stdio.h>
int main(){
int i;
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips the iteration when i is 3
printf("%d ", i);
iii)Goto – Transfers control to a labeled statement.
Example
#include <stdio.h>
int main() {
int i = 1;
start: // Label
printf("%d\n", i);
i++;
if (i <= 5) {
goto start; // Jumps back to 'start' label
return 0;