Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 11 : While

The While command is another type of looping command.  Like the For command, While executes until a condition is met.  Here is the form of the While command:

while [conditional]
  'things to do
  wend

 
dim a as int
a=1
while a<10
   print "a=", a
   a=a+1
wend

The Wend command ends a while loop.

 

DO THIS

Copy the file "c:\…\B2Cv5\tutorial\ch11.b2c" to "C:\…\B2Cv5\ch11.b2c".  Then execute the command "build ch11.b2c"  Download the ch11.app file to the cybiko

This example program plays "Hi/Low" – a number game where the user has to guess the computer's random number

'Hi Low Number Guesser
dim guess as int
dim tries as int
dim number as int
tries=0
number = rnd(100)+1
while(guess <> number)
    print "enter your guess"
    input guess
    if guess < number then
        print guess, " is too low"
        tries=tries+1
    elseif guess > number then
        print guess, " is too high"
        tries=tries+1
    end if
wend  
print "You guessed it."
print "The number was ", number
print "It took you ", tries, " tries"
dim a
input a