Need help with explaining me a code

can someone explain what's going on in this code?

const int redLED = 12;
const int greenLED = 11;
const int BUTTON = 2;

int val = 0;
int state = 0;
int old = 0;

void setup()
{
pinMode(BUTTON, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);

digitalWrite (redLED, LOW);
digitalWrite (greenLED, HIGH);
}
void loop(){
val = digitalRead(BUTTON);
if (val==1){
delay(50); //vente i 0,05 seconds
val = digitalRead(BUTTON);
if (val==0){
state = old + 1;
}}
switch (state){
case 1:
digitalWrite (redLED, HIGH);
digitalWrite (greenLED, LOW);
old = state;
break;
case 2:
digitalWrite (redLED, LOW);
digitalWrite (greenLED, HIGH);
old = 0;

}
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

Here is your code posted in code tags to make it easier to read. I have added comments to it

const int redLED = 12;
const int greenLED = 11;
const int BUTTON = 2;

int val = 0;
int state = 0;
int old = 0;

void setup()
{
  pinMode(BUTTON, INPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite (redLED, LOW);
  digitalWrite (greenLED, HIGH);
}
void loop()
{
  val = digitalRead(BUTTON);  //read the button state
  if (val == 1) //if the state is HIGH
  {
    delay(50); //vente i 0,05 seconds
    val = digitalRead(BUTTON);  //read the state again
    if (val == 0) //if it is now LOW
    {
      state = old + 1;  //add 1 to state variable
    }
  }
  switch (state)
  {
    case 1: //if state is currently 1
      digitalWrite (redLED, HIGH);  //set the state of 2 LEDs
      digitalWrite (greenLED, LOW);
      old = state;  //save the value of state
      break;  //jump to end of switch code bloack
    case 2: //if state is currently 2
      digitalWrite (redLED, LOW); //set the state of 2 LEDs
      digitalWrite (greenLED, HIGH);
      old = 0;  //reset old to zero
  }
}

Basically it changes the state of 2 LEDs every time the button is pressed as long as it is wired correctly

How is the button wired ?

Hi. @philli2407
This code is very easy to find out how it works.
If you have difficulty understanding then you have very little knowledge of the C language.
You need to know how it works out of curiosity or you want to learn the language.
To learn C language, or the use of C language in Arduino,
I suggest:
https://www.learncpp.com/
or
https://startingelectronics.org/software/arduino/learn-to-program-course/
RV mineirin

@UKHeliBob
Here’s how ive set it up:

If you have any recommendations on how i can make the code easier for my project, i would highly apperciate that

The code as written depends on precise timing on the part of the user when pressing and releasing the button. Is that a requirement of the project or would it be OK if the change of LED states occurred no matter how long the button was held ?

If the latter then look at the StateChangeDetection example in the IDE to see how to detect when a button becomes pressed rather than when it is pressed

Whilst it would not change the operation of the code you should consider not wasting memory by using as small a data type as possible for variables. For instance, why is the button pin declared as a int that takes 2 bytes of memory rather than as a byte ?

@philli2407, your topic was moved to a more suitable location on the forum.

@UKHeliBob
it doesn't matter of the timing, my project is just to be able to switch the lights by pressing a button. Like if a room is closed, you press the button so it lights red, and if it's avaiable you press it so it lights green. The code is copied from another arduino project, but probably didn't have the same intentions as my project. Can you rewrite the code to make it easier? and the "int" as you mentioned, what do i do to just make it take up 1 byte? Thanks in advance

You will learn a lot more if you write it yourself. Look at the example I suggested and adapt it to your needs. If you get stuck then post your code to this topic and explain the problems