Transcript LECT15.PPT

The ‘while’ loop
‘round and ‘round we go
CMSC 104
1
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
CMSC 104
2
Our example while loop
while ( children > 0 )
{
children = children - 1;
cookies = cookies * 2 ;
}
CMSC 104
3
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 ?

o total
o counter

CMSC 104
Do we need to initialize them to some
value ? YES
total = 0 counter = 1
4
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
5
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);
}
CMSC 104
6
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 :

o Ask the user how many students are in the
class - use that number in the condition of
the while loop.
CMSC 104
7
A better algorithm
Grade Average for N Students
Set total to zero
Set grade counter to one
Get number of students, numStudents
While grade counter is <= numStudents
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 / numStudents
Print the class average
CMSC 104
8
Grade Average
for N students
CMSC 104
#include <stdio.h>
main ( )
{
int numStudents; counter, grade, total, average ;
total = 0 ;
counter = 1 ;
printf (“Enter Number of Students: “);
scanf (“%d”, &numStudents);
while ( counter <= numStudents) {
printf (“Enter grade : “);
scanf (“%d”, &grade);
total = total + grade ;
counter = counter + 1;
}
average = total / numStudents ;
printf (“Class average is %d\n”, average);
}
9
Why bother to make it easier ?

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.
 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

CMSC 104
10
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 )

CMSC 104
11
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.

CMSC 104
12
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
13
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 */

CMSC 104
14
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;
}
CMSC 104
15
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
CMSC 104 change a type for a calculation.

16
What Happens When We Cast ?

Before the calculation statement
counter total
average
38
2890
garbage
int
int
float
 During the calculation
counter total
average
38
2890
garbage
int
int
float
same value
as total
2890.0000
float
 After the calculation
counter total
average
38
2890
76.0526
CMSC 104
17
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 (“Enter a positive integer : “) ;
scanf (“%d”, &num) ;
}
printf (“You entered %d\n”,num);
CMSC 104
18