please dont rip this site

Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 10 : Conditionals : If … Then … Else … End If

Another thing computers are good at is making unbiased decisions.  In B2C this is implemented with the If statement.  The If statement has a conditional followed by the Then clause:

if [conditional] then
  'statements
end if
 
if a < b then
    print "a is smaller"
end if

This conditional has a left side and a right side with a relational operator in between.  You can compare two variables, or two expressions.  There are 6 relational operations:

                <            less than
                >            greater than
                <=           less than or equal to
                >=           greater than or equal to
                =            equal to
                <>           not equal to

For example:

if a+1 < b*2 then
    'statements
end if

There is also an Else statement, which indicates what to do if the conditional is not met:

if [conditional] then
    'statements
else
    'else statements
end if
   

 
if a<b then
    print "a is smaller"
else
    print "b is smaller"
end if

If you inspect our last example closely – you will see an error in the logic.  The error occurs when a is equal to b.  In this case our example will print "b is smaller" which is of course incorrect.  To correct this we have the Elseif statement.  It works just like the If statement and is executed only when the if part is false.

if [conditional] then
    'statement
elseif [conditional] then
    'elseif statements
else
    'else statements
  endif

 
if a<b then
    print "a is smaller"
elseif b<a then
    print "b is smaller"
else
    print "a and b are equal"
end if

You may have as many Else if statements as you like, and you may have as many statements in the if/elseif/else clauses as you like.  Again. it is good practice to indent the statements to show which statements belong to which if/elseif/else clauses.

Combining Conditionals

The result of the relational operators (<,>,<=,>=,=,<>) is either TRUE or FALSE.  This type of value is called Boolean (named for the mathematician George Boole).  Booleans  are either True or False (one or zero).  These types of results can be combined with the Boolean operators AND and OR.  You can even negate the conditional with the NOT operator.

The And operator results in a TRUE value if BOTH of the inputs are TRUE (and results in a FALSE otherwise).  The Or operator results in a TRUE value if EITHER of the inputs are TRUE.  In B2C, a zero value is FALSE and any other value is considered TRUE.  NOT converts a TRUE result into a FALSE result.

if a<b and b<c then
    print "b is between a and c"
end if
 
if a>b or a>c then
    print "a is bigger than either b or c"
  end if

 
a=1
b=0
if a and b then
    print "both a and b are true"
end if
 
if a or b then
    print "either a or b is true"
end if

Example program:

 

DO THIS

Copy the file "c:\…\B2Cv5\tutorial\ch10.b2c" to "C:\…\B2Cv5\ch10.b2c".  Then execute the command "build ch10.b2c".  Download the ch10.app file to the cybiko.
Conditionals will allow us to assign a letter grade to the input grades and the average
' chapter 10 example program
' sum and average of n grades
' grades are from 0-100
dim sum    'this variable will store the sum of the n grades
dim avg    'this variable will store the average of the n grades
dim n as int   ' the number of grades to average
 
print "How many grades?"
input n
dim grade[n]  ' an array of n grades
 
 ' ---INPUT ---
for i=0 to n-1                 'get the inputs
    print "Enter grade ", i   ' notice that we indent in loops
    input grade[i]                'get the grades from the student
next
 
'--- PROCESS ---
' compute the sum and the average
for i=0 to n-1
    if grade[i] >=94 then
       print grade[i], "=A"
    elseif grade[i]>=86 then
       print grade[i],"=B"
    elseif grade[i]>=78 then
       print grade[i], "=C"
    elseif grade[i]>=70 then
       print grade[i], "=D"
    else
       print grade[i], "=F"
    end if
    sum = sum + grade[i]   'notice we accumulate the sum
next
avg = sum/n
 
'--- OUTPUT ---
print "The average of your", 
print n, " grades is ", avg
if avg >=94 then
       print avg, "=A"
elseif avg>=86 then
       print avg, "=B"
elseif avg>=78 then
       print avg, "=C"
elseif avg>=70 then
       print avg, "=D"
else
       print avg, "=F"
end if
 
print "Press <Enter> to continue"
dim tmp ' a temporary variable
input tmp 'wait for the user to press enter

 


file: /Techref/cybiko/b2c/ch10.htm, 7KB, , updated: 2008/6/13 15:01, local time: 2024/11/24 20:02,
TOP NEW HELP FIND: 
3.16.82.208:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://ecomorder.com/techref/cybiko/b2c/ch10.htm"> cybiko b2c ch10</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .