change state of LED using Condition

how to change the state of led i am making capacitive proximity switch i wrote the code for switching led should remain on until it detect next change in the capacitance

#include <CapacitiveSensor.h>
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

const int ledPin = 12;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {
long sensorValue = capSensor.capacitiveSensor(30);
boolean state=LOW;


if(sensorValue >40 && state==LOW) {
  
   Serial.println("high");
  
  digitalWrite(ledPin, HIGH);
      state=!state;
       
  
}
else if(sensorValue >40 && state==HIGH){
 Serial.println("low");
 
  digitalWrite(ledPin, LOW);
      state=!state;
      delay(1000);
}

}

should remain on until it detect next change in the capacitance

Nothing in that code knows about the previous value (of the capacitance sensor), so this is not possible.

Now, if you were to save the current reading as the previous reading, then next time loop() gets called, you would have a previous reading and a current reading, and you could compare them.

Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.