Jump to content

Very basic Java for loop queston

Go to solution Solved by 2FA,

gs = 0: assigns 0 to the variable gs in the scope of the loop.

gs <= 10: as along as gs is less than or equal to 10, the loop will run.

gs++: gs gets incremented after each run through the loop

I was wondering if you could possibly answer my Java question about for loops:
According to this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html 
It says "== equal to
!=      not equal to
>       greater than
>=      greater than or equal to
<       less than 
<= less than or equal to"
But when I run it through a for loop, it seems as if the inverse is true. I have no idea why either. Am I reading it backwards?
 
For example:
It says
"int gs=0; gs<= 10; gs++"
which are the three statements: when the for loop should start, when it should end, and by how much it should increment. 
 
I run it, and it counts to ten. I do not understand why, though. Isn't this foor loop saying that the for loop should end when gs is less than or equal to 10? So, shouldn't it just not start? Well, it doesn't even start when I use >= there. Can you please give me a short explanation?
 
Thank you so much!
 
tl;dr why is <= not working correctly in a for loop

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to comment
https://linustechtips.com/topic/800121-very-basic-java-for-loop-queston/
Share on other sites

Link to post
Share on other sites

gs = 0: assigns 0 to the variable gs in the scope of the loop.

gs <= 10: as along as gs is less than or equal to 10, the loop will run.

gs++: gs gets incremented after each run through the loop

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

1 minute ago, DeadEyePsycho said:

gs = 0: assigns 0 to the variable gs in the scope of the loop.

gs <= 10: as along as gs is less than or equal to 10, the loop will run.

gs++: gs gets incremented after each run through the loop

Ohhhhhhhhhh

1 minute ago, elpiop said:

No, it is saying that the loop should continue until gs is not less than or equal to 10

Man I am such an idiot

 

I see it now

 

I am just so tired...

 

Thank you!

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×