The while loop round and round we go

  • Slides: 19
Download presentation
The ‘while’ loop ‘round and ‘round we go CMSC 104 1

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

A Repetitive Structure The while Loop l l A repetitive structure allows the programmer

A Repetitive Structure The while Loop l l 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 cookies /* give one cookie to one child */ subtract one from the # of children subtract one from the # of cookies end_while CMSC 104 2

Our example while loop while ( children > 0 ) { children = children

Our example while loop while ( children > 0 ) { children = children - 1; cookies = cookies - 1; } /* Note that if the condition is FALSE, the loop body will never be entered. */ CMSC 104 3

Another while loop example Problem: We want a program that calculates the average exam

Another while loop example Problem: We want a program that calculates the average exam grade for a class of 10 students. l What variables will we need ? l o total o counter l CMSC 104 What is the first thing we do to these variables? 4

Another While Loop Example l CMSC 104 We initialize them! total = 0; counter

Another While Loop Example l CMSC 104 We initialize them! total = 0; counter = 1; 5

The pseudo-code Set total to zero Set grade counter to one While grade counter

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 CMSC 104 6

Class Grade Average for 10 Students #include <stdio. h> main ( ) { int

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); /* input from console */ total = total + grade ; counter = counter + 1; } average = total / 10 ; printf (“Class average is %dn”, average); } CMSC 104 7

Versatile ? How good is this program ? l Only works with class sizes

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

A better algorithm Grade Average for N Students Set total to zero Set grade

A better algorithm Grade Average for N Students Set total to zero Set grade counter to one Get number of students, nr. Students While grade counter is <= nr. Students 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 / nr. Students Print the class average CMSC 104 9

Grade Average for N students #include <stdio. h> main ( ) { int nr.

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

Why bother to make it easier ? l Why do we write programs ?

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

Using a Sentinel Value We could let the user keep entering the grades and

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. l This special signal value is called a sentinel value. l 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 ) l CMSC 104 12

The Priming Read When we use a sentinel value to control a while loop,

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. l This is known as a priming read. l We have to give significant thought to the initialization of variables, the sentinel value and getting into the loop. l CMSC 104 13

Pseudo-code for Using a Sentinel to End a while Loop’s Execution Initialize total to

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 CMSC 104 Print the average 14

Using a Sentinel to End a while Loop #include <stdio. h> main ( )

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 */ l CMSC 104 15

Using a Sentinel (continued) while (grade != -1) { total = total + grade

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 %. 2 fn”, average; } CMSC 104 16

The cast operator ( ) We can use a cast operator to create a

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

What Happens When We Cast ? l Before the calculation statement counter total average

What Happens When We Cast ? l Before the calculation statement counter total average 38 2890 garbage int float l During the calculation counter total average 38 2890 garbage int float same value as total 2890. 0000 float l After the calculation counter total average 38 2890 76. 0526 CMSC 104 18

Using a while Loop to Check User Input main ( ) { int num

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