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.

40 thoughts on “Nested loops in C++

    1. It's hard to say what you mean by basic rules?
      Fundamental rules:
      – Cycles should be invested according to the principle “matrjoşki”: each inner loop must be completely ** ** placed inside covering;
      – Never change the setting (variable) the outer loop in an internal;
      – You never get out of the nested loops operator goto;

  1. Tell, This solution is an example of your lesson with the square of the symbols, equivalent to your decision is given in lesson? Or your version of all the same has the advantage? If yes then what? Thank you.

    1. Decision any problem can be written in many, often many tens of, different ways.

      The criterion for the correctness of the program is only result its implementation – run and make sure.

      1. How do: the user enters a radius and a circle drawn with a radius
        P.s. I apologize if there are errors( I was a schoolboy:) )

    2. An identical solution with a choice:

      #include
      using namespace std;

      int main()
      {
      int lines, columns;
      char sym;

      cout <> sym;
      cout <> lines;
      cout <> columns;
      for (int i = 1; i <= lines; i )
      {
      for (int j = 1; j <= columns; j )
      {
      if (j == 1 || i == 1)
      {
      cout << sym;
      continue;
      }
      else
      {
      if (i == lines || j == columns)
      {
      cout << sym;
      if (j == columns)
      {
      break;
      }
      }
      else
      {
      cout << " ";
      }
      }
      }
      cout << endl;
      }

      return 0;
      }

  2. to Soldier:
    “draw a circle” – it is the task of the field graphics, it does not have any relation to the C ++ programming language, and 100% determined by, What graphic libraries (frameworks: MFC, Qt, GTK, wxWidgets и др.) you use + which operating system.

      1. “a circle” must not “draw” in C++.
        “a circle” шt is possible “draw” Only the functions of various graphic libraries … that can be called, including, and from C ++ code.
        But directly to C ++ it has nothing to do.

        P.S. Pass it on to a friend.

  3. who knows how to make it so that, in the task with an empty square, the sides were double?

  4. Probably so

    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    system(“cls”);
    double sredn = 0;
    int exit = 0, a = 0, b = 0;
    cout << "Введите длину и высоту сторон: " <> a;
    cin >> b;
    for (int i=0; i < a;i )
    {
    for (int j=0 ; j < b; j )
    {
    if ( j == 0 || j==a-1 || j == 1 || j == a – 2)
    cout << "q";
    else if (i == 0 || i == b – 1 || i == 1 || i == b – 2)
    cout << "q";
    else cout << " ";

    }
    cout << endl;
    }
    return 0;
    }

Leave a Reply

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