User Tools

Site Tools


cplusplus:repetition_examples

Control Structures: Repetition examples

The while statement

simple_while.cpp
// This program counts to an upper limit
#include <iostream>
using namespace std;
 
int main()
{
    const int UPPER_LIMIT = 5;
    int num = 1;
 
    while (num <= UPPER_LIMIT)
    {
        cout << num << endl;
        num = num + 1;
    }
 
    return 0;
}

increment operator

increment-example.cpp
// This program counts to an upper limit
#include <iostream>
using namespace std;
 
int main()
{
    int num = 10;
 
    cout << num << endl;
    num++;
 
    return 0;
}

Repetition code patterns

Counter-controlled repetition

counter-controlled-repetition.cpp
// This program counts to an upper limit
#include <iostream>
using namespace std;
 
int main()
{
    const int UPPER_LIMIT = 5;
    int num = 1;                 // initialize the counter
 
    while (num <= UPPER_LIMIT)   // test the count
    {
        cout << num << endl;
        num++;                   // increment the counter
    }
 
    return 0;
}

Sentinel-controlled repetition

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 -1 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;
    }

Input validation

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;
 
    // Fall through loop only if input is valid.
    while (num < LOWER_BOUND || num > UPPER_BOUND)
    {
        cout << "Enter a number between " << LOWER_BOUND
             << " and " << UPPER_BOUND << ": ";
        cin >> num;
    }
 
    cout << "Congratulations, you can follow directions." << endl;
 
    return 0;
}

Running total

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 accumulator
        num++;
    }
 
    cout << "The sum of integers from " << LOWER_BOUND
         << " to " << UPPER_BOUND
         << " is " << sum << endl;
 
    return 0;
}

The for statement

simple-for.cpp
// This program counts to an upper limit using a for structure.
#include <iostream>
using namespace std;
 
int main()
{
    const int UPPER_LIMIT = 5;
    int num;
 
    for (num = 1; num <=UPPER_LIMIT; num++)
    {
        cout << num << endl;
    }
 
    return 0;
}

Nested loops

nested-loop.cpp
// This program does some graphics with nested loops.
#include <iostream>
using namespace std;
 
int main()
{
    const int NUM_ROWS = 5;
    const int NUM_COLUMNS = 3;
 
    for (int row = 1; row <= NUM_ROWS; row++)
    {
        for (int column = 1; column <= NUM_COLUMNS; column++)
        {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

break and continue

simple-break.cpp
// Print out a list of powers of 2.
#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
    const int UPPER_LIMIT = 100;
    int num = 1;    // counter
    char choice;    // used to get user input
 
    while (num <= UPPER_LIMIT)
    {
        cout << "2 to the " << num << " power is " << pow(2, num) << endl;
        cout << "Should I go on? [y/N]: ";
        cin >> choice;
        if (choice == 'N' || choice == 'n')
            break;    // (try replacing with 'continue')
        num++;
    }
 
    cout << "Goodbye!" << endl;
    return 0;
}
cplusplus/repetition_examples.txt · Last modified: 2019/02/14 21:28 by mithat