C Programming ยท Unit 4
Control Structures
Exam-focused notes for Control Structures (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Control Structures in short
Control structures decide which statements run and how many times. They let a program make choices and follow different paths based on data.
What this unit covers
- If else statement
- If else ladder
- Nested if else
- Switch case statement
- Comparison of switch and if else ladder
- Break and continue statements
- Goto statement and labels
- Flowchart design
If else ladder
2081 ยท 5 marks
Explain if else ladder with syntax and semantic. Write a program to find largest among 3 different numbers. [2+3]
Switch case statement
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.[10]
Show solved answerHide answer
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 |
Nested if else
2080 ยท 5 marks
Explain nested if else ladder with suitable example. [5]
Flowchart design
2079 ยท 5 marks
What do you mean by flowchart? Draw a Flowchart to find the largest among three numbers entered by user. [5]
Show solved answerHide answer
Flowchart
Definition
A flowchart is a diagrammatic or pictorial representation of an algorithm or a step-by-step solution to a given problem. It uses standard geometric symbols connected by arrows (flow lines) to show the sequence of steps, decisions, and operations required to solve a problem.
Common Flowchart Symbols
| Symbol | Shape | Purpose |
|---|---|---|
| Terminal | Oval / Rounded Rectangle | Start / Stop |
| Input/Output | Parallelogram | Read / Print data |
| Process | Rectangle | Arithmetic / Assignment operations |
| Decision | Diamond | Yes/No or True/False condition |
| Flow Lines | Arrows | Direction of flow |
Flowchart to Find the Largest Among Three Numbers
Problem: Read three numbers A, B, and C from the user and find the largest among them.
+----------+
| START |
+----------+
|
v
+------------------+
| Read A, B, C |
+------------------+
|
v
+------------+
| Is A > B? |
+------------+
/ \
YES NO
| |
v v
+--------+ +--------+
|Is A > C?| |Is B > C?|
+--------+ +--------+
/ \ / \
YES NO YES NO
| | | |
v v v v
+---+ +---+ +---+ +---+
| A | | C | | B | | C |
|is | |is | |is | |is |
|Lar| |Lar| |Lar| |Lar|
|gest |gest |gest |gest
+---+ +---+ +---+ +---+
\ \ / /
\ \ / /
\ \/ /
\ +----------+ /
\ | Print | /
+-->| Largest |<--+
+----------+
|
v
+--------+
| STOP |
+--------+
Clean Step-by-Step Logic
START
|
Read A, B, C
|
Is A > B?
โโโ YES --> Is A > C?
โ โโโ YES --> Print "A is Largest"
โ โโโ NO --> Print "C is Largest"
|
โโโ NO --> Is B > C?
โโโ YES --> Print "B is Largest"
โโโ NO --> Print "C is Largest"
|
STOP
Example Trace
| A | B | C | A > B? | Next Check | Result |
|---|---|---|---|---|---|
| 10 | 5 | 8 | YES | A > C? (10>8) YES | A = 10 is Largest |
| 3 | 9 | 6 | NO | B > C? (9>6) YES | B = 9 is Largest |
| 4 | 6 | 11 | NO | B > C? (6>11) NO | C = 11 is Largest |
2081.2 ยท 5 marks
Write an algorithm and flowchart to test whether the given integer is odd or even. [5]
Break and continue statements
2078 ยท 5 marks
How break statement is different from continue statement. Explain with examples? [5]
2077 ยท 5 marks
How break statement is different from continue statement? Explain with suitable example. [5]
Comparison of switch and if else ladder
2081.2 ยท 5 marks
Differentiate between switch statement and if else if ladder. [5]
0 ยท 5 marks
Differentiate between switch case and else .. if ladder. Why is break statement used inside switch case? Explain briefly. [5]
Make Unit 4 stick
Practice BIT102 with flashcards & quizzes