can anyone tell me about for loop with detail and example.
Answers
Answer:
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
Quiz Course 169K views
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 n 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
Alida D.
Student
Dumont, New Jersey
I liked that Study.com broke things down and explained each topic clearly and in an easily accessible way. It saved time when preparing for exams.
Catherine S.
Student
Jefferson, Missouri
There are so many options on Study.com! I can research almost any subject, delve into it more deeply if I wish, and begin studying at a deeper level right away.
Computer Science 111: Programming in C
10 chapters | 81 lessons
Ch 1. Introduction to Computer Programming...
Ch 2. C Programming Basics
Ch 3. Programming Using Selection in C
Ch 4. Programming Using Repetition in C
Loops in C Programming: Structure & Examples4:29
While Loop: Definition, Example & Results4:08
For Loop: Definition, Example & Results4:44
4:08
Next Lesson
Do While Loop: Definition, Example & ResultsDo While Loop: Definition, Example & Results
Loop Control Statements in C: Definition & Examples
Nesting Loops & Statements in C Programming3:25
Risks & Errors in While, For & Do While Loops in C5:35
Practical Application for C Programming: While & Do While Loops
Go toProgramming Using Repetition in C
Ch 5. Programming Functions in C
Ch 6. Arrays, Characters & Strings in...
Ch 7. Arrays, Addresses & Pointers in...
Ch 8. Data Files & Streams in C
Ch 9. Data Structures in C Programming
Ch 10. Required Assignments for Computer...
For Loop: Definition, Example & Results
Related Study Materials
Related Lessons
What is While Loop in C++? | While Loop Statement, Syntax & Example
What is While Loop in C++? | While Loop Statement, Syntax & Example
Do While Loop: Definition, Example & Results
Do While Loop: Definition, Example & Results
Loops in C Programming: Structure & Examples
Loops in C Programming: Structure & Examples
Array Initialization in C Programming
Array Initialization