for Loop in C: Usage and Examples
Table of Content:
A for loop in C is a control flow statement that allows you to iterate over a range of values. It is typically used to execute a block of code a certain number of times, or until a certain condition is met.
In C, you can repeatedly execute a sequence of code in several ways. These looping capabilities are the subject of this chapter, and they consist of the following:
- The for statement
- The while statement
- The do while statement
Syntax of for loop
for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Statement(s); }
for (i = initialValue; i < endValue ; i++) { // Loop body; Statement(s); }
Similar Syntax while loop
i = initialValue; // Initialize loop control variable while (i < endValue) { // Loop body ... i++; // Adjust loop control variable }
Similar Syntax do while loop
i = initialValue; // Initialize loop control variable do{ // Loop body ... i++; // Adjust loop control variable }while (i < endValue) ;
The for loop allows many variations, but its most common form works like this:
The initialization is an assignment statement that is used to set the loop control variable.
The condition is a relational expression that determines when the loop exits.
The increment defines how the loop control variable changes each time the loop is repeated.
You must separate these three major sections by semicolons.The for loop continues to execute as long as the condition is true. Once the condition becomes false, program execution resumes on the statement following the for.
flowchart of the for loop
A for loop performs an initial action once, then repeatedly executes the statements in the loop body, and performs an action after an iteration when the loop-continuation-condition evaluates to true.
Example of for Loop
#include void main() { int i; for (i = 0; i < 100; i++) { printf("Welcome to atnyla! \n"); } }
The above code is written in C programming language. The code includes a 'for loop' which is used to execute a block of code repeatedly. The 'for loop' starts with the keyword 'for
', followed by an initialization statement 'i=0'
, a termination condition 'i<100', and an increment or decrement statement 'i++
' inside the parenthesis. The initialization statement initializes the loop variable 'i
' to 0
. The termination condition checks whether the loop variable is less than 100, and if it is true, the code inside the loop will execute. The increment statement increments the value of the loop variable by 1 after each iteration of the loop.
The code inside the for loop is "printf("Welcome to atnyla! \n");
" which will print the "Welcome to atnyla!" on the screen and '\n' is used to change the line after each execution.
The for loop will execute 100 times, and each time it will print the "Welcome to atnyla!" on the screen.
Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Welcome to atnyla! Press any key to continue . . .
Example of for Loop
#include void main() { int i; for(i=1;i<=10;i++){ printf("%d \n",i); } }
1 2 3 4 5 6 7 8 9 10 Press any key to continue . . .
Another Example of for Loop
#include void main() { int x; for(x = 15; x < 25; x = x + 1) { printf("value of x : %d",x); printf("\n"); } }
value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 value of x : 20 value of x : 21 value of x : 22 value of x : 23 value of x : 24 Press any key to continue . . .