button press with LCD

Hello, i am very beginner to programming and i have been tinkering with basic sketches in the Arduino IDE examples. I want to combine two sketches. I want to use the button sketch and LCD “hello world” sketch.

Basically, i want to whenever a button is pressed light an led and print on the LCD “LED on” or if it not pressed then i want it to say on the LCD “LED off”.

I tried my best to put everything together in the IDE but i am not sure i put everything in the correct format. Unfortunately, i lost my old LCD screen, so i don not have a way to physically test it but i want to know from an experienced programmer if i did it correctly. ( the sketch does compile by the way)

#include <LiquidCrystal.h>
const int buttonPin = 9;
int buttonState = 0;
const int ledPin = 8;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

  pinMode(ledPin, OUTPUT);
 
  pinMode(buttonPin, INPUT);
  
  lcd.begin(16, 2);
  
}

void loop() {
 buttonState = digitalRead(buttonPin);

 
  if (buttonState == HIGH) {
   
    digitalWrite(ledPin, HIGH);
  
   lcd.print("LED on!");

 } else {
    
    digitalWrite(ledPin, LOW);
  lcd.print("LED off");
  }
  lcd.setCursor(0, 1);

  lcd.print(millis() / 1000);
}

Where do you expect the cursor to be when you are printing "LED on" and "LED off" messages?

As written it looks like you'll print the LCD message all the time *while * the button is pressed/unpressed. In the examples go to 'state change detection' and work with that. You want to print the message once when the switch *becomes * pressed/unpressed.