• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

What is the mistake in this for loop?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Please point me mistake in my for loop.



I thought this is equivalent to below while loop:

 
Marshal
Posts: 26114
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put your code fragments into the "code" tags. Please do that in future so that it's easier to discuss code fragments. (Select the code fragment in your post and click on the "Code" button at the top of the box.)

Anyway: Look at line 3 in your second code fragment. Notice that you don't have the same thing in the first code fragment, so the two aren't equivalent.
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I put your code fragments into the "code" tags. Please do that in future so that it's easier to discuss code fragments. (Select the code fragment in your post and click on the "Code" button at the top of the box.)

Anyway: Look at line 3 in your second code fragment. Notice that you don't have the same thing in the first code fragment, so the two aren't equivalent.



Thank you for pointing out. I didn't notice that. Even no compiler error (Obviously). When I changed it, it stopped with one iteration while we want it to stop iteration only if input is -1. So I wrote below code.



This worked.

Thanks again.
 
Paul Clapham
Marshal
Posts: 26114
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dreke Droga wrote:This worked.



Yes... but I have to say, I find the other version with the while-loop easier to understand.
 
Marshal
Posts: 71028
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a better way to run that loop, but you need () round the assignment operator because you want that executed before the comparison operator:-?
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. This is definitely better code. May I please know how you analyzed your algorithm inorder to come to this approach?
 
Paul Clapham
Marshal
Posts: 26114
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a concept called "N-and-a-half loop" which means that a loop will be executed N and a half times. This situation occurs when the loop looks like this:



It happens fairly often, and that's what Campbell has posted there. Only in the posted code, the "initialize" part is score = myScanner.nextInt() and the break-out test is score < 0. So the "initialize" and "break out" parts can be combined and put into the loop header.

But sometimes the "initialize" part is too complicated to do that and then the loop looks like this:



You don't see this very often, though, because there's somewhat of a prejudice against breaking out of the middle of a code block which dates back a couple of decades or so, back to when "structured programming" was the new big thing. So if the "initialize" part is too complex to roll into the loop header, people will make it into a method and then put that into the loop header.
 
Campbell Ritchie
Marshal
Posts: 71028
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think it would be a good idea for beginners to learn structured programming; they will be better able to understand when it is permissible to breach its conventions.
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:There's a concept called "N-and-a-half loop" which means that a loop will be executed N and a half times. This situation occurs when the loop looks like this:



It happens fairly often, and that's what Campbell has posted there. Only in the posted code, the "initialize" part is score = myScanner.nextInt() and the break-out test is score < 0. So the "initialize" and "break out" parts can be combined and put into the loop header.

But sometimes the "initialize" part is too complicated to do that and then the loop looks like this:



You don't see this very often, though, because there's somewhat of a prejudice against breaking out of the middle of a code block which dates back a couple of decades or so, back to when "structured programming" was the new big thing. So if the "initialize" part is too complex to roll into the loop header, people will make it into a method and then put that into the loop header.





Thank you for explanation. I understood your code. I don't know Structural programming. Although I didn't grasp what you said completely, I will go through this again. I will start learning Structural programming along with java now.
 
Paul Clapham
Marshal
Posts: 26114
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Structured programming
reply
    Bookmark Topic Watch Topic
  • New Topic