• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

SmallTalk Looping

Apologies for posting this to the Java section, but there does not appear to be a SmallTalk area.

The following is supposed to display a series of dialog boxes with the name of each month and index from month1 to month2 inclusive or from month2 to month1 inclusive, depending on which is the larger number. Problem with the code below is that the first loop does not stop at month2, but overruns by one, then steps back. If I run the first loop on its own, it behaves fine. Is there a way to say 'Elseif' in SmallTalk?

Many thanks,
Mab
***************************************

      ifTrue:[
      [month1 <= month2]
            whileTrue:
                  [Dialog warn: (MonthNames at: month1),' ',month1 printString.
                  month1:= month1 + 1].
      [month1 >= month2]
            whileTrue:
                  [Dialog warn: (MonthNames at: month1),' ',month1 printString.
                  month1:= month1 - 1]
      ]
0
mjacobs2929
Asked:
mjacobs2929
  • 3
1 Solution
 
objectsCommented:
Looks like you can use ifFalse: something like the following maybe:

month1<=month2
   ifTrue:[
      [month1 <= month2]
          whileTrue:
               [Dialog warn: (MonthNames at: month1),' ',month1 printString.
               month1:= month1 + 1].
   ]
   ifFalse:[
     [month1 >= month2]
        whileTrue:
               [Dialog warn: (MonthNames at: month1),' ',month1 printString.
               month1:= month1 - 1]
   ]
0
 
mjacobs2929Author Commented:
Thanks objects!
Something like the above should do the trick. And thanks for the link to the book - Looks Very informative!
0
 
objectsCommented:
no worries, good luck with it :)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now