
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 . . .
- Question 1: Differentiate between for loop and a while loop? What are its uses?
- Question 2: Explain the syntax for for loop.
- Question 3: What is a nested loop?
- Question 4: Write a loop statement that will show the following output:112123123412345
Related Questions
- Assignment 1: do while loop in C Programming language
- Assignment 2: For loop Example Program in C Programming Language
- Assignment 3: Nested For Loop in C
- Assignment 4: for loop with multiple test conditions
- Assignment 5: C program to find the sum of first n natural numbers.
- Assignment 6: Write a program to print the sum of the series 1+2+3+4+... up to n terms.
- Assignment 7: Write a program that prints the sum of digits of a number using for loop.
- Assignment 8: C Program - Write a program to find the factorial of a number using for loop
- Assignment 9: C program to print the multiplication table of 6 from 1 to 10 using for loop.
- Assignment 10: Write a program that prints out a multiplication table.
- Assignment 11: Write a program that will compute the binary equivalent number of a decimal number and print it as output.
- Assignment 12: Write a program that will compute the Fibonacci series and print it as output.
- Assignment 13: Write a program that will search and find out the position where the given key element exist in a user chosen array and print it as output.
- Assignment 14: Nested for Loop in C Programming Language
- Assignment 15: C program break statement in for loop
- Assignment 16: continue statement in for loop
- Assignment 17: C program to print square or rectangle star pattern
***** ***** ***** ***** *****
- Assignment 18: C program to print square or rectangle star pattern
******************** ******************** ******************** ******************** ********************
- Assignment 19: C program to print hollow square or rectangle star pattern.
***** * * * * * * *****
- Assignment 20: Logic to print hollow rectangle star pattern.
*************** * * * * * * ***************
- Assignment 21: C program to print hollow square star pattern with diagonal
***** ** ** * * * ** ** *****
- Assignment 22: C program to print rhombus or parallelogram star pattern
***** ***** ***** ***** *****
- Assignment 23: C program to print hollow rhombus, parallelogram star pattern
***** * * * * * * *****
- Assignment 24: C program to print mirrored rhombus, parallelogram star pattern
***** ***** ***** ***** *****
- Assignment 25: Program to print mirrored parallelogram star pattern
******** ******** ******** ******** ******** ******** ******** ******** ********
- Assignment 26: C program to print hollow mirrored rhombus, parallelogram star pattern
***** * * * * * * *****
- Assignment 27: Program to print hollow mirrored parallelogram star pattern
********* * * * * * * * * *********
- Assignment 28: C program to print right triangle star pattern
* ** *** **** *****
- Assignment 29: C program to print hollow right triangle star pattern
* ** * * * * *****
- Assignment 30: C program to print mirrored right triangle star pattern
* ** *** **** *****
- Assignment 31: C program to print hollow mirrored right triangle star pattern
* ** * * * * *****
- Assignment 32: C program to print reverse right triangle star pattern
***** **** *** ** *
- Assignment 33: C program to print hollow inverted right triangle star pattern
***** * * * * ** *
- Assignment 34: C program to print reversed mirrored right triangle star pattern
***** **** *** ** *
- Assignment 35: C program to print hollow mirrored inverted right triangle star pattern
***** * * * * ** *
- Assignment 36: C program to print Equilateral triangle (Pyramid) star pattern
* *** ***** ******* *********
- Assignment 37: C program to print hollow pyramid (Equilateral triangle) star pattern
* * * * * * * *********
- Assignment 38: C program to print reverse Pyramid star pattern
********* ******* ***** *** *
- Assignment 39: C program to print hollow inverted pyramid star pattern
********* * * * * * * *
- Assignment 40: C program to print half diamond star pattern
* ** *** **** ***** **** *** ** *
- Assignment 41: C program to print hollow diamond star pattern
********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** **********
- Assignment 42: C program to print mirrored half diamond star pattern
* ** *** **** ***** **** *** ** *
- Assignment 43: C program to print right arrow star pattern
***** **** *** ** * ** *** **** *****
- Assignment 44: C program to print diamond star pattern
* *** ***** ******* ********* ******* ***** *** *
- Assignment 45: C program to print left arrow star pattern
***** **** *** ** * ** *** **** *****
- Assignment 46: C program to print plus star pattern
+ + + + +++++++++ + + + +
- Assignment 47: C program to print X star pattern
* * * * * * * * * * * * * * * * *
- Assignment 48: C program to print heart star pattern
*** *** ***** ***** ************** ************* *********** ********* ******* ***** *** *
- Assignment 49: C program to print 8 star pattern
**** * * * * * * * * **** * * * * * * * * ****
- Assignment 50: C program to print heart star pattern with name in center
***** ***** ******* ******* ********* ********* *******atnyla****** ***************** *************** ************* *********** ********* ******* ***** *** *
- Assignment 51: C program to print 0 or 1 square number pattern
11111 11111 11111 11111 11111
- Assignment 52: Program to print number pattern of 1's and 0's at alternate rows
11111 00000 11111 00000 11111
- Assignment 53: Program to print number pattern with 1, 0
11111 00000 11111 00000 11111
- Assignment 54: C program to print number pattern with 1, 0 at alternate columns
10101 10101 10101 10101 10101
- Assignment 55: Program to print number pattern at even/odd columns
10101 10101 10101 10101 10101
- Assignment 56: C program to print chessboard number pattern with 1 and 0
10101 01010 10101 01010 10101
- Assignment 57: C program to print box number pattern with 1 and 0
11111 10001 10001 10001 11111
- Assignment 58: C program to print box number pattern with plus in center
11011 11011 00000 11011 11011
- Assignment 59: C program to print box number pattern of 1 with 0 center
11111 11111 11011 11111 11111
- Assignment 60: C program to print box number pattern of 1 and 0 with cross center
10001 01010 00100 01010 10001
- Assignment 61: C program to print circle box number pattern with 1 and 0
01110 10001 10001 10001 01110
- Assignment 62: Program to print the given pattern
11111 22222 33333 44444 55555
- Assignment 63: Program to print the given pattern
12345 12345 12345 12345 12345
- Assignment 64: Program to print the given pattern
12345 23456 34567 45678 56789
- Assignment 65: Program to print the given number pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
- Assignment 66: Program to print number pattern
55555 54444 54333 54322 54321
- Assignment 67: Program to print the given number pattern
12345 23455 34555 45555 55555
- Assignment 68: Program to print the given number pattern reverse
6777777 5677777 4567777 3456777 2345677 1234567
- Assignment 69: Program to print the given pattern
12345 23451 34521 45321 54321
- Assignment 70: Program to print the given number pattern
12345 21234 32123 43212 54321
- Assignment 71: Print perfect square spiral number pattern
1 2 3 4 5 620 21 22 23 24 719 32 33 34 25 818 31 36 35 26 917 30 29 28 27 1016 15 14 13 12 11
- Assignment 72: Program to print the given number pattern
555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555
- Assignment 73: Program to print the given number pattern
1 22 333 4444 55555
- Assignment 74: Program to print the given number pattern
55555 4444 333 22 1
- Assignment 75: Logic to print the given number pattern
55555 4444 333 22 1
- Assignment 76: Program to print the given number pattern
11111 2222 333 44 5
- Assignment 77: Logic to print the given number pattern
11111 2222 333 44 5
- Assignment 78: Program to print the given number pattern
5 44 333 2222 11111
- Assignment 79: Print the given number pattern
5 44 333 2222 11111
- Assignment 80: Program to print the given number pattern
1 12 123 1234 12345
- Assignment 81: Print the given number pattern
1 12 123 1234 12345
- Assignment 82: Logic to print the given number pattern
- Assignment 83: Logic to print the given number pattern
12345 1234 123 12 1
- Assignment 84: Logic to print the given number pattern
1 21 321 4321 54321
- Assignment 85: Logic to print the given number pattern
1 21 321 4321 54321
- Assignment 86: Program to print the given number pattern
54321 4321 321 21 1
- Assignment 87: Logic to print the given number pattern
54321 4321 321 21 1
- Assignment 88: Program to print the given number pattern
5 54 543 5432 54321
- Assignment 89: Program to print the given number pattern
5 54 543 5432 54321
- Assignment 90: Program to print the given number pattern
54321 5432 543 54 5
- Assignment 91: Program to print the given number pattern
54321 5432 543 54 5
- Assignment 92: Program to print the given number pattern
5 45 345 2345 12345
- Assignment 93: Program to print the given number pattern
5 45 345 2345 12345
- Assignment 94: Program to print the given number pattern
12345 2345 345 45 5
- Assignment 95: Program to print the given number pattern
12345 2345 345 45 5
- Assignment 96: Program to print the given number pattern
1 23 345 4567 56789
- Assignment 97: Program to print the given number pattern
1 23 345 4567 56789
- Assignment 98: Program to print the given number pattern
56789 4567 345 23 1
- Assignment 99: Program to print the given number pattern
56789 4567 345 23 1
- Assignment 100: Program to print the odd number pattern
13579 3579 579 79 9
- Assignment 101: Program to print the odd number pattern with spaces
13579 3579 579 79 9
- Assignment 102: Check whether a given number is prime or not (using for loop)
- Assignment 103: C Program to Check Armstrong Number
- Assignment 104: C program to find prime factors of a number using for loop