Home » Java » java – For loop adding an int to an int to get end result-Exceptionshub

java – For loop adding an int to an int to get end result-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

so I have three ints, a start int, end int, distance int, I want to start with the start int add the distance to the end int to get a result, then with the next loop, start with the end result, add the distance and get another end result.
I have tried:

int start
int end = 0;
int distance = 5;
for(start = 0; distance > 0; end ++)
{
  System.out.println(end);
}
How to&Answers:

In Java the Syntax of a for loop is:

for (initialization; termination; increment) 
{
    statement(s) 
}

In your piece of code, it should be:

int start;
int end = 0;
int distance = 5;
for(start = 0; distance > 0; start ++)
{
    System.out.println(end);
    end ++; 
    distance--;
}