Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookIntroduction to the For Loop in C

Swipe to show menu

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt