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


}



}

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.