The condition inside the loop in smalltalk

I am trying to draw a character string using a loop. I do this, but always draw circles x...

1 to: x do: [
    (self lastWasSquare)
            ifTrue: [ self drawCircle]
            ifFalse: [ self drawSquare]
]

I also tried:

x timesRepeat: [ 
    (self lastWasSquare)
            ifTrue: [ self drawCircle]
            ifFalse: [ self drawSquare]
]. 

But still drawing circles. I also tried to do this by adding a variable :n |to the loop and asking, even if, but again, it always executes the circle code. What am I doing wrong? thank you

+4
source share
1 answer

It looks like your call self lastWasSquarekeeps returning trueso that your #ifTrue:ifFalse:continues to go to the block that is calling self drawCircle. You can:

  • , drawCircle drawSquare lastWasSquare ( , , getter).
  • , .

, lastWasSquare , . , , ( ) :

| lastWasSquare |
lastWasSquare := false.
x timesRepeat: [ 
    lastWasSquare
        ifTrue: [ self drawCircle ]
        ifFalse: [ self drawSquare ].
    lastWasSquare := lastWasSquare not
].

, lastWasSquare true false, . ( , , " "...)

, , , , .

+3

Source: https://habr.com/ru/post/1606195/


All Articles