The basics of programming in c++ for beginners

Nested loops in C++

вложенные циклы C++We all got acquainted with three repetition structures in C ++ programming. It loopsfor, while и do while. If you've been paying attention, attachment structures logical choice if and else in the loops have been applied. Now we consider the nested loops – when the body is one loop (external) hosts another loop (internal). Such nested loops in the external loop may be somewhat.

On its structure, nested loops remind me boxes of different sizes, as in our drawing. These boxes can be easily folded into one another and placed in a large box. So with loops. Program, to get to the nested loop, you must first begin any of the outer loop (open the lid of a large box), run, what is written in the code to the inner loop, and then proceed with the implementation of this loop.

There is one interesting common example, to showcase the work of nested loops. Consider it: using loops, must draw a rectangle of size 5 x 15 of the characters @.

At first it starts loop for in string 6. Since his body nothing is written to the second loop, the program immediately starts executing this nested loop – strings 8 – 11. As a result of its implementation, on the screen are displayed on one line 15 characters @ and it exits. Here work cout  in string 12, the control variable i increased by one and continue the implementation of the main and nested loops. That is, again 15 characters on the screen and move to the line below. So it will be done 5 times, after which the program exits. On the screen we see the, that was in the condition:

вложенные циклы в C

You can slightly complicate the task. Now, let the symbol for printing the user selects pieces, by keyboard input. He chooses the size of the – width and height. Another innovation – the figure should be empty, not filled, as in our previous example. That is, it must consist solely of the contour. Here is our solution:

The result depends on the input, which asks the user. I turned the black square of the issues :)

вложенные циклы в C++

One more example: print a multiplication table.

As a result, we see a multiplication table:

вложенные циклы в C++

вложенные циклы в C++

.

.

.

.

.

.

.

.

.

Figures showing only the beginning and the end of the table.

I hope the lesson has been useful for you. Nested constructs in C++ programming are fairly common. Necessarily consider the tasks, we have prepared. If you have any questions on the topic – we are waiting for them in the comments.

41 thoughts on “Nested loops in C++

    1. I am pleased to see that some kind of praise from you )

      About cin indented code, I would highlight the proposal to introduce a bit of data and data entry itself, that was written all in a solid block 6 strings. About tom, it is necessary to exactly, Of course no one says )

  1. Hello!
    Read the book “C ++ for Dummies” And somehow vaguely in her painted theme of nested loops. I searched the internet and came across your website. Examples are very clear and are available for beginners. Even compared with five other sites, You are the best!
    huge Pasiba! It seems difficult subject, and thanks to all of your article as ABC. Respect!
    I could not help but thank you for such work…

  2. Zdravstvuyte.Ya tried to solve the problem (second)
    #include
    using namespace std;
    int fields,symbol;
    int main()
    {
    cout<<"Kol-vo strok/i="strok;
    cout<<"Kol-vo simbol/j"simbol;
    for(int i=1;i<= stroke;i )
    {
    for(int j=1;j<= symbol;j )
    {
    if ((i!==1 && (strok-i!==1))&&( j==1 || j==2 )&&(j == simbol|| j==(symbol-1)))
    {
    cout<<"*";
    }

    if (i==1||stroke-1!==1)
    {
    cout<<"*";
    }

    }
    cout <KOD.cpp
    1>c:\usersтимурdesktopdocumentsvisual studio 2008projectsvl_chirluvl_chirlukod.cpp(14) : error C2059: syntax error: =
    1>c:\usersтимурdesktopdocumentsvisual studio 2008projectsvl_chirluvl_chirlukod.cpp(15) : error C2143: syntax error: lack of “;” before “{”
    1>c:\usersтимурdesktopdocumentsvisual studio 2008projectsvl_chirluvl_chirlukod.cpp(19) : error C2059: syntax error: =
    1>c:\usersтимурdesktopdocumentsvisual studio 2008projectsvl_chirluvl_chirlukod.cpp(20) : error C2143: syntax error: lack of “;” before “{”
    Prompt in what an error?????

  3. wonderful lesson!! Thank you.
    But, Please help me with a problem 2, which draws a rectangle.
    Do not understand the line
    for (int j = 1; j <= symbInLines – 2; j )
    explain, you are welcome, in detail.

    1. symbInLines a variable, which indicates what the width of the square.
      For example symbInLines = 50. Description j = 1; j <= symbInLines – 2 He says that the cycle should run 48 time withdrawing 48 gaps between two characters border square.

  4. Can anyone be interested in While

    #include
    using namespace std;
    int main()
    {
    int a=1,b=0,c=1,d=0;
    cout<<b;
    cout<<d;
    while(a<=b)
    {
    c=1;
    while(c<=d)
    {
    if (a==1||a==b)
    {
    cout<<"0";
    }
    else if (c==1||c==d)
    {
    cout<<"0";
    }
    else
    {
    cout<<"1";
    }
    c++;
    }
    a++;
    cout<<endl;
    }

    return 0;
    }


  5. for (int i = 0; i < height; i++)
    {
    for (int j = 0; j < width; j++)
    {
    if ((i == 0 || i == height - 1) || (j == 0 && i != 0 && i != height - 1) || (j == width - 1 && i != 0 && i != height - 1 ))
    {
    cout << symbol;
    }
    else
    {
    cout << ' ';
    }
    }
    cout << endl;
    }

    good lessons)

Leave a Reply

Your email address will not be published. Required fields are marked *