Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

The for loop  RSS feed

 
Biniman Idugboe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, bear with me.  I am new to Java programming language.  The following for loop does not work.  Execution skips the code block altogether.  Could somebody explain to me, what's wrong with it.

 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biniman Idugboe wrote:


The condition for the loop to keep going is "i" should be less than 1 and its value has been set to 10 which is greater than 1
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And finally, I get to say it.

Welcome to the Ranch!
 
Biniman Idugboe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot man!  You have just relieved me.  I can go to sleep now.  It far past bedtime in my region.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biniman Idugboe wrote:Thanks a lot man!  You have just relieved me.  I can go to sleep now.  It far past bedtime in my region.


No worries bro, one time I was stuck on my application for a whole 5 days and no one could figure out my issue, turned out I was just missing a zero somewhere and I discovered it when I had decided to scrap the whole thing and do it something else. It is usually the small things that are the issue. Look for those

Good night
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:
Biniman Idugboe wrote:


The condition for the loop to keep going is "i" should be less than 1 and its value has been set to 10 which is greater than 1

One of options. Another option is, that OP might wanted to print out numbers starting from 10 and going down to 1 or so. If that is the case, then for the boolean condition other operator needs to be used.
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably best would be if OP would describe what app supposed to be doing. Not describing problem clearly, I could say - this loop works great as it does exactly what it supposed to be doing.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Probably best would be if OP would describe what app supposed to be doing. Not describing problem clearly, I could say - this loop works great as it does exactly what it supposed to be doing.


I can tell you are a pro dev from your posts, however, me and him are both noob programmers still learning. So we understand each other, my friend

The noobers connection I call it
 
fred rosenberger
lowercase baba
Bartender
Posts: 12464
43
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paper and pencil (or their electronic equivilent) are often your best friend.  I would suggest stepping through the code a piece at a time...something like:

first, set i to 10.
Then if i is less than 1, execute the body.
...well...i is 10, and 10 is NOT less than 1, so skip the body..
oh...
problem solved.

learning to do this is an incredibly important skill. Start practicing now, on simple stuff, so when you get to more complicated stuff, it's not as huge a cliff to climb.
 
Nick David
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming you wanted to print from 10 down to 2. In that case, what your loop is essentially saying is  i = 10, then i is < 1. This will never run because 10 is not less than 1. Change "i < 1;"  to "i > 1;" since i is 10, an 10 is greater than 1. And welcome to the Java world
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch  Nick

Ah, I feel I am part of the family already 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!