aspose file tools*
The moose likes Beginning Java and the fly likes Not looping... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not looping..." Watch "Not looping..." New topic
Author

Not looping...

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I am trying to code some small stuff as suggested and am trying to do it 100% from memory.. I am trying to get a loop to add 1 to variable x until it reaches 100.. The code below when run shows 1 and stops..

What am I missing or doing wrong here?
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
You need a looping structure: while, for, do/while or a recursive method.
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
In other words, something like this:
... or this ...

... or this ...

The previous poster suggested you could even use recursion, something like this:

[ October 01, 2006: Message edited by: Paul Santa Maria ]

Paul M. Santa Maria, SCJP
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
so what would an if statement be best used for?
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
One more example (C/C++):

No, you shouldn't actually write code like this (and you can't, in Java! )

But the point is, use "if/else" to check a condition ("is this true at this point in time?"). Use "while", "for" and "do" to create a loop ("repeat this action until you reach some goal").

Make sense?

'Hope that helps .. PSM
[ October 01, 2006: Message edited by: Paul Santa Maria ]
 
wood burning stoves
 
subject: Not looping...