Prev | Current Page 193 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

We can use a while statement to read the file until there are no
more scores in the file to be read:
double total = 0.0;
BufferedReader in = new BufferedReader(
new FileReader( "Students" ) );
String score = in.readLine();
while( score != null ) {
total = total + Double.parseDouble( score );
score = in.readLine();
}
CHAP. 5] PROGRAMMING IN JAVA 73
We use the same BufferedReader as before to read the file Students. After reading the first line in
the file, the while statement checks to see if the variable score has a value of null. The variable score will
be null only if there was nothing in the file to read. As long as score is not null, the body of the while statement
will execute??”it will ???parse,??? or interpret, as a fractional number, whatever the BufferedReader just
read, and then it will read the next line in the file.
The while statement syntax is very simple:
while( )
Again, the statement can be a code block enclosed in curly braces. As long as the test condition remains
true, the body of the while statement continues to execute. This is a very appropriate control structure when
we do not know in advance how many times a code block must execute.
do-while
A variation of the while statement is the do-while.


Pages:
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
projekty domków letniskowych grzejniki łazienkowe Hotel spa chemia kosmetyki hotele w miedzyzdrojach