For Loop: Definition, Example & Results

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Do While Loop: Definition, Example & Results

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 What Is a For Loop?
  • 0:38 Syntax of a For Loop
  • 1:14 Example of a For Loop
  • 3:29 Decrementing
  • 3:46 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami
This lesson demonstrates the definition and use of the for loop in C programming language. It also walks through various practical applications of the same.

What Is a For Loop?

A for loop enables a particular set of conditions to be executed repeatedly until a condition is satisfied. Imagine a situation where you would have to print numbers from 1 to 100. What would you do? Will you type in the printf command a hundred times or try to copy/paste it? This simple task would take an eternity. Using a for loop you can perform this action in three statements. This is the most basic example of the for loop. It can also be used in many advanced scenarios depending on the problem statement.

Check out the flowchart of a for loop to get a better idea of how it looks:


Flow Chart of a For Loop


Syntax of a For Loop

for (initialization statement; test expression; update statement) {
 // statements
}

The for loop starts with a for statement followed by a set of parameters inside the parenthesis. The for statement is in lower case. Please note that this is case sensitive, which means the for command always has to be in lower case in C programming language. The initialization statement describes the starting point of the loop, where the loop variable is initialized with a starting value. A loop variable or counter is simply a variable that controls the flow of the loop. The test expression is the condition until when the loop is repeated. Update statement is usually the number by which the loop variable is incremented.

Example of a For Loop

The following piece of code is an example to calculate the sum of five natural numbers:

#include <stdio.h>
int main() {
 int num, count, sum = 0;
 printf("Enter a positive integer: ");
 scanf("%d", &num);
 //for loop terminates when n is less than count
 for(count = 1; count <= num; ++count) {
  sum += count;
 }
 printf("Sum = %d", sum);
return 0;}

Explanation and Output

In this code we're looking at, we're trying to find out the sum of n natural numbers. The code asks us to input a positive integer. To explain with a little more detail, let's consider the positive integer entered to be 4. The variable count in this case is the loop variable or counter.

1st iteration: count is 1. The test condition count<=num is satisfied as (1<=4). Since this condition is satisfied, the control enters the loop and executes the statement sum+ = count which means (sum = 0 + 1). After the iteration is fully completed, the value of count is incremented by 1.

2nd iteration: count is 2. The test condition count<=num is satisfied as (2<=4). Since this condition is satisfied, the control enters the loop and executes the statement sum+ = count again, which means (sum = 1 + 2). After the iteration is entirely completed, the value of count is incremented by 1.

3rd iteration: count is 3. The test condition count<=num is satisfied as (3<=4). Since this condition is satisfied, the control enters the loop and executes the statement sum+ = count again, which means sum = (1 + 2) + 3. After the iteration is entirely completed, the value of count is incremented by 1 .

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support