Continue Statement

The Continue statement gives you the option to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop iterations. That is, the current iteration of the loop will be disrupted, but the program will return to the top of the loop. The Continue statement will be within the block of code under the Loop statement, usually after a conditional if statement.

The continue statement (in most computer programming languages), takes the generic form of:

WHILE Boolean expression
statement_1
IF Boolean expression THEN
CONTINUE
ENDIF
statement_2
counter = counter + 1
ENDWHILE
FOR counter in range(n)
statement_1
IF Boolean expression THEN
CONTINUE
ENDIF
statement_2
ENDFOR

In this example program, the user is asked to enter a positive integer and the program will count how many times it goes through the loop until it reaches that number, except it will always skip 5!

Top-Down Design for the Continue Statement

Top-Down Design for Continue Statement

Flowchart for the Continue Statement

Continue Statement flowchart

Pseudocode for the Continue Statement

GET positive_integer
WHILE (counter < positive_integer)
counter = counter - 1
IF (counter + 1 == 5) THEN
CONTINUE
ENDIF
SHOW counter + 1
ENDFOR

Code for the Continue Statement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program uses a for loop

#include <stdio.h>

int main() {
    // this function uses a for loop
    int positiveInteger;

    // input
    printf("Enter how many times to repeat: ");
    scanf("%d", &positiveInteger);
    printf("\n");

    // process & output
    while (positiveInteger >= 0) {
        // yes, this is the exception on placing the counter at the top
        // if you did not, then there would be an infinit loop

        positiveInteger--;
        if (positiveInteger + 1 == 5) {
            continue;
        }
        printf("Current variable value: %d\n", positiveInteger + 1);
    }

    printf("\nDone.\n");
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program uses a for loop

#include <iostream>

int main() {
    // this function uses a for loop
    int positiveInteger;

    // input
    std::cout << "Enter how many times to repeat: ";
    std::cin >> positiveInteger;
    std::cout << std::endl;

    // process & output
    while (positiveInteger >= 0) {
        // yes, this is the exception on placing the counter at the top
        // if you did not, then there would be an infinit loop

        positiveInteger--;
        if (positiveInteger + 1 == 5) {
            continue;
        }
        std::cout << "Current variable value: " <<
                      positiveInteger + 1 << std::endl;
    }

    std::cout << "\nDone." << std::endl;
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program uses a for loop
*/

using System;

/*
 * The Program class
*/
class Program {
    static void Main() {
        // this function uses a do ... while loop

        int positiveInteger;

        // input
        Console.Write("Enter how many times to repeat: ");
        positiveInteger = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine();

        // process & output
        while (positiveInteger >= 0) {
            // yes, this is the exception on placing the counter at the top
            // if you did not, then there would be an infinit loop

            positiveInteger--;
            if (positiveInteger + 1 == 5) {
                continue;
            }
            Console.WriteLine("Current variable value: " + (positiveInteger + 1));
        }

        Console.WriteLine("\nDone.");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program uses a continue statement
 */

package main

import (
	"fmt"
)

func main() {
	// this function uses a continue statement

	var positiveInteger int

	// input
	fmt.Print("Enter a count-down number (ex: 10): ")
	fmt.Scan(&positiveInteger)
	fmt.Println()

	// process & output
	for positiveInteger >= 0 {
		// yes, this is the exception on placing the counter at the top
		// if you did not, then there would be an infinite loop

		positiveInteger--

		if positiveInteger+1 == 5 {
			continue
		}
		fmt.Printf("Current variable value: %d\n", positiveInteger+1)
	}

	fmt.Println("\nDone.")
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * This program uses a continue statement
 *
 * @author  Mr Coxall
 * @version 1.0
 * @since   2020-09-01
 */

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // this function uses a continue statement

    // create Scanner object for user input
    Scanner scanner = new Scanner(System.in);

    // input
    System.out.print("Enter a count-down number (ex: 10): ");
    String positiveIntegerStr = scanner.nextLine();
    System.out.println();

    // process & output
    int positiveInteger = Integer.parseInt(positiveIntegerStr);

    while (positiveInteger >= 0) {
      // yes, this is the exception on placing the counter at the top
      // if you did not, then there would be an infinit loop

      positiveInteger--;
      if (positiveInteger + 1 == 5) {
        continue;
      }
      System.out.println("Current variable value: %d".formatted(positiveInteger + 1));
    }

    // close the Scanner object
    scanner.close();
    System.out.println("\nDone.");
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program uses a continue statement
 */

const prompt = require("prompt-sync")()

let counter = 0

// input
const positiveIntegerStr = prompt("Enter a count-down number (ex: 10): ")
console.log("")

// process & output
let positiveInteger = parseInt(positiveIntegerStr)

while (positiveInteger >= 0) {
  // yes, this is the exception on placing the counter at the top
  // if you did not, then there would be an infinit loop

  positiveInteger--
  if (positiveInteger + 1 == 5) {
    continue
  }
  console.log(`Current variable value: ${positiveInteger + 1}`)
}

console.log("\nDone.")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
"""
Created by: Mr. Coxall
Created on: Sep 2020
This module uses a continue statement
"""


def main() -> None:
    """The main() function uses a continue statement, returns None."""

    # input
    positive_integer = int(input("Enter a count-down number (ex: 10): "))
    print("")

    # process & output
    while positive_integer >= 0:
        # yes, this is the exception on placing the counter at the top
        # if you did not, then there would be an infinit loop
        positive_integer -= 1
        if positive_integer + 1 == 5:
            continue
        print(f"Current variable value: {positive_integer + 1}")

    print("\nDone.")


if __name__ == "__main__":
    main()

Example Output

Code example output