Chapter 5 Iteration
  1. while
    while (expression)
       statement;
    
    1. The expression is evaluated before each repetition of the loop and so long as it is nonzero the statement is executed.
    2. Examples of while statement.
        i=1;
        while(i<4) {
           cout << i << endl;
           i++;
        }
      
        i=1;
        while(i++ < 4)
           cout << i << endl;
      
        i=1;
        while(++i < 4)
          cout << i << endl;
      



  2. do .. while
    do
     statement;
    while (expression);
    
    1. Expression is evaluated after the the statement is executed. If the expression is nonzero the statement is repeated.
    2. do .. while examples.
        i=1;
        do {
            cout << i << endl;
            i++;
        } while (i < 4);
      
        i=5;
        do {
            cout << i << endl;
            i++;
        } while (i < 4);
      

  3. for
    for(expression1;expresssion2;expression3)
       statement;
    
     equivalent to
    
      expression1;
      while(expression2) {
           statement;
           expression3;
      }
    
    
    1. expression1
      initializes the loop. executed only once.
    2. expression2
      same as while test. It checks the termination of the loop before statement is executed.
    3. expression3
      is evaluated after statement is executed.
    4. Sample for statement:
         for(i=1;i<4;i++) cout << i << endl;
      
  4. comma operator ,
    evaluates the expression to the left then removes the result from the stack. The primary use the the comma (sequential operator) is in for statements.
       for(i=1,j=4;i<4;i++,j--) cout << i << " " << j << endl;
    

  5. break The break statement may be used to terminate switch, while, do..while and for loops.
       while (fabs(a-rp(x,r)) > 0.001) {
             if(k++ > 40) break;
             x = x-(rp(x,r)-a)/(r*rp(x,r-1));
             cout << x << endl;
       }
    
  6. continue The continue statement is only used with loops. It skips to evaluate the loop again.
    1. While loop
        i=1;
        while(i++ < 10) {
            if(i%2) continue;
            cout << i << endl;
        }
      
    2. For Loop.
        for(i=1;i<10;i++) {
            if(i%2) continue;
            cout << i << endl;
        }
      
  7. goto
    The got statement transfers control unconditionally to another statement. The format of the goto is :
       goto label;
      
    
       if(i == 2) goto ok;
       cout << i << endl;
     ok: i=2;
    

    The use of go to is frowned upon in any structured program. However, the statement may prove useful to break out of nested loops.

  8. . Examples.
    1. The lock problem.
      #include <iostream>
      using namespace std;
      /*
        A safe has five locks 1,2,3,4 and 5, all of which must be unlocked
        for the safe to open. The keys to the locks are distributed among
        five employees in the following manner:
          Mr. Alden has keys for locks 1 and 3
          Ms  Baker has keys for locks 1 and 4
          Ms. Cable has keys for locks 2 and 4
          Ms  Deere has keys for locks 3 and 5
          Mr. Evans has keys for locks 1 and 5
      */
      
      int here(char *);    // Function prototype
      
      void main()
      { 
         int Lock1,Lock2,Lock3,Lock4,Lock5,OPEN;
         int test = 0;
         char tryagain;
      
      
         Lock1=020;   //  010 000 or 16
         Lock2=010;   //  001 000 or 8
         Lock3=004;   //  000 100 or 4
         Lock4=002;   //  000 010 or 2
         Lock5=001;   //  000 001 or 1
         OPEN=  Lock1 | Lock2 | Lock3 | Lock4 | Lock5;   //  011 111 or 31
         
         do {
      	cout  << " Who is Here? " << endl;
      	if(here("Mr. Alden"))  test |= Lock1 | Lock3;
      	if(here("Ms. Baker"))  test |= Lock1 | Lock4;
      	if(here("Ms. Cable"))  test |= Lock2 | Lock4;
      	if(here("Ms. Deere"))  test |= Lock3 | Lock5;
      	if(here("Mr. Evans"))  test |= Lock1 | Lock5;
        
      	if(test == OPEN) cout << "Safe can be Opened" <<endl;
      	else             cout << "Safe Cannot be Opened" << endl;
              test=0;
      	cout << "Try Again? ";
      	cin >> tryagain;
         } while (tryagain == 'y' || tryagain == 'Y');
      }
       
      /*
       The here function reurns 0 (false) or 1 (true)
       based on the question passed.
      */
      int here(char *person)
      {
      	char response;
      
      	cout << "Is " << person << " here? ";
      	cin >> response;
      	return ( response == 'y' || response == 'Y');
      }
      

       Who is Here?
      Is Mr. Alden here? y
      Is Ms. Baker here? y
      Is Ms. Cable here? y
      Is Ms. Deere here? n
      Is Mr. Evans here? n
      Safe Cannot be Opened
      Try Again? y
       Who is Here?
      Is Mr. Alden here? y
      Is Ms. Baker here? n
      Is Ms. Cable here? y
      Is Ms. Deere here? n
      Is Mr. Evans here? y
      Safe can be Opened
      Try Again? n
      Press any key to continue
      


    2. From prior problem print all the combinations that can open the safe.
      #include <iostream>
      #include <iomanip>
      using namespace std;
      /*
        A safe has five locks 1,2,3,4 and 5, all of which must be unlocked
        for the safe to open. The keys to the locks are distributed among
        five employees in the following manner:
          Mr. Alden has keys for locks 1 and 3
          Ms  Baker has keys for locks 1 and 4
          Ms. Cable has keys for locks 2 and 4
          Ms  Deere has keys for locks 3 and 5
          Mr. Evans has keys for locks 1 and 5
      */
      
      
      
      void main()
      { 
         int Lock1,Lock2,Lock3,Lock4,Lock5,OPEN;
         int test;
      
         Lock1=020;   //  010 000 or 16
         Lock2=010;   //  001 000 or 8
         Lock3=004;   //  000 100 or 4
         Lock4=002;   //  000 010 or 2
         Lock5=001;   //  000 001 or 1
         OPEN=  Lock1 | Lock2 | Lock3 | Lock4 | Lock5;   //  011 111 or 31
      
         int
         alden=020,  // Alden Pattern
         baker=010,  // Baker pattern
         cable=004,  // Cable pattern 
         deere=002,  // Deere pattern
         evans=001;  // Evans pattern
      
         int pattern,count=1;
      
         cout << "The following sets of Employees can open the safe" << endl << endl;
         cout << "Mr. Alden |"
              << "Ms. Baker |" 
      	<< "Ms. Cable |" 
      	<< "Ms. Deere |" 
      	<< "Mr. Evans |" << endl;
         
         char space[] = "______________________________________________________";
         char here[] ="    xxx   |";
         char gone[] ="          |";
         
         cout << space << endl;
         for(pattern=1;pattern<32;pattern++) {   // Genate patterns
             test=0;                             // Pattern to zero
      	   if(pattern&alden)  test |= Lock1 | Lock3;
             if(pattern&baker)  test |= Lock1 | Lock4;
             if(pattern&cable)  test |= Lock2 | Lock4;
             if(pattern&deere)  test |= Lock3 | Lock5;
      	   if(pattern&evans)  test |= Lock1 | Lock5;
      	   if( test == OPEN) {
      		   cout << (pattern&alden ?  here : gone);
      		   cout << (pattern&baker ?  here : gone);
      		   cout << (pattern&cable ?  here : gone);
              	   cout << (pattern&deere ?  here : gone);
      		   cout << (pattern&evans ?  here : gone);
      		   cout <<endl<< space << endl;
      	   }
         }
         
      }
      
      

      The following sets of Employees can open the safe
      
      Mr. Alden |Ms. Baker |Ms. Cable |Ms. Deere |Mr. Evans |
      ______________________________________________________
                |          |    xxx   |    xxx   |    xxx   |
      ______________________________________________________
                |    xxx   |    xxx   |    xxx   |          |
      ______________________________________________________
                |    xxx   |    xxx   |    xxx   |    xxx   |
      ______________________________________________________
          xxx   |          |    xxx   |          |    xxx   |
      ______________________________________________________
          xxx   |          |    xxx   |    xxx   |          |
      ______________________________________________________
          xxx   |          |    xxx   |    xxx   |    xxx   |
      ______________________________________________________
          xxx   |    xxx   |    xxx   |          |    xxx   |
      ______________________________________________________
          xxx   |    xxx   |    xxx   |    xxx   |          |
      ______________________________________________________
          xxx   |    xxx   |    xxx   |    xxx   |    xxx   |
      ______________________________________________________
      Press any key to continue
      



    3. The problem from Vos Savant.
      #include <iostream.h>
      #include <stdlib.h>
      #define MAX 1000000
      void simulate(int);
      int pick(int, int);
      void main()
      {
      /*
         Scenario 1
         a coin is under one of three cups
         you select one.  The program selects and
         turnover one where the coin isn't.
         You don't switch.
      */
       simulate(1); 
      /*
         Scenario 2
         a coin is under one of three cups
         you select one.  The program selects and
         turnover one where the coin isn't.
         You always switch.
      */
       simulate(2);
      /*
         Scenario 3
         a coin is under one of three cups
         you select one.  The program selects and
         turnover one where the coin isn't.
         New person randomly selects one of the remaining cups.
      */
       simulate(3);
      
      }
      void simulate(int policy)
      { 
        int plays,right,wrong,isat,guess,turnover,other;
        int i;
      
      
         right=wrong=plays=0;
         while(plays++<MAX) {
               isat=rand()%3+1;    // where  coin is at 1, 2, or 3
               guess=rand()%3+1;   // random guess
               turnover=pick(isat,guess);
               other=pick(guess,turnover);
               switch(policy) {
                  case 1 :  break;
                  case 2 :  guess=other; break;
                  case 3 :  if(rand()%2) guess=other; break;
               }
               if(guess==isat) right++;
               else wrong++;
         }
         cout << "Scenario " << policy;
         cout << "\n right ="  << right;
         cout << "\n wrong =" << wrong << endl;
      }
      //
      //  return a number 1 2 3 that is not x or y
      //
      int pick(int x, int y)
      {
        int value;
      
        if( x != 1 && y != 1 ) value=1;
        else if( x!=2 && y != 2) value=2;
             else value=3;
        return(value);
      }
      
      $ a.out
      Scenario 1
       right =333297
       wrong =666703
      Scenario 2
       right =666448
       wrong =333552
      Scenario 3
       right =500055
       wrong =499945