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);


}



}

Edited 3 Years Ago by happygeek: fixed formatting

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

Edited 5 Years Ago by Akill10: n/a

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.

Edited 3 Years Ago by happygeek: fixed formatting

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?

Edited 5 Years Ago by Akill10: n/a

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.

Edited 5 Years Ago by JKP(): n/a

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.

This article has been dead for over six months. Start a new discussion instead.