• 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

for loop!

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the following code, it runs fine.The answers say what is wrong with the above code? Can I anybody help me to select the correct answer.

What is the error in the above code?
Ans. a. The loop will never terminate.
b. You cannot print integer values without converting them to strings.
c. The comment line is not formatted correctly.
d. You cannot declare variables inside a for-loop.
e. Variable j is referenced outside its scope.
Edited by Corey McGlone: Added Code Tags
[ July 18, 2002: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akash,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
It looks to me like you've got an infinite loop there, so I'd say that 'a' is the correct answer.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akash,
Ans. A is right, b/c this loop will never terminate due to absence of increment.
Look closely u will find the error.
Bye
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
malik is right,
for statement systax is wrong;
for(initialization;condition;iteration){
}
this is the syntax,here thers is no iteration part.
regds
swarna
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by swarna kumar:
malik is right,
for statement systax is wrong;
for(initialization;condition;iteration){
}
this is the syntax,here thers is no iteration part.
regds
swarna


All parts of a for loop are optional. You can leave out the initialization, conidition, and iteration statements. In the case that you leave out the condition statement, a true value is implied. The following is an infinite loop:

Had the syntax been violated, a compiler error would have occurred - not an infinite loop.
Corey
 
Akash Kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks
This runs fine. No systax error, no infinite loop also. Pls see below the output pls:
i: 1 j: 2
i: 2 j: 3
i: 3 j: 4
i: 4 j: 5
i: 5 j: 6
i: 6 j: 7
i: 7 j: 8
i: 8 j: 9
i: 9 j: 10
i: 10 j: 11
i: 11 j: 12
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akash:
Hi Folks
This runs fine. No systax error, no infinite loop also.


No, this is an infinite loop. Just because the output stops, doesn't mean that the loop ends. The fact is that the output only occurs when i <= 10. Also, i is only incremented while i <= 10.
Therefore, i will eventually become 11, and the loop will cycle endlessly, doing nothing. Try modifying the loop to this to see the infinite loop:

Corey
 
Akash Kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Corey you are right, I was not looking closely.
Thanks a lot Corey,Swarna, and Malik.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akash,
Let me again remind you of the JavaRanch Naming Policy. Please change your publicly displayed name to conform to this rule.
Thanks,
Corey
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey
There seem to be a lot of reminders for people to change their names. Ever tried modifying the UBB cgi code to be more restrictive? Or would that invalidate your support contract
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neil,
you just said what i had in my mind.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic