• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem understanding "for(b=1;b<=a;b++)"  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

First post on the forums, so I hope I'm posting this in the right section!

I'm trying to understand a piece of code that was given to me, and this section is giving me some problems:



Specifically I'm seeking help in understanding the first part. Where it states the selection "for(b=1;b<=a;b++)" referring to the "AvailableValues[b];".


Can anyone help me in understanding exactly what this does/means?





 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Caspar. Welcome to the Ranch!

That's just a for loop. Have you not come across those before? There's a section in the Java tutorials here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The most common form looks something like this:
That declares a variable b and initialises it to 1. Then it executes the loop body a times - on each step it increases b by 1, and it keeps going as long as b <= a is true. The only difference in your code is that the declaration is left off (no int), which means you need to have already declared b somewhere earlier in the code.

Does that help?
 
Ranch Hand
Posts: 221
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The page in the Java Tutorials should help.

In effect, for(b=1;b<=a;b++) is shorthand for


Regards,
Robert
[edited to fix my html code (forgot the "/i"]
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use i j k for integers; it is the conventional way to do it, and it will be more obvious.

Beware of this sort of pitfall
 
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe Campbell, i think you meant to say since j is always greater than i

for loops are very basic. i really hope you are not having that much of a problem understanding them
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never had any difficulty understanding “for” loops. My difficulty is writing as you have already noticed.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!