0

I am confused about for loop, in "test counter" why it is used less than (jerry < tom.length)..?

public static void main (String args[]) {

int tom[] = {1, 2, 3, 4, 5};    

    for (int jerry=0; jerry<tom.length; jerry++)
    {
System.out.println(jerry+ "\t" +tom[jerry]);

} }
Sakthi Kumar
  • 3,047
  • 15
  • 28

1 Answers1

0

Arrays in java(c/c++/ruby etc) start indexing arrays from 0 not 1.
So that's why it starts as jerry=0(first index is 0) and end at length of your array - 1, which is the last index(in your example last index is 4)

Darek Nędza
  • 1,420
  • 1
  • 12
  • 19
  • can u clear me on this: lets see jerry value is 0 < tom.length value is 0, as arrays indexing start from 0, so this condition is true or false. if this condition is false the loop should not go further right? – user2694115 Aug 18 '13 at 18:45
  • If both jerry AND length of your array(tom.length) is 0 then this condition is false, so your for loop doesn't run. That what you asked? ps. in your case tom.length=5, not 0 – Darek Nędza Aug 18 '13 at 18:59