Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not looping...

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a looping structure: while, for, do/while or a recursive method.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what would an if statement be best used for?
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic