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