FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Switch / Case problem
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Switch / Case problem  (Read 324 times)
spectre_240sx

Email
Switch / Case problem
« on: Apr 15th, 2004, 3:37am »

Ok, how can anyone have a problem with this, right? Well, I figured out a way.
 
Anyway, I've got a pretty simple statement, but for some reason, the values keep going to default rather than one of the cases.
 
Here's the code. It's in a method of a class called ant. Direction is defined as an int in the begining of the class and called in the constructor.
 
Code:

switch (Direction) {
   case 0: posY++;
   case 1: posX++; posY++;
   case 2: posX++;
   case 3: posX++; posY--;
   case 4: posY--;
   case 5: posX--; posY--;
   case 6: posX--;
   case 7: posX--; posY++;
   default: println("Error. " + Direction);
 }

 
at least 50% of the time I get a return of "error..." but the direction listed is one with a specific case already assigned... anyone have any ideas of what might be the problem?
 
mungbean

pixelfrenzy WWW
Re: Switch / Case problem
« Reply #1 on: Apr 15th, 2004, 4:02am »

you need a "break" (don't we all?) after each case.
 
 
Code:

case 0: posY++;
   break;

 
and so on.  Otherwise execution will carry on into the
next case (which can sometimes be useful, but not often.)
 
 
 
 
Pages: 1 

« Previous topic | Next topic »