User Tools

Site Tools


cplusplus:repetition_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
cplusplus:repetition_examples [2013/03/01 01:10] mithatcplusplus:repetition_examples [2019/02/14 21:27] – [Counter-controlled repetition] mithat
Line 1: Line 1:
 ====== Control Structures: Repetition examples ====== ====== Control Structures: Repetition examples ======
  
-===== while =====+===== The while statement =====
  
 <file c++ simple_while.cpp>// This program counts to an upper limit <file c++ simple_while.cpp>// This program counts to an upper limit
Line 34: Line 34:
     cout << num << endl;     cout << num << endl;
     num++;     num++;
-    cout << num << endl; 
  
     return 0;     return 0;
Line 40: Line 39:
 </file> </file>
  
-===== Sentinel-controlled repetition ===== 
  
-<file c++ simple-sentinel.cpp>// This program calculates the squares of integers. The user +===== Repetition code patterns ===== 
-// enters -999 when finished.+ 
 +==== Counter-controlled repetition ==== 
 +<file c++ counter-controlled-repetition.cpp>// This program counts to an upper limit
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 49: Line 49:
 int main() int main()
 { {
-    int num = 1;+    const int UPPER_LIMIT = 5; 
 +    int num = 1;                 // initialize the counter
  
-    cout << "Enter an integer and I will tell you its square. " << endl; +    while (num <= UPPER_LIMIT)   // test the count 
-    cout << "Enter -999 to quit: ";+    { 
 +        cout << num << endl; 
 +        num++;                   // increment the counter 
 +    } 
 + 
 +    return 0; 
 +}</file> 
 + 
 +==== Sentinel-controlled repetition ==== 
 + 
 +<file c++ simple-sentinel.cpp>    // This program determines whether test scores are passing marks. 
 +    #include <iostream> 
 +    using namespace std; 
 + 
 +    int main() 
 +    { 
 +        const unsigned int PASSING_SCORE = 60; 
 +        int score; 
 + 
 +        cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; 
 +        cout << "Enter -to quit: "; 
 +        cin >> score; 
 + 
 +        while (score != -1) 
 +        { 
 +            if (score >= PASSING_SCORE) 
 +            { 
 +                cout <<  score << ": PASS" << endl; 
 +            } 
 +            else 
 +            { 
 +                cout <<  score << ": FAIL" << endl; 
 +            } 
 + 
 +            cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; 
 +            cout << "Enter -1 to quit: "; 
 +            cin >> score; 
 +        } 
 + 
 +        return 0; 
 +    } 
 + 
 +</file> 
 + 
 +==== Input validation ==== 
 +<file c++ simple-input-validation.cpp>// Simple example of input validation. 
 +#include <iostream> 
 +using namespace std; 
 + 
 +int main() 
 +
 +    const int LOWER_BOUND = 0; 
 +    const int UPPER_BOUND = 100; 
 +    int num; 
 + 
 +    cout << "Enter a number between " << LOWER_BOUND  
 +         << " and " << UPPER_BOUND << ": ";
     cin >> num;     cin >> num;
  
-    while (num != -999)+    // Fall through loop only if input is valid. 
 +    while (num < LOWER_BOUND || num > UPPER_BOUND)
     {     {
-        cout << num <<" squared is " << num * num << "." << endl +        cout << "Enter a number between " << LOWER_BOUND 
-             << endl; +             << " and " << UPPER_BOUND << ": ";
-        cout << "Enter an integer and I will tell you its square. " << endl; +
-        cout << "Enter -999 to quit: ";+
         cin >> num;         cin >> num;
     }     }
 +
 +    cout << "Congratulations, you can follow directions." << endl;
  
     return 0;     return 0;
-}</file>+} 
 + 
 +</file> 
 + 
 +==== Running total ==== 
 +<file c++ simple-running-total.cpp>// Simple running total example. 
 +// Total all the integers between a lower and an upper bound. 
 +#include <iostream> 
 +using namespace std; 
 + 
 +int main() 
 +
 +    const int LOWER_BOUND = 0; 
 +    const int UPPER_BOUND = 10; 
 + 
 +    int num = LOWER_BOUND;  // counter 
 +    int sum = 0;            // accumulator 
 + 
 +    while (num <= UPPER_BOUND) 
 +    { 
 +        sum += num;         // add the new value to the sum 
 +        num++; 
 +    } 
 + 
 +    cout << "The sum of integers from " << LOWER_BOUND 
 +         << " to " << UPPER_BOUND 
 +         << " is " << sum << endl; 
 + 
 +    return 0; 
 +
 + 
 +</file>
  
-===== for ===== +===== The for statement ===== 
-<file c++ simple-for.cpp>// This program counts to an upper limit +<file c++ simple-for.cpp>// This program counts to an upper limit using a for structure.
-// using a for structure.+
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 128: Line 216:
         cin >> choice;         cin >> choice;
         if (choice == 'N' || choice == 'n')         if (choice == 'N' || choice == 'n')
-            break;    // (try replacing with continue)+            break;    // (try replacing with 'continue')
         num++;         num++;
     }     }
cplusplus/repetition_examples.txt · Last modified: 2019/02/14 21:28 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki