This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
    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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Having trouble with this for loop

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
So I was trying to use this for loop to print the numbers from the first number entered to the last one entered. I don't know why when I run it it doesn't acknowledge the loop at all. Can anyone see any errors that could be causing it to not run?
Screen-Shot-2020-02-09-at-9.08.45-PM.png
[Thumbnail for Screen-Shot-2020-02-09-at-9.08.45-PM.png]
 
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Will retireAge be less than userAge?

For future reference: Please don't post screen shots. Cut and paste code into a post instead. Thanks.
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Hanna Roberts
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator

Carey Brown wrote:Will retireAge be less than userAge?

For future reference: Please don't post screen shots. Cut and paste code into a post instead. Thanks.



No I was trying to have retireAge greater than userAge. And oh ok sorry. Iposted the screenshot so you could see that the loop was completely ignored after I input the numbers.
 
Hanna Roberts
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator

Carey Brown wrote:The loop will continue while this is true. Is this what you want?


I think I have the retireAge and userAge switched. Could that cause it to not run at all?
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Hanna Roberts wrote:

Carey Brown wrote:Will retireAge be less than userAge?

For future reference: Please don't post screen shots. Cut and paste code into a post instead. Thanks.



No I was trying to have retireAge greater than userAge. And oh ok sorry. Iposted the screenshot so you could see that the loop was completely ignored after I input the numbers.

Usually it is stated as: while the user age is less than the retirement age.
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Hanna Roberts wrote:

Carey Brown wrote:The loop will continue while this is true. Is this what you want?


I think I have the retireAge and userAge switched. Could that cause it to not run at all?

yes.
 
Hanna Roberts
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
Oh I think I get the problem now. I had the sign backwards it need to be retireAge >= i; right?
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Hanna Roberts wrote:Oh I think I get the problem now. I had the sign backwards it need to be retireAge >= i; right?


Yes. But...

Usually the 'i' variable precedes the limit variable. So if you swap it around you get
i <= retireAge
 
Hanna Roberts
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator

Carey Brown wrote:

Hanna Roberts wrote:Oh I think I get the problem now. I had the sign backwards it need to be retireAge >= i; right?


Yes. But...

Usually the 'i' variable precedes the limit variable. So if you swap it around you get
i <= retireAge



Oh I see. Thanks.
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
This doesn't make your answer wrong but as go along your Java learning curve you will notice that there are a lot of Java programming conventions. Following the conventions allows seasoned Java programmers to quickly read and understand the intent of the code. When you don't follow conventions it may still work but then the reader has to stop and analyze the code to understand why it was written differently, this can slow the reading process down enormously.

Another convention that popped up in your code is ++i. This is a per-increment instruction. Because there's no other larger expression that it is a part of it behaves the same as i++, a post-increment instruction. The convention is to always use post increment unless you are specifically doing something that requires pre-increment. Again, this is just one of those things you get used to but you'll find your reading and comprehension speed improving.
 
Hanna Roberts
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator

Carey Brown wrote:This doesn't make your answer wrong but as go along your Java learning curve you will notice that there are a lot of Java programming conventions. Following the conventions allows seasoned Java programmers to quickly read and understand the intent of the code. When you don't follow conventions it may still work but then the reader has to stop and analyze the code to understand why it was written differently, this can slow the reading process down enormously.

Another convention that popped up in your code is ++i. This is a per-increment instruction. Because there's no other larger expression that it is a part of it behaves the same as i++, a post-increment instruction. The convention is to always use post increment unless you are specifically doing something that requires pre-increment. Again, this is just one of those things you get used to but you'll find your reading and comprehension speed improving.



That makes sense. Thanks for letting me know
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
While we're on the subject, you declare you int variable 'i' outside of the loop. A seasoned reader would notice this and start looking for the place outside of the loop where you used 'i'. You don't have such a place so this added a slight delay in comprehension. The convention is that if your loop variable is only needed inside the loop then that is where it should be declared. So you should remove your current int i; declaration and modify the loop to look like this:
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!