0% found this document useful (0 votes)
462 views3 pages

C Programming For Loop

Loops allow code to be repeatedly executed in programming. There are three types of loops in C: for, while, and do-while. A for loop initializes a variable, tests a condition, and updates the variable each repetition. It executes the code block if the test is true and terminates when the test becomes false. For example, a for loop could sum the first n natural numbers entered by the user by initializing a counter variable, testing if it is less than or equal to n each repetition, and incrementing it after each iteration. This allows the code to repeatedly add the counter to the running sum until the counter exceeds n.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
462 views3 pages

C Programming For Loop

Loops allow code to be repeatedly executed in programming. There are three types of loops in C: for, while, and do-while. A for loop initializes a variable, tests a condition, and updates the variable each repetition. It executes the code block if the test is true and terminates when the test becomes false. For example, a for loop could sum the first n natural numbers entered by the user by initializing a counter variable, testing if it is less than or equal to n each repetition, and incrementing it after each iteration. This allows the code to repeatedly add the counter to the running sum until the counter exceeds n.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

C Programming for Loop

C Programming Loops
Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used
in performing repetitive task in programming. Consider these scenarios:

You want to execute some code/s 100 times.

You want to execute some code/s certain number of times depending upon input from user.
These types of task can be solved in programming using loops.
There are 3 types of loops in C programming:

1. for loop
2. while loop
3. do...while loop

for Loop Syntax


for(initialization statement; test expression; update statement) {
code/s to be executed;
}

How for loop works in C programming?


The initialization statement is executed only once at the beginning of the for loop. Then the test expression is
checked by the program. If the test expression is false, for loop is terminated. But if test expression is true then
the code/s inside body of for loop is executed and then update expression is updated. This process repeats until
test expression is false.
This flowchart describes the working of for loop in C programming.

for loop example


Write a program to find the sum of first n natural numbers where n is entered by user. Note: 1,2,3... are
called natural numbers.
#include <stdio.h>
int main(){
int n, count, sum=0;
printf("Enter the value of n.\n");
scanf("%d",&n);
for(count=1;count<=n;++count) //for loop terminates if count>n
{
sum+=count; /* this statement is equivalent to sum=sum+count */
}
printf("Sum=%d",sum);
return 0;
}

Output
Enter the value of n.
19
Sum=190

In this program, the user is asked to enter the value of n. Suppose you entered 19 then, count is initialized to 1
at first. Then, the test expression in the for loop,i.e., (count<= n) becomes true. So, the code in the body of
for loop is executed which makes sum to 1. Then, the expression ++count is executed and again the test
expression is checked, which becomes true. Again, the body of for loop is executed which makes sum to 3 and
this process continues. When count is 20, the test condition becomes false and the for loop is terminated .
Note: Initial, test and update expressions are separated by semicolon(;).

You might also like