Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

For Loop In C

 

[adinserter name="Block 7"]

The most widely used loop by the programmer is For Loop In C language.

The For Loop In C is widely used by programmers because it contains initialization, termination and control flow in one place. The for loop allows a programmer to specify three things about a loop in a single line.

  • Setting a loop counter to an initial value (initialization).
  • Testing the loop counter to determine whether its value has reached the number of repetitions desired. (condition or termination)
  • Increasing or decreasing the value of the loop counter each time the body of the loop has been executed (flow control).
[et_pb_dmb_code_snippet title=”SYNTAX Of For Loop In C” code=”Zm9yIChpbml0aWFsaXphdGlvbiA7IGNvbmRpdGlvbiA7IGZsb3cpCnsKc29tZSBzdGF0ZW1lbnRzOwouLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4KfQ==” _builder_version=”4.0.9″]Zm9yIChpbml0aWFsaXphdGlvbiA7IGNvbmRpdGlvbiA7IGZsb3cpCnsKc29tZSBzdGF0ZW1lbnRzOwouLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4KfQ==[/et_pb_dmb_code_snippet]

Notice the two semicolons inside the bracket of for loop. Since these are part of the syntax of for loop so it should always be mentioned. Two semicolons create three sections.

The first section is used for initialization, second is used for condition check or termination and the third section is used to mention the control flow of a program.

[et_pb_dmb_code_snippet title=”EXAMPLE: For Loop In C” code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjb25pby5oPgppbnQgbWFpbiAoKQp7CiBpbnQgaTsgICAvL3ZhcmlhYmxlIGRlY2FsYXJhdGlvbi8vCiBmb3IgKCBpPTE7IGk8PTIgOyBpKyspCiAgewogICBwcmludGYoIk51bWJlciA9ICVkIFxuIiwgaSk7CiAgIH0KZ2V0Y2goKTsgLy8gdXNlIGluICh0dXJibyBjIG9yIG9sZGVyIElERSksIGRvbid0IHVzZSBpbiBjb2RlOjpibG9ja3MvLwpyZXR1cm4gMDsgIC8vIHJldHVybiB0eXBlIG9mIG1haW4oKSBmdW5jdGlvbiBpcyBpbnQvLwp9″ _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjb25pby5oPgppbnQgbWFpbiAoKQp7CiBpbnQgaTsgICAvL3ZhcmlhYmxlIGRlY2FsYXJhdGlvbi8vCiBmb3IgKCBpPTE7IGk8PTIgOyBpKyspCiAgewogICBwcmludGYoIk51bWJlciA9ICVkIFxuIiwgaSk7CiAgIH0KZ2V0Y2goKTsgLy8gdXNlIGluICh0dXJibyBjIG9yIG9sZGVyIElERSksIGRvbid0IHVzZSBpbiBjb2RlOjpibG9ja3MvLwpyZXR1cm4gMDsgIC8vIHJldHVybiB0eXBlIG9mIG1haW4oKSBmdW5jdGlvbiBpcyBpbnQvLwp9[/et_pb_dmb_code_snippet]

OUTPUT:

Number = 1
Number =2

Understand Program Execution

  • Inside the main() function, a variable i of integer data type is declared.
  • Inside the bracket of for loop the variable, i is initialized with 1. Now, the condition (i<=2) will be checked. Since 1<2 so condition becomes true and control will move inside the for loop and execute the statement inside for loop and in this case it will print the value of i on the output screen.
  • After the execution all the statements inside for loop, the value of the variable i increases by 1 (also called as a counter variable).
  • Now the value of the variable i will be 2. Again condition will be checked and the condition becomes true again. Therefore, control will enter for loop and execute the statements inside the loop. Now 2 will be printed on screen and value of the variable i will be 3.
  • Again, the condition will be checked and this time the condition (3<=2) will be false and hence control will exit from for loop.

Special Attention

  • You can initialize the variable either inside the main function (as a local variable) or outside (as a global variable) during the declaration of a variable. In this case syntax of for loop will be for ( blank (don’t write anything); condition; flow).
  • Don’t use looping statements when you have small work like the above-mentioned program. It is always a good practice to use looping statements only when you have at least more than 10 statements to execute because looping statements are complex and it takes time in execution. So if you will use this for the smaller program it will unnecessarily increase execution time and hence lowers the execution speed. However, using looping statements in the large program saves programmers time taken to write the same code multiple times, reduces space and execution time.
  • Please note that \n is an escape sequence that is just used to write one output on one line. You may or may not use this, it’s up to you. However, if you will not use this your output in the above program will look something like this 12 (in one line). It becomes really hard to read output when there are several numbers of outputs to print. In order to make your output more readable, you must \n.

Related Contents

while loop   do-while loop  nested loop    infinite loop

[adinserter name=”Block 7″]

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

[adinserter block=”6″]

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This