V
V
Vladimir Yurchenkov2021-11-17 10:32:49
C++ / C#
Vladimir Yurchenkov, 2021-11-17 10:32:49

Why is the nested loop iteration skipped?

In continuation of the question .

Guys for the first time in general I write something similar.
The general task is to build a query string like this: I generate these strings, specifying different parameters, for example - the first parameter is the number of data elements (which always starts from 01 and up to 999) - the second parameter is a range of numbers, for example from 1 to 5 By a simple condition in the loop, with the first parameter, I easily generate strings:

text data01 text 1 2 3 4 5





text data01
text data02
text data03
...
text data10
text data11
text data12


I use the second parameter to calculate the elements of the array and alternate them.
That is, if you separately display them, then everything is buzzing. The alternation is (the first element is shifted to the end, with each iteration:

1 2 3 4 5 
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4


My task is to cross these two cycles and combine the lines. Here is my code:

int start = 1, last = 5;
int[] arr = Enumerable.Range(start, last - start + 1).ToArray();
int count = arr.Count();
int n = 5; // количество data


for (int j = 1; j < n; j++)
{
textBox.AppendText("text data0" + j);
    for (int i = 0; i < count; i++) {
        textBox.AppendText($"{ arr[(i + j) % count]}");
}
}


That's how I get what I need. But the first iteration goes wrong (what starts with 0 skips the iteration). If I add a condition for 0, this iteration is not displayed, but the 2nd loop, which outputs the array alternation, starts from the 2nd iteration.

What I get:

text data00     1 2 3 4 5
text data01     2 3 4 5 1
text data02     3 4 5 1 2
text data03     4 5 1 2 3
text data04     5 1 2 3 4
text data05     1 2 3 4 5


And I want:

text data01     1 2 3 4 5
text data02     2 3 4 5 1
text data03     3 4 5 1 2
text data04     4 5 1 2 3
text data05     5 1 2 3 4


How to skip 1 loop iteration, where 0 gets row 1 2 3 4 5
but at the same time the 2nd iteration of the loop, start also from row 1 2 3 4 5, and not from 2 3 4 5 1?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yurchenkov, 2021-11-18
@EPIDEMIASH

$"name{arr[(i + j - 1) % count]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question