What Is For Loop In C?

What Is For Loop In C?
Techiio-author
Written by Shuvhojit DebFebruary 16, 2022
8 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, we'll get to know about the For Loop in C.

For loop in C

The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.

Syntax

The syntax of for loop in c language is given below:

for(Expression 1; Expression 2; Expression 3){  
//code to be executed  
}

Infinite Loops

Infinite loops can be a series of instructions that can be carried out forever. These types of loop happen whenever there is simply no terminating condition offered or possibly a terminating condition that could never be fulfilled (just like 1==2 and so on.) or maybe occasionally due to a run time error. In the old system, infinite loops triggered the whole system to become irresponsible; however, in modern Operating Systems, these types of loops usually can be ended through the end-user.

Flowchart

blogpost

Working of For loop in C

  1. The initialization declaration is executed just once.
  2. After that, the conditional expression can be examined. If the test expression is false (0), for loop is ended. However, if the conditional expression is true (nonzero), codes within the body of for loop are performed, and the update expression is updated.
  3. This technique repeats before the test expression can be false.
  4. The for loop is usually applied if the quantity of iterations is well known.
  5. To find out more on conditional expression (once test expression is examined to nonzero (true) and 0 (false))

Examples

The most powerful iteration statement, but potentially also a source of bugs. Let’s get the loop variable initialized first.

blogpost

The loop condition is evaluated if its outcome is true.

blogpost

The body of the loop is executed.

blogpost

If not, execution continues following the for a statement after the body’s executed.

The expression updating the loop variable is executed, and the loop condition is again evaluated and so on, and this continues until the loop terminates. Of course, this loop will not execute its body since the count starts at 10, and this does not satisfy the condition.

blogpost

It’s easier to see such things at a glance with a for statement. So let’s change the initializer to 0 and take it for a spin, and there’s our count from 0 through 9 as expected.

blogpost

Output:

blogpost

An interesting thing about for statement is that any one of these may be omitted. We can, for example, use a loop variable declared elsewhere. This is fine and has the same effect.

blogpost

But now the count variable is visible beyond the for statement, again potentially a source of bugs. You should always try to keep a variable as limited and local as possible. Still, this is legal if you need it. You can also omit the expression by updating the loop variable.

blogpost

Again, this is fine, but what might be somewhat surprising is that you can even omit the loops condition expression itself.

blogpost

In that case, the condition is assumed to be true, and the loop will remain the same, so loop indefinitely or until you terminate it in some other way.

blogpost

Here again, we are using the break statement. We first introduced with a switch statement.

blogpost

It can also be used to break out of the loop statement and causes execution to commence following the loop. This works just as well with a while statement, by the way. This now is again equivalent to the original while statement and the original for statement with three parts of the for statement neatly in line.

The main difference is that the loop condition is not actually checked upfront, although we know visually that the condition will hold at least once. The body is then executed, which includes the statement updating the loop variable and the if statement evaluating the loop condition manually.

Let’s give it a try. And sure enough, 0 through 9 again.

blogpost

Conclusion

  • The primary statements are provided by the C programming language for selection and iteration.
  • We considered the if statement the most widely used statement for selection or control flow.
  • If some condition expression is true, then the associated statement or compound statement is executed. If not, execution continues at the next statement, if any.
  • For statement gives you a lot of control over iteration in a more condensed syntax. There is nothing you cannot write with a while loop, but it is more convenient and safe in many cases since you can include a declaration which the other statements cannot, at least in C.
  • The significance of loops in the different programming languages is enormous; they will enable us to minimize the number of lines within a program, producing our program more understandable and as well, effective.
C Language
Programming
Loops
For loop
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance