--- title: For Loop --- # For Loop The `for` loop executes a block of code until a specified condition is false. Use `while` loops when the number of iterations are variable, otherwise use `for` loops. A common use of `for` loops is array iterations. It is also known as an 'entry-controlled loop' since the condition is checked before the next iteration. Another example of an 'entry-controlled loop' is a while loop. The block of code around which the for loop iterates is packed inside the curly braces. A for loop is also acceptable without curly braces. The compiler assumes only the 1st statement to be under the imaginary curly braces. A variable declared inside the curly braces of a for loop is only valid inside that particular for loop. We cannot use a variable declared inside a for loop outside it. ## Syntax of For Loop ```c for ( init; condition; increment ) { statement(s); } ``` The `for` loop consists of 3 sections: 1. The initialization section (start value) 2. Condition (stop value) 3. Test Expression (step value) The initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated. This process repeats until the test expression is false. The for loop is commonly used when the number of iterations is known. #### Example ```c #include int main () { int array[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { printf("Item on index %d is %d\n", i, array[i]); } return 0; } ``` - It should be noted that the value of `i` is set to `0` because arrays are indexed from `0` to `size-1`, and we want to print the first value as well #### Output: ```shell > Item on index 0 is 1 > Item on index 1 is 2 > Item on index 2 is 3 > Item on index 3 is 4 > Item on index 4 is 5 ``` #### Explanation: The `for` loop checks the value of `i` based on the conditions. If `i` is smaller than `5`, the text will be printed. After printing, the value of `i` is increased by 1. This function will continue until `i` is greater than 4, at which point the loop will stop and exit. ### Example for printing star pattern for pyramid ```c #include int main () { int i, j; for (i = 1; i <= 5; i++) { for (j = i; j < 5; j++) { printf (" "); } for (j = 1; j <= (2 * i - 1); j++) { printf ("*"); } printf ("\n"); } return 0; } ``` Output: ```shell * *** ***** ******* ********* ``` ## Syntax of For Infinite loop An infinite loop occurs when the condition will never be met, due to some inherent characteristic of the loop. An infinite loop also called an endless loop, and it is a piece of coding that lacks a functional exit so that it repeats indefinitely. ### Examples: ```C for ( ; ; ) { statement(s); } ``` ```C #include int main () { for (int i = 0; i < 5; i--) { printf("%d \n", i); } } ``` ### Warning! Some older versions of compilers don't support declaration inside the for loop: ```C #include int main () { int array[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { // The int i = 0 will show you an error on older compiler versions printf("Item on index %d is %d\n", i, array[i]); } } ``` You can solve this problem if you declare the variable before: ```C #include int main () { int array[] = {1, 2, 3, 4, 5}; int i; // You declare the variable before the for loop for (i = 0; i < 5; i++) { // Now you won't have a problem printf("Item on index %d is %d\n", i, array[i]); } } ``` ## Break and Continue statements When "break" statement is executed, the loop stops executing all other statements in it and immediately comes out of the loop. ```C #include int main(void) { int n = 5,i; for(i=0;i int main(void) { int n = 5,i; for(i=0;i