Decide What You're Going To Say
Normally, spontaneity is considered best, anything rehearsed sounds weird. However, in this situation, it is better to decide what you are going to say in advance, along with some words of condolence. There is no need to learn it verbatim, just have a fair idea of what you will say and remain flexible to adapt according to the person's reaction. You do not want to end up saying anything insensitive, which could aggravate the pain of the person.