I was trying to find the smallest common divisor for the numbers from 1 to 20.
I tried the following code. My problem was how to get back to the beggining of the for loop after the if test has found the number is not divisible.

But then i added the last statement i = 1; It seems to have worked because it gave me the write answer.

My question, how did the last statement tell the loop to restart again? Are there other technics of restarting a loop?

The code is used is below.

public class SmallDivisible {
static int numbi = 2520;


public static void main (String[] args){


for (int i = 1; i < 21; i++){
if (numbi % i == 0){
continue;
} else  {


numbi++;
i = 1;
}


}
System.out.println(numbi);


}



}

Dani AI

Generated

A quick explanation of what happened, why it is fragile, and a better way to solve the 1..20 problem.

A Java for loop runs this cycle: body -> update -> condition. Assigning the loop counter inside the body changes the value that the loop’s update expression will see. That is why resetting the counter in the body made the checks appear to “restart” — the update still runs after the body, so the next iteration begins from whatever value the update produced. That trick can work, but it makes the code hard to read and easy to break. was right to warn about infinite-loop risk when the counter is manipulated from inside the body.

Two safer approaches:

  • Use an explicit outer loop that increments the candidate number and an inner loop that checks all divisors; when the inner loop fails, break/continue to the outer loop. This keeps loop control obvious and avoids changing loop-control variables mid-body.
  • For correctness and speed, compute the least common multiple via gcd (Euclid’s algorithm) instead of repeatedly testing candidates. That is what people solving Project Euler Problem 5 do.

A compact, reliable Java implementation (gcd + lcm) — avoids messing with loop counters and is fast:

static long gcd(long a, long b) {
    while (b != 0) {
        long t = b;
        b = a % b;
        a = t;
    }
    return a;
}

static long lcm(long a, long b) {
    return (a / gcd(a, b)) * b;
}

long result = 1;
for (int n = 1; n <= 20; n++) {
    result = lcm(result, n);
}
System.out.println(result);

Notes and cautions:

  • Divide before multiplying in lcm to reduce overflow risk; use long or BigInteger if ranges grow.
  • For theory and background see the explanations of the Least common multiple and the Euclidean algorithm.

This keeps logic clear (as suggested to change strategy) and avoids brittle in-body counter tweaks.

Recommended Answers

All 7 Replies

Your loop runs from i=1;i<21 ...So if you revert the value of i to something below 21, then it will then start from that number. I don't see the confusion :D

The else statement assigns a value of (1) to the variable (i) in the for loop. As a result, your increment statement in the for loop(i++) is useless because you assign an integer value of (1) to the variable (i) all the time, this is an infinite loop.

l

The else statement is always excecuted because any integer devided by 1 has no remainder. The else statement then assigns a value of (1) to the variable (i) in the for loop. As a result, your increment statement in the for loop(i++) is useless because you assign an integer value of (1) to the variable (i) all the time, this is an infinite loop.

Wrong. The else statement is executed when there is a remainder. You were right about the infinite loop though(aslong as the number returns a remainder)

@op, I think this is what you were trying to do:

public class SmallDivisible {
static int numbi = 2520;

public static void main (String[] args){

for (int i = 1; i < 21; i++){
if (numbi % i == 0){
	System.out.println(numbi + " divided by "+i+" has no remainder");
continue;
} else {
	System.out.println(numbi + " divided by "+i+" has a remainder of " +numbi%i);
	numbi++;
i++;
}

}
System.out.println(numbi);

}
}

But i'm unsure why you would want to increment the number you are testing?

Wrong. The else statement is executed when there is a remainder. You were right about the infinite loop though(aslong as the number returns a remainder)

@op, I think this is what you were trying to do:

public class SmallDivisible {
static int numbi = 2520;

public static void main (String[] args){

for (int i = 1; i < 21; i++){
if (numbi % i == 0){
	System.out.println(numbi + " divided by "+i+" has no remainder");
continue;
} else {
	System.out.println(numbi + " divided by "+i+" has a remainder of " +numbi%i);
	numbi++;
i++;
}

}
System.out.println(numbi);

}
}

But i'm unsure why you would want to increment the number you are testing?

I know I misread the code.. I edited my post.. Thats what I meant. Thank you.

Your loop runs from i=1;i<21 ...So if you revert the value of i to something below 21, then it will then start from that number. I don't see the confusion :D

Thank you Akill10. Is this approach something one can use in a loop regularly?

Wrong. The else statement is executed when there is a remainder. You were right about the infinite loop though(aslong as the number returns a remainder)

@op, I think this is what you were trying to do:

public class SmallDivisible {
static int numbi = 2520;

public static void main (String[] args){

for (int i = 1; i < 21; i++){
if (numbi % i == 0){
	System.out.println(numbi + " divided by "+i+" has no remainder");
continue;
} else {
	System.out.println(numbi + " divided by "+i+" has a remainder of " +numbi%i);
	numbi++;
i++;
}

}
System.out.println(numbi);

}
}

But i'm unsure why you would want to increment the number you are testing?

Akill10:

thanks again for the help. I was trying to find a number (the one being tested) which can divide all the numbers from 1 to 20 without a reminder. For that i thought i have to keep increamenting the test number until i find such a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.