Dev C++ For Loop Counter

Let's say that you want to make a for loop in C, but in addition to the determined condition you also know when, or if the loop should stop. Note that such a condition does not have to be necessarily hard-coded, nor should the loop have such a determining value all the time.

-->

Executes a statement repeatedly until the condition becomes false. For information on the range-based for statement, see Range-based for Statement (C++).

Syntax

for (init-expression;cond-expression;loop-expression)
statement;

Remarks

Use the for statement to construct loops that must execute a specified number of times.

Dev C++ For Loop Countertops

The for statement consists of three optional parts, as shown in the following table.

for Loop Elements

Syntax NameWhen ExecutedDescription
init-expressionBefore any other element of the for statement, init-expression is executed only once. Control then passes to cond-expression.Often used to initialize loop indices. It can contain expressions or declarations.
cond-expressionBefore execution of each iteration of statement, including the first iteration. statement is executed only if cond-expression evaluates to true (nonzero).An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type. Normally used to test for loop-termination criteria.
loop-expressionAt the end of each iteration of statement. After loop-expression is executed, cond-expression is evaluated.Normally used to increment loop indices.

The following examples show different ways to use the for statement.

init-expression and loop-expression can contain multiple statements separated by commas. For example:

loop-expression can be incremented or decremented, or modified in other ways.

A for loop terminates when a break, return, or goto (to a labeled statement outside the for loop) within statement is executed. A continue statement in a for loop terminates only the current iteration.

If cond-expression is omitted, it's considered true, and the for loop won't terminate without a break, return, or goto within statement.

Although the three fields of the for statement are normally used for initialization, testing for termination, and incrementing, they're not restricted to these uses. For example, the following code prints the numbers 0 through 4. In this case, statement is the null statement:

for Loops and the C++ Standard

The C++ standard says that a variable declared in a for loop shall go out of scope after the for loop ends. For example:

C# For Loop Example

By default, under /Ze, a variable declared in a for loop remains in scope until the for loop's enclosing scope ends.

/Zc:forScope enables standard behavior of variables declared in for loops without needing to specify /Za.

It's also possible to use the scoping differences of the for loop to redeclare variables under /Ze as follows:

Dev C++ For Loop Counter

This behavior more closely mimics the standard behavior of a variable declared in a for loop, which requires variables declared in a for loop to go out of scope after the loop is done. When a variable is declared in a for loop, the compiler internally promotes it to a local variable in the for loop's enclosing scope. It's promoted even if there's already a local variable with the same name.

Dev C++ For Loop Counter Free

See also

Dev Loop Control

Iteration Statements
Keywords
while Statement (C++)
do-while Statement (C++)
Range-based for Statement (C++)