Unit 2: Control structures and functions
Programming in C notes · PTU syllabus (BSIT102/BSBC102)
On this page
Unit summary
Control structures decide which statements run and how often; functions break programs into reusable parts. This unit covers decision making with if, if-else and nested if, while, do-while and for loops, jump statements — break, continue, goto and switch — function declaration and categories, parameters and parameter passing, call by value and call by reference, recursion and storage classes.
After this unit you can
- Use decision-making statements
- Use loops and jump statements
- Declare and call functions with different parameter passing
- Apply recursion and storage classes
PTU syllabus topics
- Decision making with if/if-else/nested if
- while/do-while/for loops
- jump statements (break, continue, goto, switch)
- function declaration and categories
- parameters and parameter passing
- call-by-value/reference
- recursion
- storage classes
Topic 1
Decision making: if, if-else, nested if and switch
- if: runs a block only if the condition is true.
- if-else: chooses between two blocks.
- Nested if / else-if ladder: tests several conditions in order.
- switch: selects one of many cases by the value of an integer or character expression; each case usually ends with
break.
cint day = 3;
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
default: printf("Invalid day");
}Tests
Any condition, including ranges
Equality with constant values only
Data types
Any
int or char
Readability
Harder for many cases
Cleaner for many fixed cases
Fall-through
Not possible
Happens if break is missing
Example
Grading with else-if: if (m >= 75) printf("Distinction"); else if (m >= 60) printf("First"); else if (m >= 40) printf("Pass"); else printf("Fail"); — for m = 65 the output is First.
Exam tip
Use switch when testing one variable against fixed values (menus); remember break after each case.
Topic 2
Loops: while, do-while and for
A loop repeats a block of statements while a condition holds.
while
Before the body (entry-controlled)
No
do-while
After the body (exit-controlled)
Yes
for
Before the body; initialisation and update in one line
No
c/* Sum of first n natural numbers */
int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum = %d", sum); /* 55 */Example
A menu program uses do-while, because the menu must be shown at least once before the user chooses to exit.
Topic 3
Jump statements: break, continue and goto
- break ends the loop (or switch) immediately and jumps to the statement after it.
- continue skips the rest of the current iteration and moves to the next one.
Example
Printing 1 to 10 but skipping 5: inside the loop, if (i == 5) continue;. Stopping at the first negative number in input: if (x < 0) break;.
- goto: transfers control to a labelled statement — legal but discouraged because it makes programs hard to follow; occasionally used to exit deeply nested loops.
Topic 4
Function declaration and categories
Functions are grouped into four categories by arguments and return value: no arguments, no return; arguments, no return; no arguments, with return; and arguments with return. A function needs a prototype (declaration), a definition and a call. A function is a self-contained block of code that performs a specific task. Functions reduce repetition, make programs easier to read and debug, and allow reuse.
- Function prototype (declaration): tells the compiler the name, return type and parameter types:
int add(int, int); - Function definition: the actual body.
- Function call: runs it:
s = add(4, 5);
c#include <stdio.h>
int add(int a, int b); /* prototype */
int main() {
printf("%d", add(4, 5)); /* call: prints 9 */
return 0;
}
int add(int a, int b) { /* definition */
return a + b;
}No arguments, no return value
Arguments, no return value
No arguments, return value
Arguments and return value
Example
A function with arguments and return: int sum(int a, int b) { return a + b; } — calling printf("%d", sum(4, 5)); prints 9. The prototype int sum(int, int); is written before main.
Exam tip
Learn the three parts — prototype, definition and call — with a short example.
Topic 5
Parameter passing: call by value and call by reference
In call by value, a copy of the value is passed, so changes inside the function do not affect the original. In call by reference, the address is passed using pointers, so the function can change the original variable.
- Actual arguments are the values in the call; formal arguments are the parameters in the definition.
- In C, arguments are passed by value: the function gets a copy, so changes inside it do not affect the original variable.
- To change the caller's variable, pass its address (call by reference using pointers), as in a swap function.
What is passed
A copy of the value
The address of the variable
Effect on original
Unchanged
Can be changed
Syntax
swap(a, b)
swap(&a, &b) with pointer parameters
Example
Swapping two numbers: void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } called as swap(&x, &y); really swaps x and y. A version taking int a, int b swaps only the copies, and x and y stay unchanged.
Exam tip
Swap is the classic exam example for call by reference — practise writing it.
Topic 6
Recursion
Recursion is when a function calls itself. Every recursive function needs a base case that stops the recursion and a recursive case that moves towards it.
cint fact(int n) {
if (n <= 1) return 1; /* base case */
return n * fact(n - 1); /* recursive case */
}Example
fact(4) = 4 × fact(3) = 4 × 3 × fact(2) = 4 × 3 × 2 × fact(1) = 24.
Exam tip
Without a base case recursion never stops and the program crashes with a stack overflow.
Topic 7
Storage classes
A storage class decides where a variable is stored, its default value, its scope (where it can be used) and its lifetime (how long it exists).
| Storage class | Default value | Scope | Lifetime |
|---|---|---|---|
| auto | Garbage | Inside the block | Until the block ends |
| extern | Zero | Whole program (global) | Whole program |
| static | Zero | Inside the block (or file) | Whole program; keeps its value between calls |
| register | Garbage | Inside the block | Until the block ends; stored in a CPU register if possible |
Example
A static counter inside a function keeps counting across calls: the first call prints 1, the second 2, and so on, because it is not re-created each time.
Key terms
- Nested if
- if statement inside another if
- Entry-controlled loop
- Condition checked before the body (while, for)
- Exit-controlled loop
- Condition checked after the body (do-while)
- Call by reference
- Passing addresses so the function can change the caller's variables
- Recursion
- Function calling itself
Quick revision
- if, if-else, nested if, else-if ladder, switch.
- while, do-while, for; entry vs exit controlled.
- break, continue, goto.
- Function prototype, definition, call; four categories; value vs reference.
- Recursion (base case); auto, register, static, extern.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Distinguish while and do-while.
- Q2.When is switch preferred to else-if?
- Q3.What does continue do?
- Q4.Distinguish call by value and call by reference.
- Q5.What is a base case in recursion?
- Q6.Name the four storage classes.
Long-answer questions
- Q1.Explain decision-making statements in C with examples.
- Q2.Explain loops and jump statements in C.
- Q3.Explain functions, their categories and parameter passing methods.
- Q4.Explain recursion and storage classes with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Programming in C, or to ask about studying B.Sc IT at Synetic.
