Basic Programming Question

When 'flag' is true your code is allowed to check for a HIGH on your switch.
When 'flag' is false your code is allowed to check for a LOW on your switch.

Setting 'flag' false once a HIGH is detected prevents the 'if()' from running more than once.

You will find that 'flags' will be useful in preventing code form running or letting different code to run.

In the future:
Using delay() freezes your code/sketch from executing during the delay time.
This is very often not desired.

Study this, you will need to understand it soon.
https://forum.arduino.cc/index.php?topic=223286.0