STYLE: simplify loops

Use the foreach construct to iterate over the user actions.
Remove a test from inside a loop as it only needs to be done once.
From feedback from finetjul