The attached screenshot is basically what you want. The loop is highlighted. Note how I check for A or B after the loop too.