For loops

Are two initialization of a counters allowed in a for loop? (I'm new)
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {

  for (int i = 0; i < 100; i++ && int a = 1; a < 100; a++){

std::cout<< a<< "This happens 100 times!\n";

} }
Last edited on
1
2
3
4
5
6
7
#include <iostream>

int main() {
    for(int i = 0; i < 100; i++ && int a = 1; a < 100; a++){
        std::cout<< a<< "This happens 100 times!\n";
    }
}

main.cpp: In function ‘int main()’:
main.cpp:4:36: error: expected primary-expression before ‘int’
     for(int i = 0; i < 100; i++ && int a = 1; a < 100; a++){
                                    ^~~

Compiler says no.

https://en.cppreference.com/w/cpp/language/for

There's a lot of funny things you can do with a for loop, but the overall syntax looks like:
for ( declaration-or-expression(optional) ; declaration-or-expression(optional) ; expression(optional) )


The real issue is:
i++ && int a = 1;
Even outside of a for loop, this doesn't make sense. You can't mix a variable declaration and an operation like && like that.

You could move the i++ part to the third section of the for loop (where the a++ is). But if you try to pack too much stuff into one line, then things just become unreadable.
Last edited on
Perhaps,
1
2
3
4
5
6
7
#include <iostream>

int main() {
    for ( int i = 0, a = 1; i < 100 && a < 100; ++i, ++a ) {
        std::cout << a << " This happens 99 times!\n";
    }
}

but for this trivial example:
1
2
3
4
    for ( a = 1; a < 100; ++a ) {
        int i = a - 1; // but i is unused
        std::cout << a << " This happens 99 times!\n";
    }


[EDIT]
1
2
3
int i = 0, a = 1 // is valid declaration of two int variables
i < 100 && a < 100 // is valid boolean expression
++i, ++a // is valid too, although the comma-operator is tricky and best avoided 
Last edited on
Hello AnMTGDude,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Yes. Using the , operator to separate them .
for (int i = 0, j = 1; i < 10; i++)

You can also do:
for (int i = 0, j = 1; i < 10; i++, j++)

Andy
Last edited on
Handy Andy wrote:
Yes. Using the , operator to separate them .
for (int i = 0, j = 1; i < 10; i++)

That is not the comma operator.

Declaration can have a comma-separated list. List separator is not operator.
@Keskiverto,

Thank you. My brain was thinking about something else at the time.

Andy
Handy Andy, sorry!
I found out another way to do it *face palm* i'm so dumb!
1
2
3
4
5
6
7
8
#include <iostream>

int main() {

for(int a = 1; a < 100; a++){
std::cout<< a<<". " <<"This happens 100 times!\n";
}
}
That loop only happens 99 times, though :)
;)
Edit: Yeah I know, I would have to do:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {

for(int a = 1; a < 101; a++){
std::cout<< a<<". " <<"This happens 100 times!\n";
}
}

Last edited on
Hello AnMTGDude,

AnMTGDude wrote:


1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    for (int a = 1; a < 101; a++)
    {
        std::cout << a << ". " << "This happens 100 times!\n";
    }
}


Instead of starting "a" at 1 you should get use to starting at (0) zero because most things like arrays are zero based.

On a rare occasion like you have here starting works and I would prefer to use 101 over "<=" as this can end up doing 1 moer loop than you want.

Andy
Thanks Andy! Will do!
Topic archived. No new replies allowed.