Answer the question
In order to leave comments, you need to log in
Why, when resetting the counter in the condition inside the loop, the counter immediately increases by +1, skipping the iteration?
int arr[] = {2, 5, 65, 23, 47, -12, -35, -72, -14, 0};
int min = arr[0];
int max = arr[1];
for (int i = 0; i < arr.length - 1; i++) {
System.out.println("i равно: " + i);
if (arr[i] > arr[i+1]) {
System.out.println("Пара для смены: " + arr[i] + " " + arr[i+1]);
System.out.println("***************");
min = arr[i+1];
max = arr[i];
arr[i+1] = max;
arr[i] = min;
i = -1;//почему счётчик при следующей
//итерации сразу же увеличивается?
continue;
}
else System.out.println("Пара без смены: " + arr[i] + " " + arr[i+1]);
}
System.out.println("***************");
for (int j = 0; j < arr.length; j++)
System.out.println("Элемент [" + j + "]: " + arr[j]);
Answer the question
In order to leave comments, you need to log in
First, always use code block highlighting in if/for even if there is only one statement, as this will avoid potential errors and will be more readable. And actually the answer to the question, continue simply interrupts the current iteration, and even if it is interrupted by it, i++ will always be executed after the execution of the entire loop body. Therefore, to reset to zero, you will have to write not 0, but -1.
JavaRush ?)
Continue skips 1 iteration. The counter is incremented.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question