Thursday, June 17, 2010

Exit (break) loop

Exit the loop (for, while, etc.) if the expected condition is met and if applicable.

C# example
public Employee GetEmployee(string strName)
{
    Employee objEmployee = null;
    if (strName != "" && strName != null)
    {
        for (int i = 0; i < m_arrEmployeeCollection.Count; i++)
        {
            if (strName == m_arrEmployeeCollection[i].Name)
            {
                objEmployee = m_arrEmployeeCollection[i];
                break;
            }
        }
    }
    return objEmployee;
}

Rule: Exit the loop if expected condition is met.

No comments: