for Robot Artificial Inteligence

5.Loops

|

Example 1

#include <iostream>

using namespace std;
/* loops - break and continue */

main()
{
    /*
        1 2 3 4 5  6  7  8  9  10
        2 4 6 8 10 12 14 16 18 20
        3 6 9 12 15 ....
        4 ...
        5 ...
    */
/*
    for (int i = 1; i <= 10; i++) //amount of rows, length of column
    {

        if (i == 5)
            continue; //EVERYTHING AFTER continue instruction WONT BE executed BUT LOOP WON'T END BECAUSE OF IT

        if (i == 5)
            break; //EVERYTHING after break WON't be executed AND we are LEAVING the ACTUAL LOOP


        for (int j = 1; j <= 10; j++) //amount of columns, length of row
        {
            if (j == 5)
                continue;
            cout.width(4);
            cout << i * j;
        }
        cout << endl;
    }
*/


    for (int i = 1, j = 1; i <= 10; i++)
    {
        cout.width(4); // 4 spaces
        cout << i * j;

        if (i == 10)
        {
            j++;
            i = 0;
            cout << endl;
        }

        if (j == 10 + 1)
            break;
    }
}


Example 2

#include <iostream>

using namespace std;

main()
{
    const int SIZEOFARRAY = 10;
    int i = 0;
    int array[SIZEOFARRAY];

    while(i < SIZEOFARRAY) //i = 0
    {
        array[i] = 10 * i;
        cout << array[i++] << endl;
    }

}

Example 3

#include <iostream>
#include <cstdlib>

using namespace std;

main ()
{
     int pin, checkPin;
     int counter = 5;

     cout << "Input your pin number for the first time: ";
     cin >> pin;

     system("cls");

     do
     {
         cout << "Input your pin(" << counter << " tries left): ";
         cin >> checkPin;

         system("cls");
         counter--;

     } while ((checkPin != pin) && (counter > 0));

     if (checkPin == pin)
     {
        cout << "Correct pin, welcome !";
     }
     else
        cout << "You have entered a wrong PIN code for 5 times !";

}


Example 4

#include <iostream>

using namespace std;

main ()
{
    char coordinates[3][3];
    char value = '1';

    for (int i = 0; i < 3; i++)
    {
        cout << endl << endl;
        for (int j = 0; j < 3; j++)
        {
            coordinates[i][j] = value;
            value++;

            cout.width(5);
            cout << coordinates[i][j] << " ";

        }
        cout << endl << endl;
    }

}

Example 5

#include <iostream>
#include <cstdlib>

using namespace std;

main ()
{
    char coordinates[3][3];
    int startingValue = '1';
    int player = 2;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            coordinates[i][j] = startingValue;
            startingValue++;
        }
    }

    while (true)
    {
        for (int i = 0; i < 3; i++)
        {
            cout << endl << endl;
            for (int j = 0; j < 3; j++)
            {
                cout.width(5);
                cout << coordinates[i][j] << " ";
            }
            cout << endl << endl;
        }

        int xCoordinate, yCoordinate;

        if (player % 2 == 0)
        {
            cout << "Input x and y coordinates of a cell to put X (7 is on x:1 y:3)" << endl;

            cout << "x: ";
            cin >> xCoordinate;

            cout << "y: ";
            cin >> yCoordinate;

            coordinates[yCoordinate-1][xCoordinate-1] = 'X';
            player = 1;
            system("cls");
        }
        else
        {

            cout << "Input x and y coordinates of a cell to put O (7 is on x:1 y:3)" << endl;

            cout << "x: ";
            cin >> xCoordinate;

            cout << "y: ";
            cin >> yCoordinate;

            coordinates[yCoordinate-1][xCoordinate-1] = 'O';
            player = 2;
            system("cls");

        }
    }
}


Example 6

#include <iostream>
#include <cstdlib>

using namespace std;

main ()
{
    char coordinates[3][3];
    int startingValue = '1';
    int player = 2;
    bool moveAccepted;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            coordinates[i][j] = startingValue;
            startingValue++;
        }
    }

    while (true)
    {

        int xCoordinate, yCoordinate;


            do
            {
                for (int i = 0; i < 3; i++)
                {
                    cout << endl << endl;
                    for (int j = 0; j < 3; j++)
                    {
                        cout.width(5);
                        cout << coordinates[i][j] << " ";
                    }
                    cout << endl << endl;
                }

                if (player % 2 == 0)
                {
                    cout << "Input x and y coordinates (values from 1 to 3) of a cell to put X (7 is on x:1 y:3)" << endl;
                }
                else
                {
                    cout << "Input x and y coordinates (values from 1 to 3) of a cell to put O (7 is on x:1 y:3)" << endl;
                }

                do
                {
                    cout << "x: ";
                    cin >> xCoordinate;

                } while(xCoordinate <= 0 || xCoordinate > 3);


                do
                {
                    cout << "y: ";
                    cin >> yCoordinate;

                } while(yCoordinate <= 0 || yCoordinate > 3);

                if (coordinates[yCoordinate-1][xCoordinate-1] != 'X' && coordinates[yCoordinate-1][xCoordinate-1] != 'O')
                {
                    if (player % 2 == 0)
                    {
                        coordinates[yCoordinate-1][xCoordinate-1] = 'X';
                        player = 1;
                    }
                    else
                    {
                        coordinates[yCoordinate-1][xCoordinate-1] = 'O';
                        player = 2;
                    }
                    moveAccepted = true;
                    system ("cls");
                }
                else
                {
                    moveAccepted = false;
                    system ("cls");
                }
            } while(moveAccepted == false);



        }
}


Example 7

#include <iostream>
#include <cstdlib>

using namespace std;

main ()
{
    int chosenNumbersSum = 0;
    char choice;

    for (int i = 1; ; i++)
    {

        cout << "The current sum is: " << chosenNumbersSum << endl << endl;
        cout << "Do you want to add " << i << " ?" << endl;

        cout << "Y - yes / N - no / Anything else - end : ";
        cin >> choice;

        if (choice == 'Y' || choice == 'y')
        {
            chosenNumbersSum += i;
        }
        else if (choice == 'N' || choice == 'n')
        {
            system ("cls");
            continue;
        }
        else
        {
            system ("cls");
            break;
        }
        system ("cls");
    }

    cout << "Good bye !";

}

Comments