• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

While Loop Prob:

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class test
{
public static void main(String[] agrs)
{
int i=0;
int a[] = new int[]{'A','B','C','D','E','F'};
System.out.println(a.length);
while(i++ <a.length)
{
System.out.println(i);
System.out.println((char)a[i]);
}
}
}

For this Prob: the values at while condition are
(1<6)
a[1] = B
(2<6)
a[2] = C
(3<6)
a[3] = D
(4<6)
a[4] = E
(5<6)
a[5] = F
(6<6)
Condition fails so it will come out of the loop. But while executing it showing Run time Exception (ArrayIndexOutOfBound). Can any one will tell me why it is happening like this?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In while(i++ <a.length), the post-fix operator is incrementing i after the test comparison to a.length. In other words, the i you test at the beginning of the loop is not the i you're using within the loop as an array index.

One way to correct this is to increment i at the bottom of the loop. This way, the i you test is the i you use...

[ March 19, 2006: Message edited by: marc weber ]
 
Kiran Chand Panga
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc weber
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello marc,

there are two operators in the while condition... the post-increment operator and less than sign.... according to precedence of operators post-increment operator has higher precedence over less than operator.. so i feel, first the variable is incremented and then the comparision operator is evaluated...

please correct me if i am wrong
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while(i++ <a.length)
{
System.out.println(i);
System.out.println((char)a[i]);
}
}
}

For this Prob: the values at while condition are
(1<6)
a[1] = B
(2<6)
a[2] = C
(3<6)
a[3] = D
(4<6)
a[4] = E
(5<6)
a[5] = F
(6<6)

---------------------------------------------------------------------------
In other words when there is a condition test in the while, the value of i is not incremented.The value is incremented just after the condition is tested. While entering into the block the value is incremented and hence results in the exception when the value of i=5.

e.g as you have indicated in the last but two line, (5<6) is the test condition.However,inside the block it is a[6]. This is so because it's a postincrement operator.

Please correct me guys if I am wrong.

Cheers,
-Biswa
[ March 19, 2006: Message edited by: Biswamohan Routray ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mithun gm:
...post-increment operator has higher precedence over less than operator.. so i feel, first the variable is incremented and then the comparision operator is evaluated...


The expression i++ is evaluated prior to the < comparison being made. But the postfix operator does not increment until after returning a value.

In the words of the Java Language Specification (section 15.14.2), "The value of the postfix increment expression is the value of the variable before the new value is stored."

So as Biswa explained above: In this case, when i is 5, then i++ evaluates to 5 (which is used in the comparison test), and then i is incremented to 6. So the test is passed, but within the loop itself, i is out of bounds.
[ March 19, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ur right ..

If u change the initial value of i = -1 and chk the cond like

if( i++ < (a.length -1 ))
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kiran Chand Panga

you are trying to iterate the while loop 7 times.
the array index starts at 0.
just make a small change in your code as follows...it works fine


while(++i <a.length){
System.out.println(i);
System.out.println((char)a[i]);
}

KiranKumarAlapati
SCJP 1.4
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can try this code. I think it will give a clear idea.

class Test
{
public static void main(String[] agrs)
{
int i=0;
int a[] = new int[]{'A','B','C','D','E','F'};
System.out.println(a.length);
System.out.println("before"+i);
while(i++ <a.length)
{
System.out.println(i);
System.out.println((char)a[i-1]);
}
}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try this too, this clearly explains difference of post and pre increment:
class Test
{
public static void main(String[] agrs)
{
int i=0;
int a[] = new int[]{'A','B','C','D','E','F'};
System.out.println(a.length);
System.out.println("before"+i);
while(++i <a.length)
{
System.out.println(i);
System.out.println((char)a[i]);
}
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic