2

In C++, a for loop normally takes three statements, in the form:

for (init; condition; step)
{
Loop statements
}

Can I place two or more statements in the place of init? Let's say I want to define two starting variables, a and b. To do this, I would use int a = 1; int b = 5;. However, since there is a ; between the statements, C++ would interpret int b = 5 as the condition statement. Is there a way to clump the whole statement into init, perhaps by using brackets? Can something similar be done for step?

Note: I am aware that I can initialize a variable before calling for. However, I feel that it would be more logical to place loop-related statements within the definition of the loop.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Johan
  • 343
  • 1
  • 4
  • 13
  • See this post: http://stackoverflow.com/a/1232182/57135 – Joe Dec 07 '14 at 18:57
  • @Joe The fact that the answer from another question answers this question accidentally doesn't make this one a duplicate. Questions are different. – BartoszKP Dec 07 '14 at 18:58
  • 1
    The [comma operator](http://www.mindtribe.com/2011/07/forgotten-c-the-comma-operator/) is not limited to `for` loops. – motoku Dec 07 '14 at 18:58

6 Answers6

5

No, you can only have one initializing statement. However, frequently you can use the comma operator to achieve the desired result:

for(int foo = 7, bar = 42; ...; ...) {
    ...
}

or even

int foo;
double bar;
for(foo = 7, bar = 42; ...; ...) {
    ...
}

What is not possible, is to declare two variables of different type within the initialization statement:

//Illegal code!
for(int foo = 7, double bar = 42; ...; ...) {
    ...
}
cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
4

try for (int a =1, b=5; condition; a++,b++) {}

Update

Not everything is possible but some pretty amazing things can be achieved by calling a function inside.

void bar() { /* some of the code you want */ }

int main(int argc, char **argv) {
  int i = 0;
  for(bar();i < 10; i++) {
    // your stuff
  }
}

if you consider using C++11 you can define and call an unnamed function in the place where you would initialize your variables like this:

int a = 0, b = 1;
for ([&a, &b]{ a = b++; b = foo(a); }(); b < 10; a++) {}

I don't see any benefit of doing this though.

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
  • Thank you for your answer. However, I was looking for a way to combine any two statements, not a way to fuse `int a;` and `int b;`. For the sake of argument, the statements may be variable definitions, function calls, `cout << stuff;`, anything which normally ends with `;`. Is there a way to clump them together? – Johan Dec 07 '14 at 19:03
  • care to give an idea what you want to achieve by that? doing scoped allocation might be difficult. alot of other things might be possible. – Alexander Oh Dec 07 '14 at 19:11
  • I had nothing particular in mind. I'm merely trying to learn all of the possibilities I have with the language. Unfortunately, in this case there doesn't seem to be any concise way to clump statements together. – Johan Dec 07 '14 at 19:23
1

you can have more than two INITIALIZING statements, as much as you want, but delimit them by a comma , :

for(a=1, b=25, c='C', d=25.0; b>a; a++){
}

but you cant have more than one declaration statement , and the declared variables must be in the first of statements :

for(int a=1, b=25, c='C', d=25.0; b>a; a++){}

not :

for(a=1, int b=25, c='C', d=25.0; b>a; a++){} // wrong !

,,,

you can also have multiple conditions or instructions , this is an example , the second two tests have no effect but it is just for demonstrating :

int i;
for(char a='A', i=0; i<26 || a+i<='Z' && a+i>='A' ;i++ , printf("-") ){
    printf("%c", a+i);
}

this will print : A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z-

,,,

good luck,

younes zeboudj
  • 856
  • 12
  • 22
0

Separate with commas.

for(grunt, snarl; foo; goo) {
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
0
 for(<initialization> ; <condition> ; <increment/decrement variables>){}

Initialization and increment/decrement of variables can be multiple separated by comma. But condition will be single,i.e. we cannot have multiple conditions separated by comma.

e,g.  for(i=1,j=2,k=3,l=4;j<6;k++,j--,++i,l++){}.  
MoBaShiR
  • 442
  • 3
  • 12
0

You can't have more that one initializing statements where more than one types are declared. You can however mimic that with the following hack :

for (struct { int a; char b; } i = {1, 'x'}; i.a < 15; ++i.a, ++i.b) 
{
    // ...
}

This way, you're using an unnamed struct to aggregate multiple variables of various types, and yes, this is valid C++.

Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63