6.8: The continue Statement

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    6.8: The continue Statement

    The continue statement skips the rest of the current loop iteration and jumps back to the top of the loop. In a for loop, MATLAB then moves on to the next value of the loop variable. In a while loop, MATLAB checks the loop condition again.

    Example: Display Multiples of 7

    The following program displays numbers from 1 through 50 that are divisible by 7. If a number is not divisible by 7, continue skips the fprintf statement.

    for n = 1:50
        if mod(n,7) ~= 0
            continue;
        end
        fprintf('%d is divisible by 7!', n);
    end
    

    The function mod(n,7) returns the remainder when n is divided by 7. If the remainder is not zero, then n is not divisible by 7, so the program skips to the next value of n.

    Caution

    continue can be useful, but use it carefully. Too many break and continue statements can make code harder to read.


    This page titled 6.8: The continue Statement was last modified on Tue, 30 Jun 2026 02:11:14 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.