Christophe Verré wrote:
This will block if second==first, because you're only initializing second here, its values will never change after the initialization. The second loop will not change its value, so it will loop forever. Same for the other for loops.
I wonder if I'm not understanding exactly what's going on in each of these loops.
The way I'm reading these is that it will create a random number for the variable second and check that it doesn't match a previous number. If it does then won't it select another random number, check again and keep going until it finds one that doesn't match the previous. Maybe I'm totally wrong in this. The book example did it with a switch, but my first idea was to do it this way and I really want to make it work. Guess I'm stubborn.
No. The first part of a for loop is the initialisation step - this is only done once. the value of second will never change after this. So it either gets set to something different from the first number and immediately breaks from the loop or it gets set to the same as the first number and just sits in the loop forever checking if the first value is not equal to the second value.
Donnie Inman wrote:If it does then won't it select another random number
You need to move the setting of 'second to the last part of the for loop - i.e. after the second ;
you should also move the test from inside the loop to the second part of the for loop i.e. between the two ;. That way you don't need to use a break which should be avoided if possible.
Although personally, I think a while loop would be better here
Also, if you're going to use a for loop, use all it's capabilities. For the outer loop, using a for loop is correct but change it to
and get rid of