There are many scenario when we need to prevent recursion inside the Salesforce. So this recipe will guide you how to stop recursive trigger in Salesforce.

For example you are updating a field using workflow which fire triggers again.

Recursive Code:

Trigger SampleTrigger on Contact (before update)
{
   //Your logic which insert account object
   insert Account
}

Rectified code:

public static class ConstantClass {
{
   public static Boolean isTriggerExecuted=false;
}

Trigger SampleTrigger on Contact (before update)
{
   if(!ConstantClass.isTriggerExecuted)
   {
    //your logic which insert account object
    insert acc;
    ConstantClass.isTriggerExecuted=true;

   }
}

As you can see from above code we are now using a static variable which is used as flag to stop recursion because we are now checking when we execute our code.