Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Break and Continue in C Loops | While Loop
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookBreak and Continue in C Loops

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
some-alt