devxlogo

The Scope of A Local Loop Counter

The Scope of A Local Loop Counter

C++ allows you to declare variables wherever you need them, thus enabling immediate initializations. A good example is a loop counter. It can (and should) be declared inside a for-statement:

 void f() {for (int i = 0; i < 10; i++) // i declared and initialized inside a for-statement{		cout << i <

In earlier stages of C++, a local variable declared this way remained accessible in its enclosing block. This was a source for bugs and name hiding. Consequently, the standard has been revised to fix this loophole, so to speak, and local variables created this way are inaccessible outside the for-statement in which they were declared. In the example above, i is inaccessible outside the loop.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist