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 @.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { for (int i = 0; i < 5; i++) // этот цикл отвечает за кол-во строк { for (int j = 0; j < 15; j++) // этот - за кол-во символов в каждой строке { cout << '@'; } cout << endl; // переход на строку ниже } return 0; } |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); int lines = 0; int symbInLines = 0; char symbol = 0; cout << "Введите символ: "; cin >> symbol; cout << "Количество строк: "; cin >> lines; cout << "Количество символов в строке: "; cin >> symbInLines; cout << endl; for (int i = 1; i <= lines; i++) { if (i == 1 || i == lines) // 1-я и последняя строка { for (int j = 1; j <= symbInLines; j++) { cout << symbol; // сплошная из символов } } else // все строки между первой и заключительной { cout << symbol; // показать один символ for (int j = 1; j <= symbInLines - 2; j++) { cout << ' '; // пробелы в строке (symbInLines - 2) раз } cout << symbol; // показать ещё один символ } cout << endl; // новая строка } cout << endl; return 0; } |
The result depends on the input, which asks the user. I turned the black square of the issues :)
One more example: print a multiplication table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { for (int f = 2; f <= 9; f++) { for (int s = 1; s <= 9; s++) { cout << f << " * " << s << " = " << f * s; cout << endl; } cout << endl; } cout << endl; return 0; } |
As a result, we see a multiplication table:
.
.
.
.
.
.
.
.
.
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.
sorry i'm a beginner and for me the question was, and how it differs from the usual for
nothing more than. you can just write code differently
you want to use for you know
Less weight than while
what kind of clown wrote this, what kind of clown wrote this, what kind of clown wrote this, what kind of clown wrote this
I understand how the drawing turns out.
Here in the examples in the for loops there is no indentation, hard to figure out. Perhaps, used to write like this? I advise beginners to pay attention to this, look elsewhere for a topic. Thanks anyway for the lesson., I didn't know, how to display such frames in the console.
(below example is relevant, if indents persist. I do not know, what is the formatting in the comments)
for (size_t i = 0; i < 5; i ) {
for (size_t j = 0; j < 15; j ) {
cout << '@';
}
cout << endl;
}
No, not preserved. Generally, use Tab, when you write code.
Good day , request : give a more detailed and complete explanation on the construction of a contour rectangle line by line. Especially with 29 on strings 36. When the lines are filled, everything is clear ,no problem. And here it’s more difficult. I don’t understand the train of thought .
If not difficult !
Thank you in advance.
Hello everyone! I would like to support you by asking for a step-by-step explanation of the isosceles triangle code , filled around the perimeter ! It's also unclear to me , why is the code one-sided regarding spaces.
Ago , who will help – a sea of gratitude and positivity!