ACS101 · Programming Languages · Unit 2
Decision Making and Control Flow (C)
Unit Scope & Exam Weightage: This unit makes programs decide and repeat: Control Statements, Loops — for, while, do-while, break and continue, Functions — definition, call, return, call by value and Recursion — factorial and Fibonacci. It has 23 PYQ clusters worth 121 marks (15 × 7m and 8 × 2m) from
2023-24,2024-25and2025-26. Recursion alone accounts for seven clusters, and the three Survival Mode clusters testswitch+break, nested loop output, and abreaktrace—so tracing control flow by hand is the highest-value skill.
Front Page: Importance-Wise Topic Roadmap (2023–26 PYQs)
| Priority | Topic (Exact Syllabus) | PYQs Asked | Marks Weightage | Tag |
|---|---|---|---|---|
| 1 | Recursion — factorial and Fibonacci (simple examples only) Real connect: Control flow is the brain of every program — from traffic signal logic to drone autopilot decision-making | 7 (2023-24 ×3, 2024-25 ×2, 2025-26 ×2) |
7m × 3 + 2m × 4 = 29m | Survival Mode Program |
| 2 | Functions — definition / call / return / call by value | 6 (2023-24, 2024-25, 2025-26) |
7m × 5 + 2m = 37m | High Yield |
| 3 | Control Statements | 4 (2024-25 ×2, 2025-26 ×2) |
7m × 3 + 2m = 23m | Survival Mode Program |
| 4 | Loops — for / while / do-while | 3 (2023-24 ×2, 2024-25) |
7m + 2m × 2 = 11m | Trace |
| 5 | break and continue | 3 (2023-24 ×2, 2025-26) |
7m × 2 + 2m = 16m | Survival Mode Trace |
| 6 | Algorithm / flowchart (attached PYQ topic) | 2 (2023-24) |
7m × 2 = 14m | Theory + Diagram |
| 7 | Selection-sort complexity (attached cross-unit PYQ) | 1 (2024-25) |
2m | Quick Score |
2-Hour Last-Night Survival Checklist:
- Switch fall-through: without
break, execution continues into the nextcase. The survival question expects abreakin every case and adefaultarm. - Break trace: in
for(i=0;i<5;i++){a++; if(i==3) break;}the loop runs fori=0,1,2,3, soa=4. Write the iteration table, not just the answer. - Recursion template: every solution must show
if (n == 0 || n == 1) return 1;(factorial) orif (n<=1) return n;(Fibonacci) before the recursive call. Missing base case loses a step mark. - Call by value vs reference: value passes a copy so the caller is unchanged; reference passes
&addressand dereferences with*so the caller changes. Swap example is mandatory. - Output traces: keep a four-column table
iteration | i | condition | actionready; the 2023-24 loop question breaks at the first multiple of 4 after 6.
1. Control Statements: if, if-else, Nested if-else, switch
Control statement: a construct that changes the sequential order of execution. C provides if, if-else, nested if-else, switch, and the conditional operator ?: as decision makers.
/* nested if-else structure */
if (marks >= 80)
grade = 'A';
else if (marks >= 60)
grade = 'B';
else if (marks >= 40)
grade = 'C';
else
grade = 'F';
%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '17px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart TD
A[Start] --> B{marks >= 80?}
B -->|yes| C[grade = A]
B -->|no| D{marks >= 60?}
D -->|yes| E[grade = B]
D -->|no| F{marks >= 40?}
F -->|yes| G[grade = C]
F -->|no| H[grade = F]
C --> I[Print grade]
E --> I
G --> I
H --> I
I --> J[Stop]
switch form: the controlling expression must be of integer or character type. Each case is compared for equality; break prevents fall-through; default handles unmatched values.
switch (choice) {
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
default: printf("Other\n");
}
Examiner Tip: When asked “why is break required in a switch?”, answer: it transfers control out of the switch body; otherwise all following case labels execute until a break or the end of the switch. Demonstrate with choice=1 and no break—the output would be One, Two, Other.
2. Loops: for, while, do-while, break, continue
Loop: a construct that repeats a block while a condition holds. for keeps initialisation, test and update together; while tests before entry (may execute zero times); do-while tests after the body (executes at least once).
| Loop | Syntax | When to use | Minimum executions |
|---|---|---|---|
for |
for(init; test; update) |
Count known in advance | 0 |
while |
while(test) |
Condition-driven repetition | 0 |
do-while |
do { ... } while(test); |
Menu/input validation | 1 |
/* descending star pattern: **** / *** / ** / * */
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++) printf("*");
printf("\n");
}
break vs continue: break exits the innermost loop or switch immediately; continue skips the remaining statements in the current iteration and jumps to the update/test. In a vowel checker, break stops after finding a vowel; in an even-number sum, continue skips odd values.
Trace of the survival code:
| Iteration | i before test |
a after a++ |
i == 3? |
Action |
|---|---|---|---|---|
| 1 | 0 | 1 | no | continue |
| 2 | 1 | 2 | no | continue |
| 3 | 2 | 3 | no | continue |
| 4 | 3 | 4 | yes | break |
Final printf("%d", a) prints 4.
3. Functions: Definition, Call, Return and Parameter Passing
Function: a named, reusable block that performs a specific task. Its definition has a return type, name, parameter list and body. A return statement sends one value back to the caller.
#include <stdio.h>
/* definition */
int factorial(int n) {
int f = 1;
for (int i = 2; i <= n; i++) f *= i;
return f;
}
int main(void) {
int r = factorial(5); /* call */
printf("%d\n", r); /* 120 */
return 0;
}
| Aspect | Call by value | Call by reference |
|---|---|---|
| What is passed | Copy of the value | Address of the variable |
| Formal parameter | int x |
int *x |
| Call syntax | f(a); |
f(&a); |
| Effect on caller | Original unchanged | Original can be modified via *x |
| Typical use | Safe computation | Swap, update multiple results |
Types of functions in C: library/standard functions (printf, strlen) and user-defined functions; the latter may be with or without arguments and with or without return value (four combinations).
Examiner Tip: In a call-by-reference swap, draw two boxes a and b, write &a and &b on the arrows, and show temp = *x; *x = *y; *y = temp;. This diagram plus three lines earns full marks.
4. Recursion: Factorial and Fibonacci
Recursion: a function calling itself to solve a smaller instance of the same problem. Every recursive function needs a base case that stops recursion and a recursive step that moves toward it.
int fact(int n) {
if (n <= 1) return 1; /* base case */
return n * fact(n - 1); /* recursive step */
}
int fib(int n) {
if (n <= 1) return n; /* base case */
return fib(n-1) + fib(n-2);
}
Call stack trace for fact(4): fact(4) → 4*fact(3) → 4*(3*fact(2)) → 4*(3*(2*fact(1))) → unwind 1 → 2 → 6 → 24. Without the base case, the stack grows until overflow.
Product of digits by recursion: digits(n) = (n%10) * digits(n/10) with digits(0) = 1 (multiplicative identity). For n=123, 3 * digits(12) = 3 * (2 * digits(1)) = 3*(2*1) = 6.
5. Algorithm and Flowchart (Attached PYQ Topic)
Algorithm: a finite sequence of unambiguous steps producing a result. Flowchart: a graphical representation where oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision, arrows = flow.
| Algorithm | Flowchart |
|---|---|
| Textual step list | Diagram with standard symbols |
| Easier to convert to code | Easier to visualise branching |
| Language-independent | Also language-independent |
| No drawing conventions | Requires symbols and arrows |
Properties of a good flowchart: exactly one start and one stop, one entry and one exit per decision, unambiguous labels, and arrows that never cross unnecessarily.
Buzz number: ends in 7 or divisible by 7. Algorithm: read n; if n%10 == 7 || n%7 == 0 print “Buzz”, else “Not Buzz”.
6. Solved Exam-Ready Questions (Exact PYQ Wording)
Solved Q1 (2023-24, 2025-26, 7 Marks | Survival Mode)
Q: Write a program to discuss the use of break in a switch case statement.
Solution:
#include <stdio.h>
int main(void) {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
break leaves the switch after the matching case. Without it, entering 2 would print Tuesday through Sunday and then “Invalid day” because of fall-through.
Solved Q2 (2024-25, 2025-26, 7 Marks | Survival Mode)
Q: *Write a program to print the following pattern in C:
**
- *
Solution:
#include <stdio.h>
int main(void) {
int i, j;
for (i = 4; i >= 1; i--) {
for (j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}
Outer loop controls the number of stars in a row (4,3,2,1); inner loop prints that many stars. Output:
****
***
**
*
Solved Q3 (2024-25, 2025-26, 2 Marks | Survival Mode)
Q: Write the output of the following code: #include<stdio.h> int main() { int a = 0, i = 0, b; for (i = 0; i < 5; i++) { a++; if (i == 3) break; } printf("%d", a); return 0; }
Solution: Iterations run for i = 0, 1, 2, 3; each increments a. At i = 3, break exits before i reaches 4. Output: 4.
Solved Q4 (2024-25, 7 Marks)
Q: Write a program to check the entered year is leap year or not with conditional operator.
Solution:
#include <stdio.h>
int main(void) {
int y;
printf("Enter year: ");
scanf("%d", &y);
(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
? printf("%d is a leap year\n", y)
: printf("%d is not a leap year\n", y);
return 0;
}
A year divisible by 400 is leap; otherwise it must be divisible by 4 but not by 100. Examples: 2000 → leap; 1900 → not leap; 2024 → leap.
Solved Q5 (2024-25, 7 Marks)
Q: Write a C program to check whether a number is even or odd using switch case.
Solution:
#include <stdio.h>
int main(void) {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
switch (n % 2) {
case 0: printf("Even\n"); break;
case 1: printf("Odd\n"); break;
}
return 0;
}
For negative odd numbers, n % 2 is -1 in C; add case -1: printf("Odd\n"); break; for completeness.
Solved Q6 (2023-24, 7 Marks)
Q: What is Flowchart what are the properties of Flowchart? Draw a flowchart to check number is a buzz number or not. (A number is said to be Buzz if it ends with 7 or divisible by 7).
Solution: Definition and properties are in Section 5. Flowchart:
%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '17px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart TD
A[Start] --> B[/Read n/]
B --> C{n % 10 == 7 OR n % 7 == 0?}
C -->|yes| D[/Print Buzz/]
C -->|no| E[/Print Not Buzz/]
D --> F[Stop]
E --> F
Test: 27 ends in 7 → Buzz; 49 divisible by 7 → Buzz; 25 → Not Buzz.
Solved Q7 (2023-24, 7 Marks)
Q: How algorithm is different form flowchart. Write an algorithm to check number is a Prime number or not.
Solution: Difference from Section 5 table. Prime algorithm:
- Read
n. - If
n < 2, print “Not prime” and stop. - For
i = 2tosqrt(n), ifn % i == 0, print “Not prime” and stop. - Print “Prime”.
- Stop.
#include <stdio.h>
int main(void) {
int n, i, prime = 1;
scanf("%d", &n);
if (n < 2) prime = 0;
for (i = 2; i*i <= n; i++)
if (n % i == 0) { prime = 0; break; }
printf(prime ? "Prime\n" : "Not prime\n");
return 0;
}
Testing divisors only up to √n is sufficient because any factor larger than √n pairs with one smaller than √n.
Solved Q8 (2023-24, 7 Marks)
Q: Why break statement is required? Write a C program in C language to check character is a vowel or consonant.
Solution: break is required to exit a loop or switch immediately once the desired condition is met, avoiding unnecessary iterations and preventing switch fall-through.
#include <stdio.h>
int main(void) {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch); /* leading space skips leftover newline */
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel\n"); break;
default:
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("Consonant\n");
else
printf("Not an alphabet\n");
}
return 0;
}
Stacked case labels implement logical OR without repeating the body.
Solved Q9 (2023-24, 7 Marks)
Q: Write a Program to check whether the entered number is prime or not.
Solution: Same algorithm and code as Solved Q7. For a 7-mark answer, include the i*i <= n optimisation and explain it in one sentence; this distinguishes a loop over n from a loop over √n.
Solved Q10 (2023-24, 7 Marks)
Q: What are the different types of functions in C programming? Write a C program to print the multiplication of all the digits of a number using recursion.
Solution: Types: (1) library vs user-defined; within user-defined: no-argument/no-return, no-argument/with-return, with-argument/no-return, with-argument/with-return.
#include <stdio.h>
int digitProduct(int n) {
if (n == 0) return 1; /* multiplicative identity */
return (n % 10) * digitProduct(n / 10);
}
int main(void) {
int n;
scanf("%d", &n);
printf("Product of digits = %d\n", digitProduct(n));
return 0;
}
For n = 123, trace: 3 * digitProduct(12) → 3 * 2 * digitProduct(1) → 3 * 2 * 1 * digitProduct(0) → 6.
Solved Q11 (2024-25, 2 Marks)
Q: Draw the structure of Nested if else statement.
Solution:
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
default_statement;
Only the first true condition executes; the final else is optional and handles all false cases. The diagram in Section 1 shows the same structure.
Solved Q12 (2025-26, 7 Marks)
Q: What is recursion? Write a program to find factorial of n numbers using recursion.
Solution: Definition in Section 4.
#include <stdio.h>
long long fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
int main(void) {
int n;
scanf("%d", &n);
printf("%lld\n", fact(n));
return 0;
}
fact(5) unwinds as 5*4*3*2*1 = 120. Use long long because 13! already exceeds 32-bit int.
Solved Q13 (2024-25, 7 Marks)
Q: Write a recursive C program to find the Fibonacci number.
Solution:
#include <stdio.h>
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
int main(void) {
int n;
scanf("%d", &n);
printf("Fib(%d) = %d\n", n, fib(n));
return 0;
}
fib(6) = fib(5)+fib(4) = 5+3 = 8. The sequence starts 0,1,1,2,3,5,8. Note that plain recursion recalculates subproblems; an iterative version is O(n) while this one is exponential.
Solved Q14 (2025-26, 2 Marks)
Q: What is a recursive function? Explain with an example.
Solution: A recursive function is one that invokes itself with a smaller argument until a base case is reached. Example: int fact(int n){ return n<=1 ? 1 : n*fact(n-1); }; the call fact(3) expands to 3*fact(2) and stops at fact(1)=1.
Solved Q15 (2024-25, 2 Marks)
Q: Define recursion with an example.
Solution: Recursion is the technique of solving a problem by reducing it to a smaller instance of the same problem and calling the same function. Example: fib(n) = fib(n-1) + fib(n-2) with base values fib(0)=0, fib(1)=1.
Solved Q16 (2023-24, 2 Marks)
Q: Write the importance of base value in recursive function.
Solution: The base value (base case) is the condition that stops further self-calls. Without it, recursion never terminates and the program crashes with stack overflow. For factorial, n <= 1 is the base case; for Fibonacci, n <= 1 returns n.
Solved Q17 (2024-25, 2 Marks)
Q: Write the time complexity of Selection Sort.
Solution: Selection sort is O(n²) in best, average and worst cases because it always scans the remaining unsorted subarray to find the minimum. It uses O(1) extra space. (This 2-mark question is tagged in Unit 2 but belongs to the sorting topic of Unit 3.)
Solved Q18 (2023-24, 2 Marks)
Q: *Find the Output of Code:
void main () {
int a, b;
for (a = 6, b = 4; a <= 24; a = a + 6) {
if (a % b == 0) break;
}
printf("%d", a);
}
```*
**Solution:** Values of `a`: `6` (6%4=2), `12` (12%4=0 → break). Loop stops at `a = 12`. **Output:** `12`.
---
### Solved Q19 (`2024-25`, 2 Marks)
**Q:** *Mention the use of break and continue in loops.*
**Solution:** `break` terminates the loop immediately and transfers control to the statement after the loop. `continue` skips the rest of the current iteration and jumps to the loop's update/test step. Example: `if (x < 0) continue;` ignores negative inputs; `if (x == target) break;` stops a search.
---
### Solved Q20 (`2025-26`, 7 Marks)
**Q:** *Differentiate between call by value and call by reference parameter passing in C using example.*
**Solution:** Use the Section 3 comparison table plus this program:
```c
#include <stdio.h>
void byValue(int x) { x = 99; }
void byRef(int *x) { *x = 99; }
int main(void) {
int a = 5;
byValue(a); printf("%d\n", a); /* 5: unchanged */
byRef(&a); printf("%d\n", a); /* 99: changed */
return 0;
}
byValue receives a copy, so assigning to x cannot touch a. byRef receives the address of a; dereferencing with *x writes into a itself.
Solved Q21 (2024-25, 7 Marks)
Q: Describe the difference between call by value and call by reference with examples.
Solution: Same distinction as Solved Q20. Add that arrays are passed to functions by reference automatically (the array name decays to a pointer to its first element), which is why a function can modify array elements but not the scalar variables passed alongside them.
Solved Q22 (2024-25, 7 Marks)
Q: Define call by reference. Write a program to swap two numbers with this concept.
Solution:
#include <stdio.h>
void swap(int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}
int main(void) {
int a = 10, b = 20;
printf("Before: a=%d b=%d\n", a, b);
swap(&a, &b);
printf("After: a=%d b=%d\n", a, b);
return 0;
}
Output: Before: a=10 b=20, After: a=20 b=10. The addresses &a, &b are passed; * dereferences them to read/write the original variables.
7. Practice Questions (Self-Test)
- (2 Marks) What is the output of
for(i=1;i<=3;i++){ if(i==2) continue; printf("%d",i); }? (Ans: 13.) - (7 Marks) Write a
do-whilemenu program that re-prompts until the user enters a choice from 1 to 3. - (7 Marks) Trace
fib(7)by drawing its recursive calls and state how many callsfib(2)is evaluated. - (7 Marks) Write a function
int countVowels(char s[])and explain why the array parameter lets the function read the caller's string. - (2 Marks) State one situation where
breakis preferable tocontinuein a search loop.