Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop!" Watch "for loop!" New topic
Author

for loop!

Akash Kumar
Greenhorn

Joined: Jul 17, 2002
Posts: 13
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Malik Tahir
Greenhorn

Joined: Sep 29, 2001
Posts: 18
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
swarna kumar
Greenhorn

Joined: Nov 11, 2001
Posts: 17
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
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

Joined: Jul 17, 2002
Posts: 13
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

Joined: Dec 20, 2001
Posts: 3271
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
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
Akash,
Let me again remind you of the JavaRanch Naming Policy. Please change your publicly displayed name to conform to this rule.
Thanks,
Corey
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
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
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
Hi Neil,
you just said what i had in my mind.


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop!
 
Similar Threads
Couple of questions - Help needed
WRAP class question and nested loop question!!
Loop Problem
Interface question from JQPlus