Unit 2 of 4 · BCA Sem 1

Unit 2: Input/output and control statements

Problem Solving Techniques notes · PTU syllabus (UGSEC2501)

3 min read6 topics10 exam questions
On this page
  1. Unit summary
  2. Formatted input and output
  3. Unformatted input and output
  4. Decision making: if, if-else and switch
  5. Loops: while, do-while and for
  6. Nested loops
  7. break and continue
  8. Key terms
  9. Quick revision
  10. Important questions

Unit summary

Programs need to talk to the user and to make decisions. This unit covers input and output in C — formatted and unformatted — and the control statements that decide which statements run and how many times: if-else, switch, while, do-while, for, nested loops, break and continue.

After this unit you can

  • Use printf and scanf with format specifiers, and getchar/putchar and gets/puts
  • Write programs using if, if-else, nested if and switch
  • Write loops with while, do-while and for, including nested loops
  • Use break and continue correctly

PTU syllabus topics

  • Formatted and unformatted input/output
  • while
  • do-while and for statements
  • nested loops
  • if-else
  • switch
  • break-continue statements
Comparisonwhile vs do-while
while
do-while

Condition checked

Before each iteration

After each iteration

Minimum runs

Zero

One

Syntax ends with

Closing brace

while(condition);

Typical use

Unknown number of repetitions

Menus that must show at least once

1

Topic 1

Formatted input and output

  • printf() displays output using a format string: printf("Marks = %d", m);
  • scanf() reads input into the address of a variable: scanf("%d", &m);
  • Field width and precision control layout: %5d prints in 5 spaces, %.2f prints two decimal places.
  • Escape sequences: \n new line, \t tab, \\ backslash.

Exam tip

Forgetting the & in scanf (except for strings) is the most common error. Mention it when explaining scanf.

2

Topic 2

Unformatted input and output

These functions handle characters and strings without format specifiers.

FunctionPurpose
getchar()Reads one character
putchar(c)Prints one character
gets(s) / fgets(s, n, stdin)Reads a whole line (fgets is safer)
puts(s)Prints a string and a new line
3

Topic 3

Decision making: if, if-else 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

4

Topic 4

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.

5

Topic 5

Nested loops

A loop inside another loop is a nested loop. The inner loop completes all its iterations for every single iteration of the outer loop. Nested loops print patterns and process tables and matrices.

c/* Prints a right-angled triangle of stars */
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++)
        printf("* ");
    printf("\n");
}
6

Topic 6

break and continue

  • 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;.

Key terms

Format specifier
A code like %d that tells printf/scanf the data type
Entry-controlled loop
A loop that checks its condition before running (while, for)
Exit-controlled loop
A loop that checks its condition after running (do-while)
Nested loop
A loop inside another loop
Fall-through
Running into the next case of a switch when break is missing

Quick revision

  • scanf needs & before variable names (not for strings).
  • switch works only with int/char constants; add break after each case.
  • do-while always runs at least once.
  • Inner loop finishes fully for each outer iteration.
  • break exits the loop; continue skips one iteration.

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.Differentiate between formatted and unformatted I/O functions.
  2. Q2.Why is & used in scanf?
  3. Q3.Differentiate between while and do-while loops.
  4. Q4.What is the use of the default case in switch?
  5. Q5.Differentiate between break and continue.
  6. Q6.Write the syntax of a for loop.

Long-answer questions

  1. Q1.Explain all decision-making statements of C with syntax and examples.
  2. Q2.Explain the three loops of C with examples and compare them.
  3. Q3.Write a menu-driven calculator program using switch and do-while.
  4. Q4.Write a program to print a multiplication table using nested loops, and explain how nested loops work.

Stuck on this unit?

Message SBS on WhatsApp for help with Problem Solving Techniques, or to ask about studying BCA at Synetic.

WhatsApp us