// Demonstrating the use of a while loop with a counter // condition. // Sasha Vassar Week 2, Lecture 4 /* Using a while loop allows us to repeat certain pieces of code. 1. Initialise loop control variable while (condition) { //2. Test the condition against the loop control variable //3. Update loop control variable (last statement of while loop } */ #include // Problem: I want to enter five lots of scores and add them together. // (Round the loop five times (anyone dizzy?)). int main (void) { int score; int sum = 0; int count = 1; //1. Setting the loop control variable while (count <= 5) { //2 < 5? Yes, it is - go inside while loop //1. Enter a score printf("Enter a score: "); scanf("%d", &score); //2. Add the score to a sum sum = sum + score; printf("You have gone around the loop %d times, and the sum is now %d\n", count, sum); count = count + 1; } return 0; }