C++

Iteration

break

Jumps out of the nearest enclosing loop or switch statement.

// print the numbers to a file, one per line
for (const int num : num_list) {
    errno = 0;
    fprintf(file, "%d\n", num);
    if (errno == ENOSPC) {
        fprintf(stderr, "no space left on device; output will be truncated\n");
        break;
    }
}

continue

Jumps to the end of the smallest enclosing loop.

int sum = 0;
for (int i = 0; i < N; i++) {
    int x;
    std::cin >> x;
    if (x < 0) continue;
    sum += x;
    // equivalent to: if (x >= 0) sum += x;
}

do

Introduces a do-while loop.

// Gets the next non-whitespace character from standard input
char read_char() {
    char c;
    do {
        c = getchar();
    } while (isspace(c));
    return c;
}

for

Introduces a for loop or, in C++11 and later, a range-based for loop.

// print 10 asterisks
for (int i = 0; i < 10; i++) {
    putchar('*');
}

while

Introduces a while loop.

int i = 0;
// print 10 asterisks
while (i < 10) {
    putchar('*');
    i++;
}

range-based for loop

std::vector<int> primes = {2, 3, 5, 7, 11, 13};

for(auto prime : primes) {
    std::cout << prime << std::endl;
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow