Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— Presentation transcript:

1 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.

2 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

3 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

4 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

5 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 10.00 User types Y while Purchase == 'Y' TotalPurchases = TotalPurchases + PurchaseAmt GET PurchaseYN GET PurchaseAmt User types 25.00 User types N Program Displays 35.00 ©Christine S. Wolfe

6 T Wear a jacket while Temp < 60 F READ Temp READ Temp 35 60 70 20 50 T 352070 Remove jacket ©Christine S. Wolfe

7 After INIT Body of Loop T x = x + 1 while x != 6 F x = 1 PRINT x X 11 22 33 44 55 6 After Loop TTTTT F 12345 ©Christine S. Wolfe

8 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 2100.00 3200.00 100.00 100.00 4350.00 150.00 5450.00 100.00 6550.00 100.00 7700.00 150.00 8800.00 100.00 9900.00 100.00 101000.00 100.00 150.00 1150.0011 150.00 1300.0012 100.00 1400.0013 ©Christine S. Wolfe


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google