for cycle inside switch case

the following code is causing me a lot of problem…

case '5':
     { 
for(int d=1 ; d< 128 ; d++)
  { 
         Serial.print(d);
         Serial.println("before");
         switchbank(d); //my function
          delay(25);
         Serial.print(d);
         Serial.println("after");
  }
Serial.println("END");
   }  
      break;

it should print something like
1before
1after
2before
2after

but it prints
1before
0after
1before
0after

just wondering why my d int resets to 0 after i call my switchbank function…
i tried removing the for cycle this way

case '5':
     { 
         int d = 1 ;
         Serial.print(d);
         Serial.println("before");
         switchbank(d); //my function
          delay(25);
         Serial.print(d);
         Serial.println("after");
         Serial.println("END");
   }  
      break;

and it prints as expected
1before
1after

what’s wrong with that for cycle?

My guess is something wrong with switchbank()
Whatever it is.

case '5':
     {   ///not needed. remove it
int d;  // defining d here may help
for(d=1 ; d< 128 ; d++)
  { 
         Serial.print(d);
         Serial.println("before");
         switchbank(d); //my function 
       // What does your function do to the value of d?
         delay(25);
         Serial.print(d);
         Serial.println("after");
  }
Serial.println("END");
   }   //not needed...remove it
      break;

It would help if you posted the code of your function.

thanks for the answer
i have already tried what you suggest in the code
however how can the function be the problem ,since it is a call by value(so not by reference) shouldnt it pass just a copy of the value?

  void switchbank(int b) 
         { int p [8] = {0,0,0,0,0,0,0,0};
  int x = 0;
  for(x =2; x < 10; x++)
  {
  if ((b %2) ==1 ) { p[x] = 1 ;b--;b = b/2;}else { p[x]= 0;b = b /2;}
  digitalWrite(x,p[x]);
  }
 }

just some raw way to transform a number into his 8bit binary rappresentation

thank you again for your time

Post the whole sketch.

Does the loop finish, as in you get 127 loops of ‘1before’, ‘0after’ then ‘END’. Or does it go on forever?

Format your code better too, might help you find the problem. And if you are going to squash up your code into a single hard to read line, do it properly.

void switchbank(int b){
  for( int x = 1 ; ++x < 10 ; ) digitalWrite( x, ( ( ( b % 2 ) == 1 ) ? --b, b /= 2, 1 : b /= 2, 0 ) );
}
  for(x =2; x < 10; x++)
  {
  if ((b %2) ==1 ) { p[x] = 1

So, you intend to write to p[2], p[3], …, p[9], when the valid indices for p are from 0 to 7? Quit shitting on memory that you don’t own, and your problems will go away.

Simply you can not declair any variable inside a case statement. So any variables you use must have been declaired outside, either as global or as a variable inside the function that contains the case statement.

Simply you can not declair any variable inside a case statement.

Actually, you can.

switch(mode)
{
   case 1:
   {
       int d = 14; // perfectly legal
       Serial.print("d = ");
        Serial.println(d);
   }
   break;
}

You do need the curly braces in the case statement, though.

PaulS:

  for(x =2; x < 10; x++)

{
 if ((b %2) ==1 ) { p = 1



So, you intend to write to p[2], p[3], ...., p[9], when the valid indices for p are from 0 to 7? Quit shitting on memory that you don't own, and your problems will go away.

lol
Forgot to add [x-2]
I feel a bit stupid
I cant fix it now but probably this was causing my problem
Thank you