Help me to understand this code !!!

This is the basic code to toggle the led on and off using a Button. With the help of paul McWhorter this works fine but I don't understand why it works...please someone help me with that...

int ledPin = 12;
int buttonPin =8;
int ledstate = 0;
int swi_old = 1;
int swi_new;
int dt = 100;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  swi_new = digitalRead(buttonPin);

  if(swi_old == 0 && swi_new == 1)
  {
    if(ledstate == 0)
    {
      digitalWrite(ledPin, HIGH);
      ledstate = 1;
    } 
    else
    {
      digitalWrite(ledPin, LOW);
      ledstate = 0;
    }


  }
  swi_old = swi_new;
  delay(dt);

}

```

reddit.com
u/VezTheAce — 11 days ago

What Colleges can I get ??

What are the best possible option for me.... preference is cse and allied branches then ece and allied branches

u/VezTheAce — 25 days ago

Hostel scenario of here ??

I have heard that IIIT surat does not have permanent campus right now and something like "phase 1 construction will be completed by Jan 27 ". What is hostel situation rn ??

I may get CSE here...

reddit.com
u/VezTheAce — 28 days ago