For loops

Oct 22, 2020 at 1:15am
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 Oct 22, 2020 at 8:45pm
Oct 22, 2020 at 1:22am
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 Oct 22, 2020 at 1:26am
Oct 22, 2020 at 6:24am
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 Oct 22, 2020 at 7:40am
Oct 22, 2020 at 10:39am
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 Oct 22, 2020 at 10:40am
Oct 22, 2020 at 3:56pm
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.
Oct 22, 2020 at 4:10pm
@Keskiverto,

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

Andy
Oct 22, 2020 at 8:46pm
Handy Andy, sorry!
Oct 22, 2020 at 8:53pm
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";
}
}
Oct 22, 2020 at 9:07pm
That loop only happens 99 times, though :)
Nov 1, 2020 at 11:32pm
;)
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 Nov 1, 2020 at 11:33pm
Nov 1, 2020 at 11:48pm
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
Nov 2, 2020 at 11:47pm
Thanks Andy! Will do!
Topic archived. No new replies allowed.