In this article for loop in c we give the information about the for loop is another entry-controlled loop that provides a more concise loop control structure .
for loop in c:
The for loop is another entry-controlled loop that provides a more concise loop control structure .
For loop Syntax:
for(initialization; test-condition; increment/decrement)
Body of the loop.
The implementation of the statement is as follows:
Using assignment statements like i = 0 and count = 0, control variables are initialized first. The i and count variables are known as loop-control variables.
The value of the control variable is checked using test-condition. Test-mode is a relational expression, such as i <10, which determines when the loop exits. If the condition is true, the main part of the loop will be executed; Otherwise the loop is terminated and the execution continues with a statement following the loop immediately.
When the main part of the loop is executed, the last statement in the loop is evaluated and transferred back to the control statement. Now the control variable is assigned an assignment statement like i = i + 1.
And the new value of the control variable is re-checked to see if it meets the loop condition. When the condition is met, the main part of the loop is reactivated. This process continues until the value of the control variable fails to complete the test-condition.
For Loop Flowchart:
For loop example in c:
// Program to calculate the sum of first n natural numbers
int num, count, sum = 0;
printf(“Enter a positive integer: “);
for(count = 1; count <= num; ++count)
sum += count;
printf(“Sum = %d”, sum); getch();
Enter a positive integer: 10
Sum = 55