Section 6.7. The while Statement


6.7. The while Statement

A while statement repeatedly executes a target statement as long as a condition is true. Its syntactic form is

      while (condition)               statement 

Exercises Section 6.6.5

Exercise 6.7:

There is one problem with our vowel-counting program as we've implemented it: It doesn't count capital letters as vowels. Write a program that counts both lower- and uppercase letters as the appropriate vowelthat is, your program should count both 'a' and 'A' as part of aCnt, and so forth.

Exercise 6.8:

Modify our vowel-count program so that it also counts the number of blank spaces, tabs, and newlines read.

Exercise 6.9:

Modify our vowel-count program so that it counts the number of occurrences of the following two-character sequences: ff, fl, and fi.

Exercise 6.10:

Each of the programs in the highlighted text on page 206 contains a common programming error. Identify and correct each error.


The statement (which is often a block) is executed as long as the condition evaluates as true. The condition may not be empty. If the first evaluation of condition yields false, statement is not executed.

The condition can be an expression or an initialized variable definition:

      bool quit = false;      while (!quit) {                  // expression as condition          quit = do_something();      }      while (int loc = search(name)) { // initialized variable as condition              // do something      } 

Any variable defined in the condition is visible only within the block associated with the while. On each trip through the loop, the initialized value is converted to bool (Section 5.12.3, p. 182). If the value evaluates as TRue, the while body is executed. Ordinarily, the condition itself or the loop body must do something to change the value of the expression. Otherwise, the loop might never terminate.

Variables defined in the condition are created and destroyed on each trip through the loop.



Using a while Loop

We have already seen a number of while loops, but for completeness, here is an example that copies the contents of one array into another:

      // arr1 is an array of ints      int *source = arr1;      size_t sz = sizeof(arr1)/sizeof(*arr1); // number of elements      int *dest = new int[sz];                // uninitialized elements      while (source != arr1 + sz)          *dest++ = *source++; //  copy element and increment pointers 

Code for Exercises in Section 6.6.5

      (a) switch (ival) {              case 'a': aCnt++;              case 'e': eCnt++;              default: iouCnt++;          }      (b) switch (ival) {              case 1:                  int ix = get_value();                  ivec[ ix ] = ival;                  break;              default:                  ix = ivec.size()-1;                  ivec[ ix ] = ival;          }      (c) switch (ival) {              case 1, 3, 5, 7, 9:                  oddcnt++;                  break;              case 2, 4, 6, 8, 10:                  evencnt++;                  break;          }      (d) int ival=512 jval=1024, kval=4096;          int bufsize;          // ...          switch(swt) {              case ival:                  bufsize = ival * sizeof(int);                  break;              case jval:                  bufsize = jval * sizeof(int);                  break;              case kval:                  bufsize = kval * sizeof(int);                  break;          } 


We start by initializing source and dest to point to the first element of their respective arrays. The condition in the while tests whether we've reached the end of the array from which we are copying. If not, we execute the body of the loop. The body contains only a single statement, which copies the element and increments both pointers so that they point to the next element in their corresponding arrays.

As we saw in the "Advice" box on page 164, C++ programmers tend to write terse expressions. The statement in the body of the while

      *dest++ = *source++; 

is a classic example. This expression is equivalent to

      {          *dest = *source; // copy element          ++dest;  // increment the pointers          ++source;      } 

The assignment in the while loop represents a very common usage. Because such code is widespread, it is important to study this expression until its meaning is immediately clear.





C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net