Monday, April 28, 2025

C Tutorial - 12 - Break and Continue

Break examples

In C programming, the break keyword is used to terminate a loop prematurely. It can be used inside the body of a for, while, or do-while loop to exit the loop immediately, even if the loop condition has not been fully satisfied.

When break is encountered inside a loop, the program jumps to the next statement immediately after the loop, effectively ending the loop's execution. This is often useful when you want to stop iterating over a set of data based on a certain condition, or if you want to stop a loop once a certain value has been reached. 

#include <stdio.h>

int main()
{

    int x;

    for (x = 0; x <= 10; x++)
    {
        if (x == 5)
        {
            break;
        }
        printf("X at this moment: %d \n", x);
    }

    return 0;
}

Result:

X at this moment: 0
X at this moment: 1
X at this moment: 2
X at this moment: 3
X at this moment: 4

Process returned 0 (0x0)   execution time : 0.060 s
Press any key to continue.

This is a simple C program that uses a for loop to print the value of a variable called x from 0 to 10. Inside the loop, there is an if statement that checks if the value of x is equal to 5. If it is, the loop is terminated using the break keyword.

Here is a step-by-step explanation of how the program works:

  1. The variable x is declared and initialized to 0.
  2. The for loop is executed, starting with x at 0 and incrementing it by 1 each time the loop runs, until x reaches 10.
  3. Inside the loop, the if statement checks if x is equal to 5.
  4. If x is equal to 5, the break statement is executed, which terminates the loop.
  5. If x is not equal to 5, the printf statement is executed, which prints the current value of x.
  6. After the loop is terminated, the program ends by returning 0.

Therefore, when the program runs, it prints the value of x at each iteration of the loop until it reaches 5, at which point the loop is terminated and the program ends.

#include <stdio.h>

int main()
{

    int x = 0;

    while (x <= 10 )
    {
        if (x == 5)
        {
            break;
        }
        printf("X atm: %d \n", x);
        x++;
    }

    return 0;
}

Result: 

X atm: 0
X atm: 1
X atm: 2
X atm: 3
X atm: 4

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

 

Continue example

In C programming, the continue keyword is used to skip the current iteration of a loop and move on to the next iteration. It can be used inside the body of a for, while, or do-while loop to skip over specific parts of the loop's execution.

When continue is encountered inside a loop, the program skips the remaining statements inside the loop for the current iteration and jumps back to the beginning of the loop to start the next iteration. This is often useful when you want to skip certain iterations of a loop based on a certain condition.

#include <stdio.h>

int main()
{

    int x = 0;

    while (x <= 10 )
    {
        if (x == 5)
        {
            x++;
            continue;
        }
        printf("X atm: %d \n", x);
        x++;
    }

    return 0;
}

Result:

X atm: 0
X atm: 1
X atm: 2
X atm: 3
X atm: 4
X atm: 6
X atm: 7
X atm: 8
X atm: 9
X atm: 10

Process returned 0 (0x0)   execution time : 0.010 s
Press any key to continue.

This C program uses a while loop to print the value of a variable called x from 0 to 10, except for when x is equal to 5. Inside the loop, there is an if statement that checks if the value of x is equal to 5. If it is, the program increments x by 1 and skips the current iteration of the loop using the continue keyword. Otherwise, the program prints the value of x using printf, and increments x by 1.

Here's how the program works:

  1. The program starts by declaring and initializing an integer variable x to 0.
  2. The while loop is executed repeatedly as long as x is less than or equal to 10.
  3. Inside the loop, an if statement checks if the value of x is equal to 5. If it is, the program increments x by 1 and skips the current iteration of the loop using the continue keyword.
  4. If x is not equal to 5, the program prints the value of x using printf().
  5. The value of x is incremented by 1 in each iteration of the loop.
  6. Once x becomes greater than 10, the loop terminates and the program ends.

So the output of the program will be the sequence of numbers from 0 to 10, with the number 5 skipped

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...