• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt with ++ operator

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source Majji:Q15
What will happen when you invoke the following method?

1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }

A) The loop never ends(infiniteLoop).
B) Prints "Welcome to Java".
C) Compilation error at line 5. ++ operator should not be used for byte type variables.
D) Prints nothing.

Ans: B

Doubt: at certain point b's value will exceed the range of byte, so should it not give compilation error.

regards,
gitesh
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right,
at a point, b's value will be 127 and when it still do ++b it will become
-128, which is less than 0, so the wilhe loop quits and prints the text.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why it returns "Welcome to Java" only one time. How can we know the println statement is not a body of the while loop. It is written in book, "if there is only one line in loop body you are free to don't put that line in {}. When I did this program I thought it prints "Welcome to Java" 127 times.
Now I am little confused. Can anybody help me?
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
Why it returns "Welcome to Java" only one time. How can we know the println statement is not a body of the while loop. It is written in book, "if there is only one line in loop body you are free to don't put that line in {}. When I did this program I thought it prints "Welcome to Java" 127 times.
Now I am little confused. Can anybody help me?



Check out line number 6.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is exactly one line in the body of the while loop; it's line 6. You may wish to read up on the JLS 14.6: empty statement.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is that? The ( is by mistake place there. But my question is different.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't your println statment outside the while loop though? Because you don't have these {} after while loop, the first empty stament is the only one that's in loop.

Try removing line 6 and then you will see "Welcome to Java" printing 127 times. And then add "Welcome to Java - 2" after that so you will see this printing only once after printing the first one 127 times. Then try adding {}, you will see all the difference.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, well it is really important if the ; is there or not:



prints:
Oachkatzerlschwoaf
squirrel tail #2
squirrel tail #3
.....
squirrel tail #125
squirrel tail #126
squirrel tail #127
main ready




After #127 the b is -128 and the loop is left.
Oachkatzerlschwoaf is the bavarian word for squirrel tail.
Now you also see what this code tags are good for.



Yours,
Bu.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It still gives me "Welcome to Java" only once.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:


It still gives me "Welcome to Java" only once.



Hey dolly!
It doesnt matter whether you put semicolon( ; ) on the top (with while)or new line, what matters is whether you are putting it or not...


and


both are same... please read again what Ulf has said.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



That is the original code. Here ( plays a main role. Now I got it.
Thanks Hassel ,Ulf & Akhilesh.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hats Off to everybody for Help
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic