Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære For, Continue | Control Statements
C Basics

bookFor, Continue

For Loop

The for loop incorporates the counter's initialization, the condition to exit the loop, and the counter's update.

main.c

main.c

copy
123
for (initialization; condition; update) { // Loop body }

Let's take the example from the previous lesson (displaying array elements) and reframe it using a for loop:

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int array[] = { 3, 6, 2, 134, 45, 2, 564, 8, 3, 531 }; for (int i = 0; i < 10; i++) { printf("Index of element: %d\tValue of element: %d\n", i, array[i]); } return 0; }

You can use any valid expression as the update counter. This means the counter can adapt based on any valid rule, such as (++x * 3) - 5. Furthermore, the counter can increment by characters instead of numbers.

Main.c

Main.c

copy
123456789
#include <stdio.h> int main() { for (char ch = 'a'; ch <= 'z'; ch++) { printf("ASCII value for %c is %d\n", ch, ch); } return 0; }
Note
Note

Keep in mind that characters are stored as numbers in memory.

Even if you leave out parts of the loop, like the counter initialization, the program will still compile. However, the semicolon ; remains essential:

Main.c

Main.c

copy
1234567891011
#include <stdio.h> int main() { int i = 0; // without counter initialization for (printf("It`s count initialization, but printf...\n"); i <= 4; i++) { printf("i = %d\n", i); } return 0; }

You can use the comma , to separate parts within the loop:

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int i; int n; for (i = 0, n = 5; i <= 5; i++, n++) { printf("i = %d\t n = %d\n", i, n); } return 0; }

Continue

In loops, both the break and continue commands come in handy. While the break statement forces an exit from the loop, the continue command prompts a skip to the subsequent iteration.

Consider a program designed to exclude negative integers, utilizing the continue operator:

Main.c

Main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int array[] = { 124, 235, -321, 32, 54, -549, 23445, 5534, -433 }; printf("Positive elements of array:\n"); for (int i = 0; i < 10; i++) { if (array[i] < 0) { continue; } printf("%d ", array[i]); } return 0; }

The following operators can also function as expressions to update the counter.

question mark

What is the effect of the continue statement in the loop shown below?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 2.63

bookFor, Continue

Stryg for at vise menuen

For Loop

The for loop incorporates the counter's initialization, the condition to exit the loop, and the counter's update.

main.c

main.c

copy
123
for (initialization; condition; update) { // Loop body }

Let's take the example from the previous lesson (displaying array elements) and reframe it using a for loop:

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int array[] = { 3, 6, 2, 134, 45, 2, 564, 8, 3, 531 }; for (int i = 0; i < 10; i++) { printf("Index of element: %d\tValue of element: %d\n", i, array[i]); } return 0; }

You can use any valid expression as the update counter. This means the counter can adapt based on any valid rule, such as (++x * 3) - 5. Furthermore, the counter can increment by characters instead of numbers.

Main.c

Main.c

copy
123456789
#include <stdio.h> int main() { for (char ch = 'a'; ch <= 'z'; ch++) { printf("ASCII value for %c is %d\n", ch, ch); } return 0; }
Note
Note

Keep in mind that characters are stored as numbers in memory.

Even if you leave out parts of the loop, like the counter initialization, the program will still compile. However, the semicolon ; remains essential:

Main.c

Main.c

copy
1234567891011
#include <stdio.h> int main() { int i = 0; // without counter initialization for (printf("It`s count initialization, but printf...\n"); i <= 4; i++) { printf("i = %d\n", i); } return 0; }

You can use the comma , to separate parts within the loop:

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int i; int n; for (i = 0, n = 5; i <= 5; i++, n++) { printf("i = %d\t n = %d\n", i, n); } return 0; }

Continue

In loops, both the break and continue commands come in handy. While the break statement forces an exit from the loop, the continue command prompts a skip to the subsequent iteration.

Consider a program designed to exclude negative integers, utilizing the continue operator:

Main.c

Main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int array[] = { 124, 235, -321, 32, 54, -549, 23445, 5534, -433 }; printf("Positive elements of array:\n"); for (int i = 0; i < 10; i++) { if (array[i] < 0) { continue; } printf("%d ", array[i]); } return 0; }

The following operators can also function as expressions to update the counter.

question mark

What is the effect of the continue statement in the loop shown below?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6
some-alt