# Switch in a For loop

• December 6th, 2012, 02:41 PM
Levica
Switch in a For loop
Code Java:

``` for(int r=25;r<=125;r+=25) { int color = 1; switch(color) {   case 1 : g.setColor(Color.black); Expo.fillCircle(g,625,175,r); color++; break; case 2 : g.setColor(Color.white); Expo.fillCircle(g,625,175,r); color++; break; case 3 : g.setColor(Color.blue); Expo.fillCircle(g,625,175,r); color++; break; case 4 : g.setColor(Color.red); Expo.fillCircle(g,625,175,r); color++; break; case 5 : g.setColor(Color.yellow); Expo.fillCircle(g,625,175,r); color++; break; }     } g.setColor(Color.black); //reset colors.```

I am attempting to draw 5 concentric circles with increasing radius and chose the colors that each circle is filled with via a switch of an int. The result is a circle of maximus radius (which would indicate that the for loop is working, right?), but it is all the same color, the color of black which is case 1. What am I doing wrong, why won't the other circles change colors?
• December 6th, 2012, 02:44 PM
curmudgeon
Re: Switch in a For loop
You've declared and initialized your color variable inside of the for loop. So even though you increment it, it reverts back to 1 with every iteration of the loop.
• December 6th, 2012, 02:45 PM
Norm
Re: Switch in a For loop
The code sets the switch variable's value to 1 every time through the loop.
• December 6th, 2012, 02:49 PM
Levica
Re: Switch in a For loop
Thanks!! After changing the int declaration to before the for loop there was still a problem where the largest radius circle of the last color showed up, but I easily fixed that by changing r within the for loop. Thanks again!
• December 7th, 2012, 01:05 PM
Levica
Re: Switch in a For loop
Code :

``` Random rand = new Random(); int color = 0; //1 = black, 2 = white, 3 = blue, 4 = red, 5 = yellow for(int r=125;r<=125;r-=25) //execute loop as long as the radius is 125(the max value used) or less, and decrease radius of circles as executed. { color = rand.nextInt(5)+1; switch(color) //for every circle check the color integer to determine the color the circle is to be filled with. {   case 1 : g.setColor(Color.black); Expo.fillCircle(g,625,175,r); break; case 2 : g.setColor(Color.white); Expo.fillCircle(g,625,175,r); break; case 3 : g.setColor(Color.blue); Expo.fillCircle(g,625,175,r); break; case 4 : g.setColor(Color.red); Expo.fillCircle(g,625,175,r); break; case 5 : g.setColor(Color.yellow); Expo.fillCircle(g,625,175,r); break; }     } g.setColor(Color.black)```

I am attempting to generate random colors for each circle out of the 5 color options, and it works except if you wait long enough after the circle has been drawn, the window will begin flashing random colors incessantly. Why is this?
• December 7th, 2012, 01:21 PM
Norm
Re: Switch in a For loop
Quote:

Why is this?
Probably because of the way the program is coded.
Can you make a small program that compiles, executes and shows the problem

Quote:

the window will begin flashing random colors incessantly.
Can you explain? The code uses 5 colors, each in its own ring. Are you saying that there are more than 5 colors used?

What does the method do with the variable: r? What values are given to r as the code executes?
Add a println that prints out the value of r as the code executes.