BIT102 ยท 2080 ยท 10 marks
Explain different control structures in C. Write a program in C to perform different arithmetic operations on two integers and display the result based on choice made by user using switch statement.
Explain different control structures in C. Write a program in C to perform different arithmetic operations on two integers and display the result based on choice made by user using switch statement.[10]
Quick answer
Control structures determine the flow of execution of a program. C provides three main categories.
Control Structures in C and Switch-Based Arithmetic Calculator
Part 1: Control Structures in C
Control structures determine the flow of execution of a program. C provides three main categories:
1. Sequential Structure
Statements are executed one after another in the order they appear. This is the default flow.
Statement 1 โ Statement 2 โ Statement 3
2. Selection (Decision) Structures
Used to choose between alternative paths based on a condition.
a) if Statement
if (condition) {
// executes if condition is true
}
b) if-else Statement
if (condition) {
// executes if true
} else {
// executes if false
}
c) if-else if-else (Ladder)
if (condition1) { ... }
else if (condition2) { ... }
else { ... }
d) switch Statement
Used when a variable is compared against multiple constant values.
switch (expression) {
case value1: // statements; break;
case value2: // statements; break;
default: // statements;
}
3. Iteration (Loop) Structures
Used to repeat a block of statements.
a) for Loop
for (initialization; condition; update) {
// body
}
b) while Loop
while (condition) {
// body
}
c) do-while Loop
do {
// body
} while (condition);
Executes the body at least once before checking the condition.
4. Jump Structures
Used to transfer control unconditionally.
| Statement | Purpose |
|---|---|
break | Exits the nearest loop or switch |
continue | Skips the current iteration |
goto | Jumps to a labeled statement |
return | Exits a function |
Part 2: C Program Using Switch for Arithmetic Operations
#include <stdio.h>
int main() {
int a, b, choice;
float result;
// Input two integers
printf("Enter first integer: ");
scanf("%d", &a);
printf("Enter second integer: ");
scanf("%d", &b);
// Display menu
printf("\n--- Arithmetic Operations Menu ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
// Switch statement for operation selection
switch (choice) {
case 1:
result = a + b;
printf("Result: %d + %d = %.2f\n", a, b, result);
break;
case 2:
result = a - b;
printf("Result: %d - %d = %.2f\n", a, b, result);
break;
case 3:
result = a * b;
printf("Result: %d * %d = %.2f\n", a, b, result);
break;
case 4:
if (b == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
result = (float)a / b;
printf("Result: %d / %d = %.2f\n", a, b, result);
}
break;
case 5:
if (b == 0) {
printf("Error: Modulus by zero is not allowed.\n");
} else {
printf("Result: %d %% %d = %d\n", a, b, a % b);
}
break;
default:
printf("Invalid choice! Please enter a number between 1 and 5.\n");
}
return 0;
}
Sample Output
Enter first integer: 10
Enter second integer: 3
--- Arithmetic Operations Menu ---
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 4
Result: 10 / 3 = 3.33
Key Points About the Switch Statement Used
| Feature | Description |
|---|---|
switch(choice) | Evaluates the integer variable choice |
case n: | Matches a specific constant value |
break | Prevents fall-through to the next case |
default | Handles any value not matched by a case |
| Division check | Guards against division/modulus by zero |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.