Nested Control Structures

From BR Wiki
Revision as of 14:29, 20 February 2012 by Mikhail.zheleznov (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

DO loops, FOR loops, IF statements, GOTO statements are the different control structures which may all be mixed together into one program. Please, see the example below as a demonstration of this mix:

00010 ! calculates average score , allows user to add more scores after the initial calculation
00020 print newpage
00030 let sum_of_scores = 0 ! provided for clarity, not required
00040 let average = 0 ! provided for clarity, not required
00050 let number_of_scores = 0 ! provided for clarity, not required
00060 let new_score = 0 ! provided for clarity, not required
00070 start: !
00080 let more_scores$ = "Y"
00090 do while trim$(uprc$(more_scores$)) = "Y"
00100    print "Do you want to add more scores?"
00110    input more_scores$
00120    if trim$(uprc$(more_scores$))="N" then
00130       exit do
00140    else if trim$(uprc$(more_scores$))<>"Y" then
00150       print "Invalid entry"
00160       let more_scores$ = "Y"
00170    else
00180       print "How many scores do you want to add?"
00190       input scores_to_add
00200       for count = 1 to scores_to_add
00210          print "Enter new score"
00220          input new_score
00230          if new_score < 0 or new_score > 100 then
00240             print "Invalid score. you will be asked to re-enter the score"
00250             let count -= 1
00260          else
00270             let sum_of_scores += new_score
00280             let number_of_scores += 1
00290          end if
00300       next count
00310    end if
00320 loop
00330 if number_of_scores <> 0 then
00340    let average = sum_of_scores / number_of_scores
00350    print "The average of "&str$(number_of_scores)&" scores is "&str$(average)
00360    print "Do you want to add more scores?"
00370    input more_scores$
00380    if trim$(uprc$(more_scores$)) = "Y" then
00390       goto start
00400    end if
00410 end if
00420 print "Good bye"
00430 stop