for loop in CThe 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 of for loop in CThe syntax of for loop in c language is given below: Flowchart of for loop in CC for loop ExamplesLet's see the simple program of for loop that prints table of 1. Output 1 2 3 4 5 6 7 8 9 10 C Program: Print table for the given number using C for loopOutput Enter a number: 2 2 4 6 8 10 12 14 16 18 20 Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Properties of Expression 1
Example 1 Output 35 36 Example 2 Output 1 2 3 4 Properties of Expression 2
Example 1 output 0 1 2 3 4 Example 2 Output 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12 Example 3 Output infinite loop Properties of Expression 3
Example 1 Output 0 2 1 4 2 6 3 8 4 10 Loop bodyThe braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don't need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example. Output 20 20 20 20 20 20 20 20 20 20 Infinitive for loop in CTo make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop. If you run this program, you will see above statement infinite times. Next TopicNested Loops in C |