Download presentation
Presentation is loading. Please wait.
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 for loop. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: for *affect the condition (of the loop) initialization (of the loop) loop body loop repetition condition
3
In a for loop, a set of instructions is executed as long as a condition is true. } The snippet below allows the user calculate the average score, expressed as a percent correct, on a 50 point exam given to 4 students. TotalScores += Score; printf("\nEnter the points earned by Student Number %d", StudentNum); scanf("\n%lf", &Score); { for(StudentNum = 1; StudentNum <= 4; ++StudentNum) TotalScores = 0.0; AverageScore = TotalScores / 4; AveragePercent = AverageScore / 50; /* The exam is worth 50 pts */ printf("\nThe average score is %f points which is %f percent.", AverageScore, AveragePercent); ©Christine S. Wolfe
4
} TotalScores += Score; printf("\nEnter the points earned by Student Number %d", StudentNum); scanf("\n%lf", &Score); { for(StudentNum = 1; StudentNum <= 4; ++StudentNum) TotalScores = 0.0; AverageScore = TotalScores / 4; AveragePercent = AverageScore / 50; /* The exam is worth 50 pts */ printf("\nThe average score is %f points which is %f percent.", AverageScore, AveragePercent); Click through each step to translate the code into a flowchart. start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent F ©Christine S. Wolfe
5
TotalScores: 0 start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent Click through, step by step, to see the program flow in response to user input. User Enters: 48 start TotalScores = 0.0 GET Score StudentNum =1 <= 4 +1 TotalScores += Score T F TotalScores: 48 StudentNum: 1 StudentNum: 2 ©Christine S. Wolfe
6
TotalScores: 48 start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores /4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent Click through, step by step, to see the program flow in response to user input. User Enters: 45 GET Score StudentNum =1 <= 4 +1 TotalScores += Score T F TotalScores: 93 StudentNum: 2 StudentNum: 3 ©Christine S. Wolfe
7
TotalScores: 93 start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent Click through, step by step, to see the program flow in response to user input. User Enters: 40 GET Score StudentNum =1 <= 4 +1 TotalScores += Score T F TotalScores: 133 StudentNum: 3 StudentNum: 4 ©Christine S. Wolfe
8
TotalScores: 93 start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent Click through, step by step, to see the program flow in response to user input. User Enters: 30 GET Score StudentNum =1 <= 4 +1 TotalScores += Score T F TotalScores: 163 StudentNum: 3 StudentNum: 5 ©Christine S. Wolfe
9
AveragePercent: 0.81 AverageScore: 40.75 TotalScores: 93 start TotalScores = 0.0 end GET Score A T A StudentNum =1 <= 4 +1 TotalScores += Score AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent Click through, step by step, to see the program flow in response to user input. T F TotalScores: 163 StudentNum: 3 StudentNum: 5 end AverageScore = TotalScores / 4 AveragePercent = AverageScore / 50 DISPLAY AverageScore, AveragePercent ©Christine S. Wolfe
10
After INIT Body of Loop T F X 11 22 33 44 55 6 12345 x =1 != 6 +1 DISPLAY X VALUE AFTER LOOP ©Christine S. Wolfe
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.