Learn one of the most powerful programming languages in the world and become a rockstar developer.
in this tutorial, you will learn about C for loop statement to execute a block of code repeatedly.
The C for loop statement is used to execute a block of code repeatedly. It is often used when the number of iterations is predetermined. If the number of iterations is not predetermined, we often use the while loop or do while loop statement.
The following illustrates the syntax of the for loop statement:
for (initialization_expression;loop_condition;increment_expression){ // execute statements }
There are three expressions separated by the semicolons ( ;) in the control block of the C for loop statement.
loop_condition
evaluates to false
.# Print numbers from 1 to 10 #include <stdio.h> int main() { int i; for (i = 1; i < 11; ++i) { printf("%d ", i); } return 0; }
// Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers #include <stdio.h> int main() { int num, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); // for loop terminates when num is less than count for(count = 1; count <= num; ++count) { sum += count; } printf("Sum = %d", sum); return 0; }