Declaring Lop Control Variables Inside the for Loop

Offend tile variable that controls for loop Is only needed for the purposes of the loll and is not used elsewhere. When this is the case, it is possible to declare the capable  inside the initialization portion of the for. For example, here is the preceding program receded so that the loop control variable n is declared as an int inside the for.

When you declare it variable inside a fen: loop, there is one important point to  remember:. the  of  variable ends when the for statement does. (That is, the scope of the variable is to the for loop.) Outside the for loop, the variable will cease to exist. If you need to use the loop control variable elsewhere in your program, you will not be able to declare it inside the for loop.When the loop ,control variable will not be needed elsewhere, most  s declare it inside the for. For example, here is a simple program that tests for prime ‘numbers. Notice that the loop control variable, if is declared inside the for since it is not needed elsewhere

[vfb id=1]

Share This