Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if for loop iteration is the last one [closed]

Tags:

java

I have following loop

for(int i=0;i<1000;i++) { }

how do i find out if current iteration is the last iteration in java. thanks. Value I am getting dynamically.

like image 252
user30080 Avatar asked Jan 31 '26 18:01

user30080


2 Answers

As per your question if your comparison value is dynamic

int myval = 1000;

    for(int i=0;i<myval;i++) { 

        if(i == myval -1){
            // last work
        }

    }
like image 136
someone Avatar answered Feb 02 '26 10:02

someone


You can check that i equals 999, unless of course it is additionally modified inside the loop:

for(int i=0;i<1000;i++) {
    if (i == 999) {
        // Last iteration
    }
    ... // More code
}
like image 27
Sergey Kalinichenko Avatar answered Feb 02 '26 10:02

Sergey Kalinichenko