Control Flow Questions with Answers

23 practice questions on Control Flow (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <stdio.h>

int main(void) {
    int i = 0;
    loop:
    if (i < 3) {
        printf("%d", i);
        i++;
        goto loop;
    }
    printf("\n");
    return 0;
}
  1. 123
  2. 0
  3. 012
  4. 0123

Answer: C. 012

Explanation

The goto jumps back to the label while i < 3, acting as a loop that prints 0, 1 and 2.

Step-by-step solution

  1. The goto loop prints i while i < 3.
  2. Output: 012.

Why the other options are wrong

  • A. Starts at 0.
  • B. Loops continue.
  • D. 3 is not printed.

Tip: Trace the values.

Question 2Easy

What is the output?

#include <stdio.h>

int main(void) {
    int sum = 0;
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0)
            continue;
        sum += i;
    }
    printf("%d\n", sum);
    return 0;
}
  1. 25
  2. 30
  3. 55
  4. 20

Answer: A. 25

Explanation

continue skips the even numbers, so only 1 + 3 + 5 + 7 + 9 = 25 is added.

Step-by-step solution

  1. The loop runs from i = 1 to i = 10.
  2. If i is even, continue skips the rest of the loop.
  3. Only odd numbers are added to sum: 1 + 3 + 5 + 7 + 9 = 25.
  4. Final value of sum is 25.
  5. Printf prints 25.

Why the other options are wrong

  • B. Thirty is the sum of the even numbers 2 to 10; continue skips even i, so they are never added.
  • C. Fifty-five adds all numbers 1 to 10, but continue skips the even ones, so only odd values count.
  • D. Twenty is wrong; the odd values 1, 3, 5, 7 and 9 add up to 25, not 20.

Tip: Remember that continue skips the current iteration and moves to the next.

Question 3Easy

What is the output?

#include <stdio.h>

int main(void) {
    int x = 2;
    switch (x) {
    case 1: printf("A");
    case 2: printf("B");
    case 3: printf("C"); break;
    default: printf("D");
    }
    printf("\n");
    return 0;
}
  1. BCD
  2. BC
  3. ABC
  4. B

Answer: B. BC

Explanation

Execution jumps to case 2 and, with no break, falls through into case 3; the break there stops it before default.

Step-by-step solution

  1. X is 2, so execution jumps to case 2.
  2. Case 2 prints B and has no break, so it falls through.
  3. Case 3 prints C, and its break stops the switch before default.
  4. The output is BC, which is option B.

Why the other options are wrong

  • A. Default is skipped because a case matched and break stopped the switch first.
  • C. Case 1 is not run, so A is not printed.
  • D. C is printed too, because the fall-through runs case 3.

Tip: Without a break, a switch falls through into the next case label.

Question 4Easy

What is the key difference between a while loop and a do-while loop?

  1. while always executes its body at least once
  2. do-while cannot use break
  3. while cannot have a compound body
  4. do-while always executes its body at least once

Answer: D. do-while always executes its body at least once

Explanation

do-while checks its condition after each iteration, so the first iteration is unconditional; while checks before each iteration and may run zero times.

Step-by-step solution

  1. Do-while runs the loop body first, then checks the condition.
  2. While checks the condition before running the loop body.
  3. So do-while executes at least once regardless of the condition.
  4. This makes do-while different from while in execution order.
  5. The correct answer is do-while always executes its body at least once.
  6. Final Answer: D (do-while always executes its body at least once).

Why the other options are wrong

  • A. A while loop checks the condition first, so its body may run zero times; it does not always run once.
  • B. Break works inside a do-while loop, so a do-while loop can use break just like a while loop.
  • C. A while loop can have a compound body in braces; braces work for any loop body in C.

Tip: Remember do-while runs first, then checks; while checks first, then runs.

Question 5Medium

What is the output?

#include <stdio.h>

int main(void) {
    int n = 1200, r = 0;
    while (n > 0) {
        r = r * 10 + n % 10;
        n /= 10;
    }
    printf("%d\n", r);
    return 0;
}
  1. 12
  2. 2100
  3. 0021
  4. 21

Answer: D. 21

Explanation

Reversing 1200 produces digits 0, 0, 2, 1, but leading zeros vanish in an int: r goes 0, 0, 2, 21.

Step-by-step solution

  1. n = 1200, r = 0.
  2. First loop: n % 10 = 0, r = 0 * 10 + 0 = 0.
  3. Second loop: n = 120, n % 10 = 0, r = 0 * 10 + 0 = 0.
  4. Third loop: n = 12, n % 10 = 2, r = 0 * 10 + 2 = 2.
  5. Fourth loop: n = 1, n % 10 = 1, r = 2 * 10 + 1 = 21.

Why the other options are wrong

  • A. Twelve drops digits; reversing 1200 builds the digits 0, 0, 2, 1, which is the number 21.
  • B. Two thousand one hundred would need the zeros kept at the end, but the reversal gives 21.
  • C. Leading zeros are never printed for an int value, so the reversed result prints as 21, not 0021.

Tip: Remember that integers automatically drop leading zeros when reversed.

Question 6Medium

What is the output?

#include <stdio.h>

int main(void) {
    int n = 5, count = 0;
    for (int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
            count++;
    printf("%d\n", count);
    return 0;
}
  1. 10
  2. 25
  3. 15
  4. 20

Answer: C. 15

Explanation

The inner loop runs n - i times: 5 + 4 + 3 + 2 + 1 = 15, i.e. n(n+1)/2.

Step-by-step solution

  1. Inner loop runs 5, 4, 3, 2, 1 times.
  2. Total: 15.

Why the other options are wrong

  • A. 10 is for n = 4.
  • B. 25 is the full square.
  • D. 20 is wrong.

Tip: n(n+1)/2.

Question 7Medium

What is the output?

#include <stdio.h>

int main(void) {
    int x = 7;
    switch (x % 4) {
    case 1: printf("one");
    case 2: printf("two");
    }
    printf("end\n");
    return 0;
}
  1. onetwoend
  2. end
  3. oneend
  4. twoend

Answer: B. end

Explanation

7 % 4 is 3, which matches no case, and there is no default, so the switch body is skipped entirely.

Step-by-step solution

  1. X % 4 is 7 % 4 = 3.
  2. No case label is 3, and there is no default.
  3. So the switch body is skipped and nothing in it prints.
  4. The final printf prints end, which is option B.

Why the other options are wrong

  • A. One and two need case 1 and case 2 to match, but the value is 3.
  • C. One needs case 1 to match, but the value is 3.
  • D. Two needs case 2 to match, but the value is 3.

Tip: A switch with no matching case and no default does nothing.

Question 8Medium

What happens when this code runs?

int i = 0;
while (i < 5) {
    if (i == 2)
        continue;
    printf("%d", i);
    i++;
}
  1. It prints 0134
  2. It prints 01 and stops
  3. It prints 01234
  4. It prints 01 and then loops forever

Answer: D. It prints 01 and then loops forever

Explanation

When i reaches 2, continue jumps back to the condition without executing i++, so i stays 2 forever. In a for loop, continue would still run the increment expression.

Step-by-step solution

  1. i = 0 is not 2, so 0 is printed and i becomes 1.
  2. i = 1 is printed and i becomes 2.
  3. When i = 2, continue jumps back to the test without running i++.
  4. I stays 2 forever, so the loop never ends, which is option D.

Why the other options are wrong

  • A. 2 is never printed, because continue skips the print and the loop never reaches 3 or 4.
  • B. The loop does not stop, because i stays 2 and the test stays true.
  • C. Values 2, 3 and 4 are never printed, because the loop is stuck at i = 2.

Tip: In a while loop, continue jumps to the test and skips any update placed after it; in a for loop the update still runs.

Question 9Hard

What is the output?

#include <stdio.h>

int main(void) {
    int s = 0;
    for (int i = 0; i < 5; i++) {
        switch (i % 3) {
        case 0: s += 1;
        case 1: s += 10; break;
        default: continue;
        }
        s += 100;
    }
    printf("%d\n", s);
    return 0;
}
  1. 542
  2. 222
  3. 442
  4. 440

Answer: C. 442

Explanation

i = 0 and 3 (case 0): +1, fall through +10, then +100 = 111 each.

i = 1 and 4 (case 1): +10 +100 = 110 each.

i = 2 hits default, and continue skips the +100.

Total 222 + 220 = 442.

Step-by-step solution

  1. i=0: case 0 falls into case 1: 11, then +100 = 111.
  2. i=1: +10 = 121, +100 = 221.
  3. i=2 continue. i=3: 332. i=4: 442.

Why the other options are wrong

  • A. 542 over-counts.
  • B. 222 is for fewer iterations.
  • D. 440 ignores last additions.

Tip: Trace each i.

Question 10Hard

What is the output?

#include <stdio.h>

int main(void) {
    int i;
    for (i = 0; i++ < 3; )
        printf("%d", i);
    printf(" %d\n", i);
    return 0;
}
  1. 0123 4
  2. 123 3
  3. 012 3
  4. 123 4

Answer: D. 123 4

Explanation

The condition compares the old value and then increments: 0<3 (i=1, print 1), 1<3 (print 2), 2<3 (print 3), then 3<3 is false but i still becomes 4.

Step-by-step solution

  1. i++ < 3 compares then increments.
  2. Prints 1, 2, 3.
  3. At the last check, i becomes 4.

Why the other options are wrong

  • A. Does not print 0.
  • B. i ends at 4.
  • C. Does not start from 0.

Tip: Final i is 4.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics