my $i; for ($i = 0; $i <=100; $i++) { statements ... } $i = 200 if defined ($v); #same thing as your code #$i is 101 here, but it was #100 on last loop iteration #test can be moved outside of the loop