Replace foreach macros with range based for loops in many places.

Authored by asemke on Dec 3 2017, 10:09 AM.