You are here:

C++/about increment decrement operators in c

Advertisement


Question
can we use for loop without using increment/decrement operators
if can,
then please tell me how we can do this?
its

Answer
Yes.

Firstly, revising what a for loop is.

A for-loop has the form:

  for ( initial-expression ; expression-is-true ; end-expression )
  {
  // repeat the code
  }

It is equivalent to the following:

  initial-expression;

  while ( expression-is-true )
  {
  // repeat the code

      end-expression;
  }

Note that the three parts in () separated by ; of a for loop, as implied by the names used above, are expressions, so expressions such as ++i and --i are just examples of possible valid expressions that can be used in for loops.

Oh, and I assume when you say "using increment/decrement operators" you mean for the part that is the end-expression.

Secondly, any or all of the three expressions forming the for-statement may be omitted but the semicolons must remain. If the initial-expression or end-expression is omitted it is as if it were not in the logical while loop equivalent. If the expression-is-true part is omitted then it is taken to be true, so:

  for (;;;)
  {
  // repeat code forever
  }

Will repeat the code in the braces forever, equivalent to:

 while (true)
 {
 // repeat code forever
 }

Hence omitting the end-expression (or other for loop expressions) entirely would be one example of not using increment/decrement operators.

For some other examples:

   - we could write a for loop such as:
       for ( i=0; i < MAX; ++i )
       {
         // repeated statements...
       }

     like this:
       for ( i=0; i < MAX; )
       {
         // repeated statements...

         ++i;
       }

     or this:
       for ( i=0; i < MAX; )
       {
         // repeated statements...

         i += 1;
       }

     or this:
       for ( i=0; i < MAX; )
       {
         // repeated statements...

         i = i + 1;
       }

     or this:
       for ( i=0; i < MAX; i += 1 )
       {
         // repeated statements...
       }

     or this:
       for ( i=0; i < MAX; i = i+1 )
       {
         // repeated statements...
       }


   - we can therefore use step changes other than 1:
       for ( i=0; i < MAX; i += 3 )
       {
         // repeated statements...
       }


   - we can also use operators other than + and - and their variants:
       for ( i=1; i <= 65536; i *= 2 )
       {
         // repeated statements...
       }

       Generates values of i that are powers of 2 up to and including 2 to the 16:
         1, 2, 4, 8, 16, ..., 32768, 65536


   - we could call a function:
       for ( pData = get_first(pCollection); pData!=NULL; pData = get_next(pCollection) )
       {
         // repeated statements...
       }

     or maybe even:
       for ( get_first(pCollection, pData); pData!=NULL; get_next(pCollection, pData) )
       {
         // repeated statements...
       }


   - we can use the comma ( , ) operator to update more than one thing within a for loop:
       for ( pData = get_first(pCollection), itemCount=0
         ; pData!=NULL && i < 10
         ; pData = get_next(pCollection), ++itemCount
         )
       {
         // repeated statements...
       }

     The intention above is to only work on the first 10 items in a collection, or less if the
     collection contains less than 10 items. I have wrapped the for statement across several lines
     as it was getting rather long to be all on one line.

Please note that the example code in this answer are fragments only and not whole programs, have not been compiled so may contain typos or other errors - if so then I apologise, and are for example purposes only and are not of production quality.

Hope this has given you some ideas.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.