for loop in C programming language with example
- Home
- nested for
- for loop in C programming language with example
- On
- By
- 0 Comment
- Categories: nested for, pyramid triangle
for loop in C programming language with example
for loop in C programming language with example
In this article, we will discuss for loop in C programming language with example
Looping structure is a control structure used in many programming languages for repetition tasks.
As we known, there is three looping structure in C, including for loop.
For loop – for loop in C programming language
While loop – while loop in C Programming language
Do-while loop – do -while loop in C Programming language
for loop in C programming language with example
For loop in C Programming language performs the specified number of times.
For-Loop allows repetition of flow of control until the conditional statement becomes false.
Declaration
The syntax of a for loop in C Programming language
for(initialization; test_Expression; updating_statement){
//code here
}
Ex:
for(i=1; i<=10; i++){ System.out.print(i) }
Explanation of for loop in C
For loop always has three parts of control structure to perform the significant task
Initiation –
First, it always starts and executed from the variable initialization statement. The initialization statement is executed only once. Initialization is an entry point for the loop.
condition –
Next, the for loop moves to the conditional statement to test condition. The condition is evaluated and if it is true, the statements in body part are executed. But, if the condition is false, the body of the loop does not execute.
updating statement
Finally, loop goes updating statement part and it allows you to update any loop control variables until the condition becomes false. This statement can be updated until a semicolon follows the condition.
For loop flow diagram
How for loop works
for loop in C programming language is used to execute a block of statements repeatedly until a given condition returns false.
Initially, initialization statements are executed only once then text expression is evaluated by for loop. If the test expression is true(Boolean expression makes only two output true or false) flow of control enters inside the body to execute the code. Continuously, test expression is evaluated and executed until the test expression is false.
This process repeats until the test expression is false.
When the test expression is false. for loop stops, the execution and flow of control immediately exit from the loop.
Various form of for loop in C language
form 1
int i; for(i=0; i<=10; i++){ //statement(s); }
form 2
int i=0; for(; i<=10; i++){ //statement(s); }
form 3
int i; for(i=0; i<=10;){ //statement(s); i++ }
Example of for loop
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int a; for(a=1; a<=10; a++){ printf("Value is %d\n",a); } getch(); return 0; }
When the above code compiled and executed, it produced the following result
value is 1 value is 2 value is 3 value is 4 value is 5 value is 6 value is 7 value is 8 value is 9 value is 10
Program 2
print sum number using for loop
#include <stdio.h> #include <stdlib.h> int main() { int num, count,sum=0; printf("Enter a positive integer\n"); scanf("%d",&num); // input value for num for(count=1; count<=num; count++) { printf("count of numbers:%d\n",count); } for(count=1; count<=num; count++) { sum+=count; } printf("Sum of number:%d\n",sum); getch(); return 0; }
When the above code compiled and executed, it produced the following result
Enter a positive integer 10 count of number:1 count of number:2 count of number:3 count of number:4 count of number:5 count of number:6 count of number:7 count of number:8 count of number:9 count of number:10 sum of numbers:55
At the above program, the value entered by the user is stored in variable num as an int. Suppose, the value is 5
First for loop executes 5 times and displays stored value on the count.
Finally, second for loop calculates total value.
Suggested for you