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 ]
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.
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
Joined: Dec 20, 2001
Posts: 3271
posted
0
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
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
Joined: Jul 17, 2002
Posts: 13
posted
0
Yes, Corey you are right, I was not looking closely. Thanks a lot Corey,Swarna, and Malik.
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Akash, Let me again remind you of the JavaRanch Naming Policy. Please change your publicly displayed name to conform to this rule. Thanks, Corey
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
please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details