Introduction
The break, continue, and goto statements change the normal flow of a C program. break is used to stop a loop or switch, continue skips the current loop iteration, and goto transfers control to a labeled statement. In this chapter, you will practice all three with beginner-friendly examples. The examples gradually move from simple loop control to searching, validation, menus, and practical program logic. break, continue and goto in C practice questions with solutions to help you understand the concepts.
Q1. Stop a Loop Using break
Problem Statement
Write a C program to print numbers from 1 to 10, but stop the loop when the number becomes 6.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i == 6)
{
break;
}
printf("%d\n", i);
}
return 0;
}
Sample Output
1
2
3
4
5
Explanation
The loop normally wants to run until 10.
When i becomes 6:
if (i == 6)
{
break;
}
The break statement immediately stops the loop.
Therefore, 6 and the remaining numbers are not printed.
Concepts Covered
forloopbreakif- Loop termination
Q2. Find the First Number Divisible by 7
Problem Statement
Write a C program to find the first number between 1 and 100 that is divisible by 7.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 100; i++)
{
if (i % 7 == 0)
{
printf("First number divisible by 7 = %d", i);
break;
}
}
return 0;
}
Sample Output
First number divisible by 7 = 7
Explanation
The loop checks numbers one by one.
When it reaches:
7 % 7 = 0
the condition becomes true.
The program prints 7 and then break stops the loop.
Concepts Covered
breakfor- Modulus operator
- Searching
- Loop termination
Q3. Skip Even Numbers Using continue
Problem Statement
Write a C program to print only odd numbers from 1 to 10 using continue.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
printf("%d\n", i);
}
return 0;
}
Sample Output
1
3
5
7
9
Explanation
When the number is even:
if (i % 2 == 0)
{
continue;
}
continue skips the remaining statements of the current iteration.
For example, when i = 2, the printf() statement is skipped.
The loop then moves to the next iteration.
Concepts Covered
continuefor- Modulus operator
- Odd numbers
- Skipping iterations
Q4. Print Numbers Except Multiples of 3
Problem Statement
Write a C program to print numbers from 1 to 20, but skip all numbers divisible by 3.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 20; i++)
{
if (i % 3 == 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Sample Output
1 2 4 5 7 8 10 11 13 14 16 17 19 20
Explanation
The condition:
i % 3 == 0
checks whether the number is divisible by 3.
If it is divisible by 3, continue skips that number.
For example:
3 → skipped
6 → skipped
9 → skipped
12 → skipped
Concepts Covered
continue- Modulus
- Loop filtering
forloop
Q5. Stop Input When the User Enters 0
Problem Statement
Write a C program that repeatedly accepts numbers from the user. The program should stop when the user enters 0.
C Program
#include <stdio.h>
int main()
{
int number;
while (1)
{
printf("Enter a number (0 to stop): ");
scanf("%d", &number);
if (number == 0)
{
break;
}
printf("You entered: %d\n", number);
}
printf("Program ended.");
return 0;
}
Sample Output
Enter a number (0 to stop): 15
You entered: 15
Enter a number (0 to stop): 25
You entered: 25
Enter a number (0 to stop): 8
You entered: 8
Enter a number (0 to stop): 0
Program ended.
Explanation
The condition:
while (1)
creates a loop that continues until something stops it.
When the user enters 0:
if (number == 0)
{
break;
}
The break statement exits the loop.
Concepts Covered
whilebreak- User input
- Sentinel value
- Infinite loop with controlled exit
Q6. Skip Negative Numbers
Problem Statement
Write a C program that accepts 5 numbers and calculates the sum of only the positive numbers. Negative numbers should be skipped using continue.
C Program
#include <stdio.h>
int main()
{
int i;
int number;
int sum = 0;
for (i = 1; i <= 5; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &number);
if (number < 0)
{
continue;
}
sum = sum + number;
}
printf("Sum of positive numbers = %d", sum);
return 0;
}
Sample Output
Enter number 1: 10
Enter number 2: -5
Enter number 3: 20
Enter number 4: -3
Enter number 5: 15
Sum of positive numbers = 45
Explanation
When the user enters a negative number:
if (number < 0)
{
continue;
}
The current iteration ends.
Therefore, the negative number is not added to sum.
The positive numbers are:
10 + 20 + 15 = 45
Concepts Covered
continuefor- User input
- Conditional filtering
- Sum
Q7. Use break in a switch Menu
Problem Statement
Create a simple menu using do-while and switch. The program should continue displaying the menu until the user selects 3.
C Program
#include <stdio.h>
int main()
{
int choice;
do
{
printf("\n===== MENU =====\n");
printf("1. Add\n");
printf("2. Display Message\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Addition selected.");
break;
case 2:
printf("Hello! Welcome to C programming.");
break;
case 3:
printf("Exiting program...");
break;
default:
printf("Invalid choice.");
}
} while (choice != 3);
return 0;
}
Sample Output
===== MENU =====
1. Add
2. Display Message
3. Exit
Enter your choice: 2
Hello! Welcome to C programming.
===== MENU =====
1. Add
2. Display Message
3. Exit
Enter your choice: 3
Exiting program...
Explanation
There are two different uses of break here.
Inside switch:
case 1:
printf("Addition selected.");
break;
The break exits the switch.
The do-while loop itself stops when:
choice == 3
So break and the loop condition perform different jobs.
Concepts Covered
switchbreakdo-while- Menu-driven programming
- Loop control
Q8. Use goto to Repeat an Input
Problem Statement
Write a C program that asks the user to enter a positive number. If the user enters zero or a negative number, use goto to ask again.
C Program
#include <stdio.h>
int main()
{
int number;
input:
printf("Enter a positive number: ");
scanf("%d", &number);
if (number <= 0)
{
printf("Invalid input. Please try again.\n");
goto input;
}
printf("Valid number = %d", number);
return 0;
}
Sample Output
Enter a positive number: -10
Invalid input. Please try again.
Enter a positive number: 0
Invalid input. Please try again.
Enter a positive number: 25
Valid number = 25
Explanation
This statement creates a label:
input:
When the input is invalid:
goto input;
transfers program control back to the input label.
When the user finally enters a positive number, the goto statement is not executed and the program continues normally.
Concepts Covered
goto- Label
- Input validation
- Conditional jump
Q9. Use goto to Exit Nested Loops
Problem Statement
Write a C program with nested loops that searches for the number 7. When 7 is found, use goto to exit both loops.
C Program
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
{
if (i * j == 7)
{
printf("Found 7 at i = %d, j = %d\n", i, j);
goto found;
}
}
}
found:
printf("Search completed.");
return 0;
}
Sample Output
Found 7 at i = 1, j = 7
Search completed.
Important Correction
The nested loops above have j only from 1 to 5, so i * j can be 7 only when i or j is 7, which is outside the range.
A correct version is:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 7; i++)
{
for (j = 1; j <= 7; j++)
{
if (i * j == 7)
{
printf("Found 7 at i = %d, j = %d\n", i, j);
goto found;
}
}
}
found:
printf("Search completed.");
return 0;
}
Sample Output
Found 7 at i = 1, j = 7
Search completed.
Explanation
A break inside the inner loop would only exit the inner loop.
The goto jumps directly to:
found:
and therefore skips the remaining iterations of both loops.
This demonstrates one possible use of goto, although structured alternatives are often preferable in normal application code.
Concepts Covered
goto- Labels
- Nested loops
- Searching
- Multiple-loop control
Q10. Create a Number Checking Program Using break and continue
Problem Statement
Write a C program that checks numbers from 1 to 20.
- Skip even numbers using
continue. - Stop the loop when the number reaches
15. - Print the odd numbers before
15.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 20; i++)
{
if (i == 15)
{
break;
}
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Sample Output
1 3 5 7 9 11 13
Explanation
The loop starts from 1.
Even numbers are skipped:
if (i % 2 == 0)
{
continue;
}
When i becomes 15:
if (i == 15)
{
break;
}
The entire loop stops.
Therefore:
1 3 5 7 9 11 13
are printed.
Concepts Covered
breakcontinueforif- Modulus operator
- Multiple loop-control statements
Key Takeaways
breakcompletely stops the nearest enclosing loop orswitch.continueskips the current loop iteration.breakdoes not automatically exit all nested loops.continueis useful for filtering unwanted values.gototransfers control to a label within the same function.- A label is followed by a colon, such as
start:. breakandcontinueare common tools for controlling loops.gotois part of standard C, but it should be used carefully.breakis useful for stopping a search as soon as the required value is found.continueis useful when certain inputs should be ignored.- Understanding these statements makes loops easier to control in larger C programs.
FAQs
1. What is break in C?
break immediately terminates the nearest enclosing loop or switch statement.
Example:
if (number == 0)
{
break;
}
2. What is continue in C?
continue skips the remaining statements of the current iteration and moves to the next iteration of the nearest enclosing loop.
3. What is the difference between break and continue?
break stops the entire loop.
continue only skips the current iteration and allows the loop to continue.
4. Can break be used inside a switch statement?
Yes. It is commonly used to prevent execution from continuing into the next case.
switch (choice)
{
case 1:
printf("Add");
break;
}
5. Can continue be used inside a switch statement?
continue applies to a loop, not to a switch by itself. If a switch is inside a loop, a continue can affect that enclosing loop.
6. What is goto in C?
goto transfers program control to a labeled statement within the same function.
Example:
goto start;
start:
printf("Hello");
7. Is goto recommended for every C program?
No. It is better to use structured control flow in most situations. However, goto is still a valid part of C and can be useful for specific situations, such as exiting multiple levels of nested processing or performing cleanup in some low-level C code.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
