Looking at this code?
import java.util.Scanner;
public class CountingMachineRevisited {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int from, to, by;
System.out.print("Count from: ");
from = scan.nextInt();
System.out.println("Count to: ");
to = scan.nextInt();
System.out.println("Count by: ");
by = scan.nextInt();
for (int i = from; i <= to; i+=by) {
System.out.println(i);
}
}
}
This code works the way I want it to, but if i change the termination condition of the for loop to i == to, it doesnt work.
for (int i = from; i == to; i+=by) {
System.out.println(i);
}
I would understand this is all the int's defaulted to 0 making the termination the same as the initial so the for loop would stop, but if I am initializing new values before the loop starts why doesnt it work?
igets the valuefrom, it is not equal totoso the loop is never executed. Try your program withfromequal totoand you will see that it will enter the for loop just once.i+bymight just jump over the value ofto