EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

For Loop in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » For Loop in C

For Loop in C

Introduction to for Loop in C Programming

Although writing C programs we may experience a purpose to perform a comparable or exact group of instructions many times, for example, Printing numbers via 1 to 100 around the display screen, This with no usage of looping can be extremely tedious as well as, produce will make the program definitely not redistributable and never understandable. This problem came to be fixed by using looping.

Looping is known as a series of statements that are specific when as well as, that can be performed several times. Within the looping, a collection of instructions will be carried out till some conditions to get termination with the loop has been reached.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Infinite Loops

Infinite loops can be a series of instructions which can be carried out forever. These types of loop happen whenever there 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 irresponsive however in modern Operating Systems these types of loops usually can be ended through the end-user.

A loop essentially includes 2 parts:

  • The Control Declaration
  • The loop Body

1. The Control Declaration

The control declaration checks the particular condition and after that, it directs regular statements included in the body with the loop.

2. The Loop Body

The loop body features a group of instruction which will be carried out until some condition to get the termination with the loop has been reached. Loops being used through programming to repeat a particular block of code. When looking over this guide, you will understand to produce for loop in C programming. The for statement has three expressions within parentheses.

Syntax:

The syntax in for loop is –

Syntax

These works together to determine whether to execute the statement.

The first thing that happens is that the first expression is evaluated. Regardless of its outcome, this conditional expression is then evaluated. This expression defines some truth. If it evaluates to true or non-zero, then the statement is executed.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

After the statement has executed, the loop expression is evaluated but again, only if the statement was executed. After the loop expression, the conditional expression is always executed to determine whether to execute the statement again.

Flow Diagram

Flow Diagram                                                    

How for Loop Works in C?

  • The initialization declaration is executed just once.
  • 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 is performed as well as the update expression is updated.
  • This technique repeats before the test expression can be false.
  • The for loop is usually applied if the quantity of iterations is well known.
  • 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.

Example - 1

The loop condition is evaluated. If its outcome is true.

Example- 2

The body of the loop is executed.

loop body executed

If not, execution continues following the for 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 count starts at 10 and this does not satisfy the condition.

body evalute 1

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.

body evalute 2

Output:

for loop c - 4

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.

for loop c - 5

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.

for loop c - 6

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

for loop c - 7

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.

for loop c - 8

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

switch statement

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, as well as 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.

loop variable

Conclusion – for Loop in C

  • The primary statements 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.
  • 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.

Recommended Articles

This is a guide to for Loop in C. Here we discuss the Introduction and how for loop works in C language with sample codes and output. You can also go through our other suggested articles –

  1. For Loop in Python
  2. While Loop in C Programming
  3. PHP Do While Loop
  4. C# While Loop

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More