Home > MicroWorlds Logo Links & Examples > MicroWorlds Code Pages > increment2 Code

increment2 Code

increment2.txt 6 KB


;  Purpose: To demonstrate incrementing a counter.
;  Copyright © 2004 by Rupert Russell
;  e-mail: rupert.russell1@gmail.com
;  URL: http://www.rupert.id.au/microworlds/
;  Created: October 29, 2004
;
;  This program is free software; you can redistribute it and/or modify
;  it under the terms of the GNU General Public License as published by
;  the Free Software Foundation; either version 2 of the License, or
;  (at your option) any later version.
;
;  This program is distributed in the hope that it will be useful,
;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;  GNU General Public License for more details.
;
;  You should have received a copy of the GNU General Public License
;  along with this program; if not, write to the Free Software
;  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
; ---------------------------------------------------------------------

to setup
  make "counter 0                                
; create the counter and set its initial value to 0
  show_value_of_counter
  cg
end

to increment

   carefully [show_value_of_counter] [setup]    
; check that the counter has been created
                                                
; if the counter has not been created then the 
                                                
; command carefully will not cause an 
                                                
; error but will run the procedure setup
   cg 
   if :counter < 10 [                           
; if the current value of :counter 
                                                
; is less than 10 then
      make "counter :counter + 1                
; increment the :counter by 1
      show_value_of_counter                     
; show the vale of the :counter
   ]
   stamp_turtles
end

to decrement

   carefully [show_value_of_counter] [setup]    
; check that the counter has been created
                                                
; if the counter has not been created then the 
                                                
; command carefully will not cause an error 
                                                
; but will run the procedure setup

   if :counter > -10 [
      make "counter :counter - 1                 
; decrement the counter by 1
   ]

   cg
   show_value_of_counter
   stamp_turtles

end

to show_value_of_counter

   ct
   if :counter = -10 [
        insert ( sentence   "Minimum "value "reached )        
;  Minimum value reached      
   ]

   if :counter = 10 [
        insert ( sentence "Maximum "value "reached  )         
; Maximum value reached
      ]

  ; insert the value of the counter in the current text box
   if (and :counter > -10 :counter < 10) [
         insert ( sentence "The "counters "value "is "  )        
         insert :counter
      ]
end

to stamp_turtles


   if :counter = 0 [
      setshape "clear_sq
      stamp
   ]

   if :counter > 0 [
      setshape "clear_sq
      stamp

      repeat :counter  [
      setshape "
green_sq
      fd 18
      stamp
      ]
   ]

   if :counter < 0 [
      setshape "clear_sq
      stamp

      setshape "
red_sq
      repeat abs :counter [
      bk 18
      stamp
         ]
   ]

   if :counter = 0 [
      setshape "clear_sq
      stamp
   ]
end

to reset

   ct
   cg
   make "counter 0
   show_value_of_counter
   stamp_turtles
end


APA citation:
Russell, R. (2016, July 04, 04:16 pm). increment2 code.
     Retrieved April 27, 2024, from http://www.rupert.id.au/microworlds/code/increment2.php



458 Visits since October 29, 2004