Presentation is loading. Please wait.

Presentation is loading. Please wait.

The ‘while’ loop ‘round and ‘round we go.

Similar presentations


Presentation on theme: "The ‘while’ loop ‘round and ‘round we go."— Presentation transcript:

1 The ‘while’ loop ‘round and ‘round we go

2 A Repetitive Structure - The while Loop
A repetitive structure allows the programmer to specify that an action is to be repeated while some condition remains true. Example in pseudo-code: while there are still more children subtract one from the # of children multiply the # of cookies by 2 end_while

3 Our example while loop while ( children > 0 ) {
children = children - 1; cookies = cookies * 2 ; }

4 Another while loop example
Problem: We want a program that calculates the average exam grade for a class of 10 students. What variables will we need ? total counter Do we need to initialize them to some value ? YES total = 0 counter = 1

5 The pseudo-code Set total to zero Set grade counter to one
While grade counter is less than or equal to ten Print “Enter another grade” Read grade Add the grade into the total Add one to the grade counter end_while Set the class average to the total divided by ten Print the class average

6 Class Grade Average for 10 Students
#include <stdio.h> main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= 10 ) printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / 10 ; printf (“Class average is %d\n”, average);

7 Class Grade Average Using a Constant
#include <stdio.h> #define STUDENTS 10 main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= STUDENTS ) printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / STUDENTS; printf (“Class average is %d\n”, average);

8 Versatile ? How good is this program ?
Only works with class sizes of 10 Would like it to work with any class size. A better way : Ask the user how many students are in the class - use that number in the condition of the while loop.

9 A better algorithm Grade Average for N Students
Set total to zero Set grade counter to one Get number of students, nrStudents While grade counter is <= nrStudents Input the next grade Add the grade into the total Add one to the grade counter end_while Set the class average to the total / nrStudents Print the class average

10 Grade Average for N students
#include <stdio.h> main ( ) { int nrStudents; counter, grade, total, average ; total = 0 ; counter = 1 ; printf (“Enter Number of Students: “); scanf (“%d”, &nrStudents); while ( counter <= nrStudents) { printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / nrStudents ; printf (“Class average is %d\n”, average);

11 Why bother to make it easier ?
Why do we write programs ? So the user can perform some task The more versatile the program, the more difficult it is to write. BUT it is more useable. The more complex the task, the more difficult it is to write, BUT that is often what a user needs ALWAYS consider the user first

12 Using a Sentinel Value We could let the user keep entering the grades and when he’s done enter some special value that signals us that he’s done. This special signal value is called a sentinel value. We have to make sure that the value we choose as the sentinel isn’t a legal grade. (i.e.. can’t use 0 as the sentinel )

13 The Priming Read When we use a sentinel value to control a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. This is known as a priming read. We have to give significant thought to the initialization of variables, the sentinel value and getting into the loop.

14 Pseudo-code for Using a Sentinel to End a while Loop’s Execution
Initialize total to 0 Initialize counter to 0 Get the first grade from the user While the grade != the sentinel value Add grade to total Add 1 to counter Get the next grade (could be sentinel) end_while average = total / counter Print the average

15 Using a Sentinel to End a while Loop
#include <stdio.h> main ( ) { float average; int counter, grade, total; total = counter = 0; /* tell user the sentinel value in the prompt*/ printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade) ; /* priming read */

16 Using a Sentinel (continued)
while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade); } average = ( float ) total / counter ; printf (“The average was %.2f\n”, average;

17 The cast operator ( ) We can use a cast operator to create a temporary value of the desired type, to be used in a calculation. Does NOT change the variable’s type or how it is stored. Is only good for the statement it’s in. Often used to avoid integer division. Used anytime we want to temporarily change a type for a calculation.

18 What Happens When We Cast ?
Before the calculation statement During the calculation After the calculation counter total average 38 2890 garbage int int float same value as total counter total average 38 2890 garbage int int float float counter total average 38 2890

19 Using a while Loop to Check User Input
main ( ) { int num ; printf (“Enter a positive integer : “) ; scanf (“%d”, &num) ; while ( num < 0 ) printf (“\nThat’s incorrect, try again\n”); } printf (“You entered %d\n”,num);


Download ppt "The ‘while’ loop ‘round and ‘round we go."

Similar presentations


Ads by Google