Unit 2 of 4 · B.Sc IT Sem 1

Unit 2: Control structures and functions

Programming in C notes · PTU syllabus (BSIT102/BSBC102)

3 min read7 topics10 exam questions
On this page
  1. Unit summary
  2. Decision making: if, if-else, nested if and switch
  3. Loops: while, do-while and for
  4. Jump statements: break, continue and goto
  5. Function declaration and categories
  6. Parameter passing: call by value and call by reference
  7. Recursion
  8. Storage classes
  9. Key terms
  10. Quick revision
  11. Important questions

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
Comparisonwhile vs do-while vs for
Condition checked
Typical use

while

Before each iteration

Repeat until a condition changes

do-while

After each iteration: runs at least once

Menus, input validation

for

Before each iteration

Known number of repetitions

1

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");
}
Comparisonif-else ladder vs switch
if-else ladder
switch

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

2

Topic 2

Loops: while, do-while and for

A loop repeats a block of statements while a condition holds.

Comparisonwhile vs do-while vs for
Condition checked
Runs at least once?

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.

3

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.
4

Topic 4

Function declaration and categories

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;
}
ClassificationCategories of functions
Functions
  • No arguments, no return value

  • Arguments, no return value

  • No arguments, return value

  • Arguments and return value

5

Topic 5

Parameter passing: call by value and call by reference

  • 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.
ComparisonCall by value vs call by reference
Call by value
Call by reference

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

6

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.

7

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 classDefault valueScopeLifetime
autoGarbageInside the blockUntil the block ends
externZeroWhole program (global)Whole program
staticZeroInside the block (or file)Whole program; keeps its value between calls
registerGarbageInside the blockUntil 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

  1. Q1.Distinguish while and do-while.
  2. Q2.When is switch preferred to else-if?
  3. Q3.What does continue do?
  4. Q4.Distinguish call by value and call by reference.
  5. Q5.What is a base case in recursion?
  6. Q6.Name the four storage classes.

Long-answer questions

  1. Q1.Explain decision-making statements in C with examples.
  2. Q2.Explain loops and jump statements in C.
  3. Q3.Explain functions, their categories and parameter passing methods.
  4. 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.

WhatsApp us