Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Break and Continue in C Loops | While Loop
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Loops for Beginners

bookBreak and Continue in C Loops

Note
Definition

Loop control statements like break and continue change the normal flow of loop execution. They help make code more efficient by allowing early exits or skipping unnecessary steps, but overusing them can make code harder to read.

When working with loops in C, you often need to control how and when the loop executes or terminates. Two important statements for this purpose are break and continue.

The break statement is used to immediately exit a loop, while the continue statement skips the current iteration and proceeds to the next one. These keywords are especially useful in situations where you want to halt the loop early or skip specific values based on conditions.

main.c

main.c

copy
12345678910111213141516171819
#include <stdio.h> void searchValue(int arr[], int size, int target) { for (int i = 0; i < size; i++) { if (arr[i] == target) { printf("Found %d at index %d\n", target, i); break; // Exit loop immediately after finding the target } } } int main() { int numbers[] = {3, 5, 8, 2, 9, 4}; int size = sizeof(numbers) / sizeof(numbers[0]); searchValue(numbers, size, 8); return 0; }

Typical use cases for break:

  • Exiting a loop when a target value is found;
  • Stopping a search once a condition is met.
main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> void printEvenNumbers(int arr[], int size) { printf("Even numbers: "); for (int i = 0; i < size; i++) { if (arr[i] % 2 != 0) continue; // Skip odd numbers printf("%d ", arr[i]); } printf("\n"); } int main() { int numbers[] = {3, 5, 8, 2, 9, 4}; int size = sizeof(numbers) / sizeof(numbers[0]); printEvenNumbers(numbers, size); return 0; }

Typical use cases for continue:

  • Skipping over certain elements in a collection;
  • Ignoring specific cases without leaving the loop entirely.

Using break and continue can greatly affect the flow of your loops. The break statement ends the loop immediately, so any code after the break inside the loop does not execute. This is ideal when you are searching for something and want to stop as soon as you find it.

In contrast, continue only skips the current iteration, so the loop keeps running for the remaining elements. This is useful when you want to ignore certain cases but still process the rest. Choose break when you want to stop looping altogether, and continue when you want to skip just one pass and keep going.

question mark

Which statement should you use if you want to skip processing certain elements in a loop, but continue checking the rest

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you give examples of how to use break and continue in C?

What are some common mistakes when using break and continue?

How do break and continue behave in nested loops?

bookBreak and Continue in C Loops

Sveip for å vise menyen

Note
Definition

Loop control statements like break and continue change the normal flow of loop execution. They help make code more efficient by allowing early exits or skipping unnecessary steps, but overusing them can make code harder to read.

When working with loops in C, you often need to control how and when the loop executes or terminates. Two important statements for this purpose are break and continue.

The break statement is used to immediately exit a loop, while the continue statement skips the current iteration and proceeds to the next one. These keywords are especially useful in situations where you want to halt the loop early or skip specific values based on conditions.

main.c

main.c

copy
12345678910111213141516171819
#include <stdio.h> void searchValue(int arr[], int size, int target) { for (int i = 0; i < size; i++) { if (arr[i] == target) { printf("Found %d at index %d\n", target, i); break; // Exit loop immediately after finding the target } } } int main() { int numbers[] = {3, 5, 8, 2, 9, 4}; int size = sizeof(numbers) / sizeof(numbers[0]); searchValue(numbers, size, 8); return 0; }

Typical use cases for break:

  • Exiting a loop when a target value is found;
  • Stopping a search once a condition is met.
main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> void printEvenNumbers(int arr[], int size) { printf("Even numbers: "); for (int i = 0; i < size; i++) { if (arr[i] % 2 != 0) continue; // Skip odd numbers printf("%d ", arr[i]); } printf("\n"); } int main() { int numbers[] = {3, 5, 8, 2, 9, 4}; int size = sizeof(numbers) / sizeof(numbers[0]); printEvenNumbers(numbers, size); return 0; }

Typical use cases for continue:

  • Skipping over certain elements in a collection;
  • Ignoring specific cases without leaving the loop entirely.

Using break and continue can greatly affect the flow of your loops. The break statement ends the loop immediately, so any code after the break inside the loop does not execute. This is ideal when you are searching for something and want to stop as soon as you find it.

In contrast, continue only skips the current iteration, so the loop keeps running for the remaining elements. This is useful when you want to ignore certain cases but still process the rest. Choose break when you want to stop looping altogether, and continue when you want to skip just one pass and keep going.

question mark

Which statement should you use if you want to skip processing certain elements in a loop, but continue checking the rest

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
some-alt