When do I use gerund and when do I use infinitive. Please help.

" Swimming/To swim is a good form of exercise."

In this case, use the gerund.

Swimming is a good form of exercise.

Infinitives are usually used after another conjugated verb:

I am going to swim. Do you want to swim?
Sometimes I see that to-infinitive is a subject.

For example: To teach is to learn.

Please help. Thanks.