Sorry I should have mentioned that. Yes it is a 8266, more specifically a WEMOD D1 mini Pro.
Using Arduino version 1.8.16. I tried version 2 but I had problems and backed off.
What's the difference between the two sketches - besides the fact that the first one you posted is not complete?
If I try the second one, it works as expected.
Edit: Ok, I see, you exchanged two lines. But that makes no difference for me.
Tested on a WeMos D1 mini (no Pro), driver "LOLIN(WEMOS) D1 mini (clone)", IDE 1.8.19: everything is fine, so unable to reproduce the issue.
What exactly is the board driver you're using? Are you sure you set the IDE to "LOLIN(WEMOS) D1 mini Pro"?
Just in case, try to completely remove any trace of your 1.8.16 IDE (I hope you haven't installed it from Microsft Store...) and make a new clean install: get the zip "portable" version, copy its contents into any local directory (e.g. "C:\Arduino") and use it after adding the ESP boards drivers.
PS: there's no need for that "#include <Arduino.h>"...
No, I didn't expect it to, that's not what I was implying.
I agree the board is resetting. There's no other way that setup() would get run over and over like that. The question we need to be asking is why is it resetting.
With ESP boards, one reason can be that:
In the background, the ESP is attempting to connect to your WiFi router, using the last SSID/password it has saved, even if your sketch doesn't call for that to happen.
Connecting to WiFi can draw a lot of power. If the power supply or USB cable can't supply that power without suffering a voltage drop, that voltage drop will trigger an automatic "brown-out" reset.
So I would suggest trying different USB cables, different USB ports, different D1 minis if you have them, even a different laptop/PC.
That makes it more interesting. Every time I've seen an esp8266 reset there is a bunch of debug information that gets printed. Of course I haven't had all possible esp8266 errors.
Thanks for all the ideas. I'm a bit handicapped in that I'm in a motor home in PA at a campground that has less than desirable internet access. I have two hotspots, an AT&T and T-Mobile plus the campground WiFi. I think the campground WiFi is a repeater of the AT&T cell system 'cause when my hotspot stops working the campground WiFi also does. Signals aren't very strong and even when I can run a speed test, I get around 2 Mb/s and lots of dropouts. Downloading anything is a chore.
I happened to have another 8266 dev board with me, I discovered, so I plugged it in and ran a few tests. It behaves as expected, such as others have reported so I only have to believe the D1 has a problem. I've also discovered that the problem only arises when the board is reset at the end of the upload. If I press the restart button it works OK and if I remove and replace power it seems to work OK. The flakey board is mounted on a PC board that I designed, had manufactured and populated. It's not very complex, including the D1 there is an ADS1115 ADC and associated resistors/diodes to condition the input. I can't imagine an I2C part causing the problem. It is connected to the standard clock and data pins.