Continue Statement in C Language with Examples

continue-statement-in-c-language-with-example-programs

Introduction:

We have looked at the loops and break Statement in earlier posts. In today’s article, We are going to discuss about the Continue Statement in C language and how we can use it. Let’s get started.

Pre-Requisites:

It is recommended to have minimal knowledge of the loops in C. Please go through the following articles.

Continue Statement in C Language:

The Continue statement is used to skip the present loop iteration and takes the program control back to the loop condition for the next iteration.

The continue statement is kind of the opposite of the break statement. The break statement is used to terminate the loop completely and control goes out of the loop. The Continue statement is used to skip the current iteration but the loop won’t terminate, the loop will proceed to the next iteration of the loop.

We use the continue statement in the loops.

Syntax of Continue statement:

Here is the syntax of the continue statement with the while loop

As you can see from the above syntax, As soon as the continue statement is executed, control goes back to the while condition and starts executing the next iteration (if the while condition is still true)

The flow diagram of the Continue Statement:

continue-statement-in-c-language-with-example-programs

Here is another graphical representation of the continue statement flow.

continue-statement-flow-diagram

Example program to understand the Continue statement in C language:

Let’s write a C Program Sum of Even Numbers from 1 to 20.

Program Output:

Sum of Even Numbers Program Algorithm:

  • We have used two variables counter and sum. The counter variable is used to iterate from 1 to 20 and the sum variable is used to store the sum so far. The sum variable will be initialized with zero
  • We use the for loop to iterate from 1 to 20 with the help of a counter variable. The counter variable starts with the value 1 and for each iteration counter value will increase by 1.
  • For each iteration,
    • We will check if the counter is an even or odd number. As our program is to calculates the sum of the even numbers, We don’t really worry about the odd numbers so we can skip the odd numbers.
    • So First check inside the for loop is to verify if the counter value is even or odd number. We used the Modulus operator ( counter % 2 == 1 ) to check the number. If the above condition returns true then the counter is an Odd number. If the counter % 2 == 1 becomes false then the counter is an Even number.
    • If the counter is an odd number, Then we no need to add the value to the sum, So we can skip this iteration. To do that, We can use the continue statement. which skips the present iteration and proceeds to the next iteration. ( Make sure to increment the loop control variable i.e counter)
    • If the counter is an Even number, Then we add the counter value to the existing sum value using the sum = sum + counter statement, and control goes to the next iteration. ( Make sure to increment the counter variable)
    • This process continues until the counter reaches the value 21. Once the counter reached 21 the for loop condition counter <= 20 becomes false. and the loop will be terminated.
  • Once the loop is terminated, we are printing the result of the sum using the printf function.

📢 Here we used the continue statement in c programming to skip the iteration where the counter value became an Odd number. This way we can take advantage of the continue statement to skip the unnecessary iteration based on our program logic.

Continue Statement with Different Loops:

Now Let’s look at how we can use the continue statement with different loops.

  • continue statement with for loop
  • continue statement with a while loop
  • continue statement with do while loop

Continue statement with the for loop in C language:

We have already used the for loop in the above Sum of Even Numbers program.

Syntax:

Let’s write a program to print the Odd numbers from 1 to 10 using the continue statement in c programming.

Program to print Odd numbers from 1 to 10 using Continue Statement.

Program Output:

Here we used the continue statement to skip the iteration, So whenever we got an Even value for the counter (i.e i), We skipped the iteration using the continue statement and went to the next iteration.

If the i value is an Odd number, Then only we proceeded further and printed the value onto the console.

Continue Statement with the while loop in C:

As specified earlier we can use the continue statement with any loop Here is an example of the while loop and the continue statement.

Let’s write a program to add all Odd numbers from 1 to 10 using the continue statement. We have looked at the Sum of Even number the program above using the for loop, This program is the opposite of that program.

Sum of Odd Numbers using Continue Statement and while loop:

Program Output:

As you can see from the above program, We used the continue statement to skip the iterations where the counter(i.e i) is even number.

📢 Make sure to increment the counter variable i.e i before using the continue statement. If you forgot to increment the counter variable, then we might get the Infinite loop.

Continue Statement with the do while loop in C programming:

Similar to other loops, We can use the continue statement in do while loop and skip the specific iteration.

Let’s look at an example program. We want to print the numbers from 90 to 100 except the 93 and 97 numbers.

Do While loop with Continue Statement Example Program (skip numbers):

Program Output:

As you can see from the above output, We haven’t printed the values 93 and 97. and printed the remaining numbers from the 90 to 100.

Here also, We used the continue statement to skip the loop iteration, whenever the i value is equal to either of 93 or 97 numbers.

Continue Statement with the Nested loops in C:

If we use the continue statement in the nested loops, Then only the Inner-most loop iteration will be skipped. It won’t affect the OuterLoop iterations.

Please have a look at the following program.

Nested loops flow with Continue Statement:

Program Output:

Program Explanation:

As you can see from the above output, We have a continue statement in the body of the InnerLoop. We are checking if the InnerLoop counter (i.e j) is even or odd number. If j value is an even number, Then we are skipping that specific iteration of InnerLoop using the continue statement.

Please note that, the OuterLoop iterations are not skipped due to the InnerLoop continue statement.

📢 The InnerLoop continue statement won’t affect the iterations of the OuterLoop.

Conclusion:

We covered the Continue statement in C language with examples in today’s article. We also discussed the various uses of the continue statement. Finally, we explored the nested loops and the continue statement.

C Tutorials Index Page:

C Loops Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] Continue Statement in C Language […]

  2. […] loop starts by initializing i to 0. It will continue looping as long as the current character ( inputStr[i]) is not a NULL character ( ''). At […]

  3. […] a For loop by initializing variable i with 0. It will continue looping as long as the current character ( inputStr[i]) is not a NULL character ( […]

  4. […] of the character in input string. The For loop starts by initializing variable i to 0. It will continue looping as long as the present character ( inputStr[i]) is not a NULLcharacter ( […]

Leave a Reply