motor turn in opposit direction with push button

HI
could anybody please correct this code to turn the motor opposit direction and continue when abutton is pressed and released . below are 2 codes that i have tried, but it doesn`t work.

code-1 (this code doesn`t work at all

int switchPin =2;
int motorPin1 = 3;
int motorPin2 = 4;

void setup(){

pinMode(switchPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop(){
if (digitalRead(switchPin) == HIGH) {

digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
}
if (digitalRead(switchPin) == LOW) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
}
}

code 2 (in this code when the button is pressed motor turn opposit direction but when the button release it turns same direction as before)

int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int potPin=0;

void setup ()
{
pinMode(in1Pin,OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}
void loop()
{
int speed = analogRead(potPin)/4;
boolean reverse=digitalRead(switchPin);
setMotor(speed, reverse);
}

void setMotor(int speed, boolean reverse)
{
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);

}

You need to detect when the button becomes pressed rather than when it is pressed. Each time you detect that it has become pressed flip the state of a variable to indicate the direction to rotate in and act on it .

Have a look at the StateChangeDetection example in the IDE to see the principle.

The two examples have different pin assignments - so clearly one of them won't work!

Thank you very much for all your replies. still I cant figure out how to do it. I am still learning. don`t have much knowledge in programming. if somebody could change one of above codes that will be really appreciated.
I am using this for hobby project- home made automatic car wash.
The high pressure hose traveling forward and back ward on a track. plan is when it reach the forward end it press a switch and then it travels backward. when it reach the back end same thing happens.

You will learn much more if you write it yourself but here is some pseudo code to help you

declare variables as usual
set direction variable to 0 (indicating stopped)

setup()
{
   pinMode()s etc 
}

start of loop()
  if the start button becomes pressed (assuming that some action starts the process)
    set direction variable to forward (maybe set it to 1 to indicate forward)
  end if
  
  if the stop button becomes pressed (assuming that some action stops the process)
    stop the motor
    set direction to 0 to indicate that the motor has stopped
  end if
  
  if direction is forward
    run the motor in forward direction
    if the forward limit switch becomes pressed
      stop the motor
      set direction to reverse (maybe set it to 2 to indicate reverse)
    end if
  end if

  if direction is reverse
    run the motor in reverse direction
    if the reverse limit switch becomes pressed
      stop the motor
      set direction to forward (maybe set it to 1 to indicate forward)
    end if
  end if
end of loop()