🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Loop Control Interview Questions
A quiz and interview practice lab on break, continue and infinite loops, with compiled answers for embedded C programming students.
Interviewers love loops, because a few lines reveal how well you understand control flow. This final lecture of the loop control topic gives you a full practice lab with the most asked C loop control interview questions, a quiz on break, continue and infinite loops, and output exercises checked with real runs. It belongs to our free C programming course for embedded systems.
What You Will Learn
- A quick revision of infinite loops,
breakandcontinue - Ten multiple choice questions with explanations
- Six output prediction exercises with real, compiled answers
- Three spot-the-bug problems and three coding challenges
- Twelve C loop control interview questions with model answers
Prerequisites
- Lectures cp5_19, cp5_20 and cp5_21 of this free C programming course, which prepare you for these C loop control interview questions
gccto check your own answers. Try each question on paper first
Quick Revision for C Loop Control Interview Questions
Infinite loop
break
continue
| Statement | Effect | Counter after loop (loop 1 to 6, trigger at 4) |
|---|---|---|
break | Leaves the loop | 4 |
continue | Skips pass 4 only | 7 |
Quiz Part 1: Multiple Choice Questions
Choose an answer in your head, then open the details to check it.
Q1. Which statement ends the nearest enclosing loop immediately?
break. It leaves the loop and control continues after it. continue only ends the current pass.
Q2. In a for loop, after continue control goes to the
update expression. The update such as i++ runs, then the condition is tested.
Q3. In a while loop, after continue control goes to the
condition test. Any update written after the continue is skipped.
Q4. What does for (;;) mean?
An endless loop. An empty condition is treated as true, so it runs until a break, return or goto leaves it.
Q5. In nested loops, break leaves
only the innermost loop that contains it. The outer loop goes on.
Q6. A break inside a switch inside a for loop ends
the switch only. The for loop continues with the next iteration.
Q7. while (x < 5); with x never changed does what?
Loops forever. The semicolon is an empty body, so nothing changes x.
Q8. uint8_t i; for (i = 0; i <= 255; i++) runs how many times?
Forever. An 8-bit value never exceeds 255, so the test is always true and the counter wraps to 0.
Q9. The safest way to repeat a loop exactly ten times with step 0.1 is to
count with an integer from 0 to 10 and compute the value as step / 10.0. Floats accumulate rounding error.
Q10. GCC says: break statement not within loop or switch. Why?
break was written outside any loop or switch, usually because of a misplaced brace.
Quiz Part 2: Predict the Output
Work out the output by hand. Then compare with the answer, which comes from real compiled runs with gcc -std=c17.
Exercise 1: break and continue together
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 6; i++) {
if (i % 2 == 0) continue;
if (i == 5) break;
printf("%d ", i);
}
printf("| %d\n", i);
return 0;
}
Output:
1 3 | 5
Even values are skipped by continue. At 5 the break fires before printing, so the loop stops with i equal to 5. Printed values are 1 and 3.
Exercise 2: break in a nested loop
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++) {
if (j == 2) break;
printf("%d%d ", i, j);
}
printf("\n");
return 0;
}
Output:
11 21 31
The inner break fires when j is 2, so only j equal to 1 prints for each i. The outer loop still runs three times.
Exercise 3: switch without break on case 2
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 4; i++) {
switch (i) {
case 1: printf("one "); break;
case 2: printf("two ");
default: printf("def ");
}
}
printf("| %d\n", i);
return 0;
}
Output:
def one two def def | 4
Case 2 has no break, so it prints two and falls through to def. Values 0 and 3 hit default. The final i is 4.
Exercise 4: continue with a post-increment in the condition
#include <stdio.h>
int main(void)
{
int i = 0;
while (i++ < 5) {
if (i == 3) continue;
printf("%d ", i);
}
printf("| %d\n", i);
return 0;
}
Output:
1 2 4 5 | 6
i++ runs inside the condition, so after the test i is already one larger. The value 3 is skipped and the final value is 6, because the last failed test also increments.
Exercise 5: continue in do…while
#include <stdio.h>
int main(void)
{
int i = 0;
do {
i++;
if (i == 2) continue;
printf("%d ", i);
} while (i < 4);
printf("| %d\n", i);
return 0;
}
Output:
1 3 4 | 4
The pass with i equal to 2 skips the print but the test still runs. The loop ends when i reaches 4.
Exercise 6: value of i after break
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 10; i++) {
if (i * i > 20) break;
}
printf("%d\n", i);
return 0;
}
Output:
5
i * i first exceeds 20 when i is 5 (25). The break leaves with i still 5.
Quiz Part 3: Spot the Bug
Bug 1: the loop never ends
uint8_t n;
for (n = 10; n >= 0; n--)
printf("%u\n", n);
An unsigned value is never below 0, so n >= 0 is always true and the counter wraps from 0 to 255. Use a signed int or count differently.
Bug 2: the work never happens
int tries = 0;
while (tries < 3);
tries++;
The semicolon makes an empty loop body, so tries never changes. Remove the semicolon and add braces.
Bug 3: i is stuck
int i = 0;
while (i < 10) {
if (i % 2 == 0)
continue;
i++;
}
When i is even, continue skips i++, so i stays 0 forever. Update i before the test, or use a for loop.
Practice Challenges for C Loop Control Interview Questions
Write each program yourself and run it. A hint is given with each one.
- First negative reading. Store eight integers in an array and print the position of the first negative value, or a message if none exists. Hint:
breakand a variable declared before the loop. - Sum without multiples of 5. Add the numbers 1 to 30 but skip multiples of 5. Print the final sum. Hint:
continuewith the remainder operator. - Safe retry. Simulate a device that responds on a random attempt between 1 and 8 (use a fixed number instead of random). Retry up to 5 times and report success or failure. Hint:
breakand a success flag.
C Loop Control Interview Questions and Answers
These C loop control interview questions appear in fresher and embedded interviews. Give a short answer first, then an example.
1. What is the difference between break and continue?
break terminates the loop entirely and continues after it. continue skips only the rest of the current iteration and proceeds to the next one.
2. Write three ways to create an infinite loop in C.
while (1), for (;;) and do { } while (1);. All three run until a break, return or goto leaves them.
3. Why does a float loop with != often never end?
Decimal fractions such as 0.1 are not exact in binary. Repeated addition drifts, so the value passes the target without ever equaling it. Use an integer counter.
4. How does break behave in nested loops?
It leaves only the innermost loop that contains it. The outer loop continues normally.
5. How can you exit several nested loops at once?
Use a flag tested in each loop condition, a single goto to a label after the loops, or move the loops into a function and return.
6. Can continue be used inside a switch?
Yes, if the switch is inside a loop. It then applies to the loop and skips the rest of the loop body.
7. Where does continue jump in for and while?
In for it jumps to the update expression. In while and do...while it jumps to the condition test.
8. Why does for (uint8_t i = 0; i <= 255; i++) never finish?
The type cannot hold more than 255, so i <= 255 is always true. The counter wraps to 0 after 255.
9. What does while (cond); do?
The semicolon is an empty statement and forms the loop body. The loop repeats the empty statement while the condition is true, so the lines after it are not part of the loop.
10. Why does embedded firmware use while (1) in main?
There is no operating system to return to. The super loop runs the application forever, normally together with interrupts and a watchdog.
11. Is goto acceptable in C?
Used sparingly, yes. A single forward goto for error cleanup or leaving nested loops is common. Overuse makes code hard to follow.
12. What do coding standards say about break and continue?
Some standards limit them. For example MISRA C:2012 advises at most one break or goto to terminate a loop (Rule 15.4). Follow the standard your project uses.
Common Mistakes Seen in Interviews
| Mistake | Better answer |
|---|---|
Saying break exits all loops | It exits only the innermost loop or the switch |
Saying continue jumps to the top in every loop | In for it goes to the update, not the start of the body |
Comparing floats with == in examples | Count with an integer or use a tolerance |
Forgetting that while(1) is normal in firmware | Explain super loop and watchdog |
Summary and Key Takeaways
- An infinite loop in C comes from a condition that never turns false or an update that never runs.
breakleaves the nearest loop or switch.continueskips one pass.- Know where
continuelands in each loop type. - Exit nested loops with a flag,
gotoor a function return. - Practise C loop control interview questions by predicting output before you run the code.
Conclusion
If you can answer these C loop control interview questions without running the code and explain the interview answers in your own words, you are ready for the next topic of the free C programming course. Run every program from the last three lectures, change one thing at a time, and watch what the output does. That habit builds the instinct that interviewers test.
Frequently Asked Questions
What are common C loop control interview questions?
Interviewers ask about break versus continue, infinite loops, exiting nested loops, switch with break and why float equality fails.
How should I prepare for C interview questions on loops?
Predict the output of short programs by hand, then compile them with gcc to check. Repeat with small changes.
Are these questions useful for embedded C interviews?
Yes. They cover super loops, retry logic, data filtering and wrap-around bugs that are common in firmware.
Where can I practise C for free?
This free C programming course on EmbeddedPathashala has lectures, quizzes and interview questions for students.
Do I need hardware to try these programs?
No. Every program runs on a normal PC with gcc, and the same ideas apply to microcontrollers.
What should I learn after loop control?
Continue with arrays, functions and pointers in the next chapters of the embedded C course.
Keep learning with the free C course
EmbeddedPathashala teaches C programming for embedded systems for free, one small lecture at a time. Move on to the next lecture, or revisit the previous one and run the programs yourself.

2 Comments