This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.
while Loops This slide show illustrates the order of execution of a while loop. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: while *affect the condition (of the loop) initialization (of the loop) loop body loop repetition condition
In a while loop, a set of instructions is executed as long as a condition is true. TotalPurchases = 0.0; printf("\nDid you make a purchase? Y or N:"); scanf("\n%c", &PurchaseYN); while (Purchase == 'Y') { printf("\nEnter the purchase amount: "); scanf("\n%d", &PurchaseAmt); TotalPurchases = TotalPurchases + PurchaseAmt; printf("\nDid you make another purchase? Y or N:"); scanf("\n%c", &PurchaseYN); } printf("\nThe total of all purchases is %d", TotalPurchases); The snippet below allows the user to total the cost of a set of purchases. For each purchase, the program asks for the amount of the purchase and adds it to the total. The program then asks the user if he or she has another purchase. As long as the user replies that he or she has another purchase, the program will repeat the set of instructions highlighted below. ©Christine S. Wolfe
Click through each step to translate the code into a flowchart. start GET PurchaseYN TotalPurchases = TotalPurchases + PurchaseAmt TotalPurchases = 0.0 TotalPurchases = 0.0; printf("\nDid you make ar purchase? Y or N:"); scanf("\n%c", &PurchaseYN); while (Purchase == 'Y') printf("\nEnter the purchase amount: "); scanf("\n%d", &PurchaseAmt); { TotalPurchases = TotalPurchases + PurchaseAmt; printf("\nDid you make another purchase? Y or N:"); scanf("\n%c", &PurchaseYN); } end GET PurchaseYN GET PurchaseAmt printf("\nThe total of all purchases is %d", TotalPurchases); A A F T while Purchase == 'Y' Display TotalPurchases ©Christine S. Wolfe
start GET PurchaseYN TotalPurchases = TotalPurchases + PurchaseAmt TotalPurchases = 0.0 end GET PurchaseYN GET PurchaseAmt A A F T while Purchase == 'Y' Display TotalPurchases Click through, step by step, to see the program flow in response to user input. start GET PurchaseYN TotalPurchases = TotalPurchases + PurchaseAmt TotalPurchases = 0.0 end GET PurchaseYN GET PurchaseAmt while Purchase == 'Y' Display TotalPurchases User types Y User types User types Y while Purchase == 'Y' TotalPurchases = TotalPurchases + PurchaseAmt GET PurchaseYN GET PurchaseAmt User types User types N Program Displays ©Christine S. Wolfe
T Wear a jacket while Temp < 60 F READ Temp READ Temp T Remove jacket ©Christine S. Wolfe
After INIT Body of Loop T x = x + 1 while x != 6 F x = 1 PRINT x X After Loop TTTTT F ©Christine S. Wolfe
T while Month <= 12 F Month = 1 YearSales = 0 GET MonthSales YearSales = YearSales + MonthSales Month = Month + 1 After INIT Body of Loop Month 1 After Loop YearSales 0 MonthSales ©Christine S. Wolfe