Update material.cpp

Avoid code duplication by using for loop.

No functional change.