Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multiple indices in for loop

Reply
Thread Tools

Multiple indices in for loop

 
 
mt
Guest
Posts: n/a
 
      03-29-2011
Why is it not possible to use multiple indices in a for loop in C like
in java,

Example:
for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}

Thanks,
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2011
mt <(E-Mail Removed)> writes:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}


It is, at least in C99. (I pasted your line of code into a C source
file, and gcc compiled it without complaint.)

If you're compiling in C90 mode, or using a compiler that doesn't
support C99, declarations in for loops aren't permitted at all.

But you might want to consider whether this is a good idea. You have a
loop invariant that j == 2 * i, but it's enforced in two different
places: the initial value of j (0), and the third clause of the for loop
(j = 2 * i). If those get out of sync, you've got problems.

For this particular case, you might try:

for (int i = 0; i <= 10; i ++) {
int j = 2 * i;
/* ... */
}

If you're declaring more than one variable in a for loop header,
you're probably trying to be too clever. (Note that you can't
declare, for example, an int and a char* together, just because of
the limitations of the syntax of a declaration).

FYI, the syntax for a for loop (C99 6.8.5p1) is:

for ( expression(opt) ; expression(opt) ; expression(opt) ) statement

for ( declaration expression(opt) ; expression(opt) ) statement

The second form is new in C99; the "declaration" provides the first
semicolon.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-29-2011
On 3/29/2011 2:01 PM, mt wrote:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i<= 10; i++,j = 2 * i) {}


What makes you think you cannot do this? In pre-C99 compilers you need
to declare i and j outside the loop of course, but any C99 compiler
should like it as it stands.

 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      03-30-2011
mt wrote:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}
>
> Thanks,


Impossible to answer (unless it does something different in Java than in
C, in which case I don't know).

Is it coincidence that "mt" is next door to "mu"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
loop over array with indices Johannes Ahlmann Ruby 0 05-23-2005 09:12 PM
using a string as array indices Justin Perl 1 06-02-2004 11:38 PM
deleting items within a for loop - mutable indices SnuSnu Python 6 04-25-2004 02:09 AM
Problem: Generating random indices for a container Ross MacGregor C++ 5 08-25-2003 06:17 PM



Advertisments