Can a for loop be used outside of loop?

Can a for loop be used outside of loop?

I have a Mega board and need to set pins 2-43 as outputs.
Q1: Instead creating 41 similar lines of code as shown below, could I use a for loop for values 2-43?

Q2: Should the pinmode be in setup? Or be above setup?

Thanks

pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
.
.
.
pinMode(led43, OUTPUT);

In setup(). Good idea. Let us know how it turns out.

You may need to study arrays to get it working.

You can create your on function with void Function() command
You can read about it here Arduino - FunctionDeclaration but best for pinMode is in the setup()