Understanding Break and Continue Statements in C Programming

Hello, coding genius!
Now, we’re going to learn about two special tools called Break and Continue. These statements give you extra control over your loops, allowing you to either stop a loop early or skip certain parts of a loop. They’re super useful when you need to make decisions inside loops!


What is the Break Statement?

The Break statement is used to exit a loop immediately, no matter what the loop’s condition is. When the Break statement is encountered, the loop stops running, and the program moves on to the next part of the code.

Syntax of Break Statement

Here’s how you use the Break statement in a loop:

In this example, the loop will print numbers from 1 to 4, and then it will stop when i becomes 5.

Output:


Real-Life Example of Break

Imagine you’re looking for a book on a shelf. You keep checking the books one by one, but as soon as you find the book you’re looking for, you stop searching. This is similar to how Break works—it stops the loop when a certain condition is met.

Here’s a program where the loop breaks when it finds a number divisible by 7:

Output:

1 is not divisible by 7
2 is not divisible by 7
3 is not divisible by 7
4 is not divisible by 7
5 is not divisible by 7
6 is not divisible by 7
Found a number divisible by 7: 7

What is the Continue Statement?

The Continue statement is used to skip the current iteration of the loop and jump to the next one. Unlike Break, it doesn’t stop the loop entirely—it just skips whatever code comes after it in that iteration.

Syntax of Continue Statement

Here’s how you use the Continue statement in a loop:

In this example, the loop will print all numbers from 1 to 5, except for 3, because the Continue statement tells the loop to skip that iteration.

Output:


Real-Life Example of Continue

Imagine you’re counting from 1 to 5, but you decide to skip the number 3. You don’t stop counting; you just ignore 3 and move on to 4. That’s exactly how Continue works—it skips one loop iteration but doesn’t stop the loop.

Here’s a program where we print all numbers from 1 to 10 except the even numbers:

Output:


Break vs. Continue

  • Break: Completely stops the loop when it’s encountered.
  • Continue: Skips the current iteration and moves to the next one.

Here’s a comparison:

Output:


Fun Example: Skipping Numbers

Let’s write a program where we use both Break and Continue in a fun way. We’ll print numbers from 1 to 10 but:

  • Skip the number 3.
  • Stop the loop when we reach the number 7.

Output:


Challenge Time!

Write a program that prints numbers from 1 to 10, but:

  • Skip numbers that are divisible by 4.
  • Stop the loop if a number is divisible by 8.

Here’s the solution:

Output:


Summary

  • Break: Exits a loop entirely when a condition is met.
  • Continue: Skips the current iteration and moves to the next one.
  • Break is useful when you want to stop a loop early, and Continue is useful when you want to skip over part of the loop.

With Break and Continue, you can add even more control to your loops, making your programs smarter and more flexible. Keep practicing, and soon you’ll master how to control the flow of your loops!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?