Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Introduction to the For Loop in C | For Loop
C Loops for Beginners

bookIntroduction to the For Loop in C

The for loop in C is a powerful way to repeat a block of code a specific number of times. Its structure is compact and especially useful for counting and iterating over a range of values.

The anatomy of a for loop consists of three main parts: initialization, condition, and increment. These parts are placed inside the parentheses following the for keyword, separated by semicolons. Each plays an essential role in controlling the loop's execution.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { // Print the first 10 even numbers using a for loop int i; for (i = 1; i <= 10; i++) { // initialization: i = 1; condition: i <= 10; increment: i++ printf("%d\n", i * 2); } return 0; }

In the example above, the initialization (i = 1) sets up the loop variable before the loop starts. The condition (i <= 10) is checked before each iteration; as long as it remains true, the loop continues.

The increment (i++) updates the loop variable after every iteration. This pattern lets you control the number of times the loop runs. The loop variable changes with each repetition, and when the condition becomes false, the loop stops. This makes for loops ideal for tasks where you know in advance how many times you need to repeat an action.

Note
Note

Use a for loop when you know exactly how many times you want to repeat a block of code.

question mark

Which of the following for loop structures contains an error?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you show me a basic example of a for loop in C?

What are some common mistakes to avoid when using for loops?

How does a for loop differ from a while loop in C?

bookIntroduction to the For Loop in C

Pyyhkäise näyttääksesi valikon

The for loop in C is a powerful way to repeat a block of code a specific number of times. Its structure is compact and especially useful for counting and iterating over a range of values.

The anatomy of a for loop consists of three main parts: initialization, condition, and increment. These parts are placed inside the parentheses following the for keyword, separated by semicolons. Each plays an essential role in controlling the loop's execution.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { // Print the first 10 even numbers using a for loop int i; for (i = 1; i <= 10; i++) { // initialization: i = 1; condition: i <= 10; increment: i++ printf("%d\n", i * 2); } return 0; }

In the example above, the initialization (i = 1) sets up the loop variable before the loop starts. The condition (i <= 10) is checked before each iteration; as long as it remains true, the loop continues.

The increment (i++) updates the loop variable after every iteration. This pattern lets you control the number of times the loop runs. The loop variable changes with each repetition, and when the condition becomes false, the loop stops. This makes for loops ideal for tasks where you know in advance how many times you need to repeat an action.

Note
Note

Use a for loop when you know exactly how many times you want to repeat a block of code.

question mark

Which of the following for loop structures contains an error?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1
some-alt