

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Memory Management
incomplete
2: C Program Structure
incomplete
3: Interpreted Quiz
incomplete
4: C Is Compiled
incomplete
5: Comments
incomplete
6: Basic Types
incomplete
7: Strings
incomplete
8: Printing Variables
incomplete
9: Compilation: Types
incomplete
10: Variables
incomplete
11: Constants
incomplete
12: Functions
incomplete
13: Void
incomplete
14: Unit Tests
incomplete
15: Math Operators
incomplete
16: If Statements
incomplete
17: Logical Operators
incomplete
18: Ternary
incomplete
19: Type Sizes
incomplete
20: Sizeof
incomplete
21: For Loop
incomplete
22: While Loop
incomplete
23: Do While Loop
incomplete
24: Pragma Once and Header Guards
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A for loop in C is a control flow statement for repeated execution of a block of code. Very similar to Python, but with a different syntax.
The syntax of a for loop in C consists of three main parts:
Initialization
Condition
Final-expression.
There is no "for each" (iterables) in C. For example, there is no way to do:
for car in cars:
print(car)Instead, we have to iterate over the numbers of indices in a list, and then we can access the item using the index.
for (initialization; condition; final-expression) {
// Loop Body
}for LoopInitialization
Executed only once at the beginning of the loop.
Is typically used to initialize the loop counter: int i = 0; for example
Condition
Checked before each iteration.
If true, execute the body. If false, terminate the loop
Often checks to ensure i is less than some value: i < 5; for example
Final-expression
Executed after each iteration of the loop body.
Can be used to update the loop counter or run any other code: i++ for example
Loop Body
The block of code that is executed while the condition is true.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
// Prints:
// 0
// 1
// 2
// 3
// 4Implement the print_numbers prototyped in exercise.h that takes a starting number and an ending number and prints all the numbers in that range inclusive (using a for-loop).