PROGRAMMING A program line is composed of words,letters and numbers. A program is composed of program lines which are typed into the computor. These are sometimes known as source code. A line is not fed into the computor until the return key is pressed. Lines are composed of different building blocks. COMMENTS It is possible to insert comments into the program text, which are not part of the program. Any text after the REM statement(or an ') on the same line, is not executed. This provides useful information to the programmer about a program. q 001011 QUIZ ON PROGRAM LINES AND COMMENTS True or false Type 1 if the statement is true, or 0 if the statement is false (1) Comments can be called the source code (2) All text in a program is executed (3) A program is composed of lines (4) A program consists of one line (5) Rem makes it possible to add remarks (6) An ' makes it possible to insert comments LINE NUMBERS A line number is a number to the very left of the screen which shows the position of a line in a program. Although needed in many languages, line numbers are optional in AMIGAbasic If used, line numbers to not need to be in order. e.g. 22 LABELS A label is a word of one or more characters, and which does not include any spaces. They can be any alphanumeric (letters or numbers)combination, but must end in a colon and must start with a letter. After the first character they may be any combination of alphanumeric characters. Labels may be any length. A label cannot be a reserved word(which will be described later) e.g. a: readtext: b123a: It is often useful to use labels that are meaningful, to describe what that part of the program is doing eg. readtext as in the above example. q 100100 QUIZ ON LINE NUMBERS OR LABELS True or false Type 1 if the statement is true, or 0 if the statement is false (1) Labels must end with a colon (2) There must be a line number for each line (3) Line numbers must be in order (4) Labels are optional (5) A label can be any word (6) Labels must start with a number CONSTANTS A constant is an item which never changes its value. e.g. the number 6 never changes its value. There can be numeric constants. They can be integers(whole numbers). e.g. 6,78 or 123 or can be decimal point numbers(floating point numbers) e.g. 3.4 or 17.45 or string constants in which case they are always enclosed within quotation marks e.g. "house" can be numbers or letters e.g."67" Or in fact can be any characters which can be typed in from the keyboard e.g. # @ ! - etc. Even one or more spaces may be a string. e.g." " or no spaces at all! i.e. "" numbers which are strings cannot be used in calculations e.g. x="2"+"2" does not make any sense while x=2+2 does make sense VARIABLES A variable is an item that can change its value. They can take on the value of a constant. They can be numeric or string. They cannot have the same spelling as a reserved word(See commands). Numeric variables have a value of 0 until assigned something else. They must start with a letter e.g. total string variables end with $ and like a numeric variable must start with a letter A string is a bunch of characters(or even one character) e.g. text$ It makes a program easier to understand if a variable has a meaningful name e.g. apples -(has the value of the number of apples) e.g. nam$ -is the name of something STRINGS e.g. word$="hello" The string variable -word$ has the value "hello". NUMERICS e.g. total=6 The numeric variable -total has the value of the constant 6. q 3122424313 QUIZ ON LINE NUMBERS,LABELS AND VARIABLES The following are either: (1)line number, (2)label, (3)numeric variable or (4)string variable For each question type the appropriate number (1)money (6)draw: (2)22 (7)number$ (3)start: (8)a3 (4)a3: (9)18 (5)total$ (10)start PROGRAM LINE A statement is what a program is made of. These can be compared with a sentence. e.g IF petrol = 0 THEN GOTO endjourney In the above example it is possible to have a feel for what the statement means without understanding the language. A statement can never be on more than 1 line at a time. Several statements may be placed on one line by the use of one or more colons. In this case a colon is used to mark the end of a statement. Labels, line numbers, constants or variables may be used as an object. The above can be compared with nouns in a sentence. STATEMENT Often these are ordinary commonplace English words. In some cases they are abbreviations. These can be compared with an action verb. They can be part of a program or can be entered in direct mode. Commands are reserved words which means that a label or a variable cannot have the same name as any command. (The computor would interpret them as a command). A statement containing a command can be called the name of the command. e.g. an IF...THEN statement LINES After a line has been typed in, the return key is pressed to bring the cursor to the start of the next line. Any words that are reserved words will be converted to uppercase which is a useful reminder of which words are reserved or not, or if you have spelt or used a reserved word correctly! If you wish to prevent this from happening, then if the reserved is somewhere preceded by quotation marks, then this will not happen. This may happen, for example, if you are writing some text. typing in What is for breakfast -will lead to What is FOR breakfast -but, "What is for breakfast is OK q 0000101111 QUIZ ON LINES AND STATEMENTS Type 1 if the statement is true, or 0 if the statement is false (1) A statement must always be ended in a colon (2) There can only be one statement on a line (3) Reserved words must be typed in as capitals (4) A statement must contain a command (5) A label can be likened to a noun in a sentence (6) You must type the line number at the start of a line (7) A statement can be on several lines (8) A command can be likened to a verb in a sentence (9) There can be several statements on a line (10) A label may be the only thing on a line LET This gives the variable on the left hand side of the equation the value which is on the right hand side of the equation. This can be called assigning a value to a variable. = is not the same as in maths e.g. let x=3 e.g. let x=x+3 makes no sense in maths. In practice the LET is omitted. Hence x=x+1 makes no sense in maths ,but makes sense with the LET statement. q 0031243102 QUIZ ON LET Give the value of x in the following questions. In some questions there will be several statements(seperated by a colon). In these case give the value of x in the last statement. (1) x (6)x=3:x=4 (2) 7=x (7)x=2:x=x+1 (3) x=2+1 (8)x=3:x=x-2 (4) x=x+1 (9)x=2:x=2-x (5) x=2+x (10)x=1:x=x+x PRINT This displays text on the screen . Whatever is to be displayed, is to be enclosed within quotation marks e.g.PRINT"hello" will print the word hello on the screen(without the quotation marks)> A variable can be printed, in which case the value of the variable is printed e.g. PRINT total (different from PRINT "total") If say, total was equal to 5, then 5 would be printed on the screen To save typing PRINT"Typing ? is a shortcut for print. When the cursor next leaves that line, the ? will be replaced by PRINT " After a PRINT statement, the cursor will move to the next line down. This will that subsequent print statements will start on a new line. To keep the cursor on the same line, place a semi-colon at the end of the statement, e.g. PRINT"HELLO"; PRINT Prints a blank line. q 020ht3x4-4 QUIZ ON PRINT What will each of these statements print to the screen? (1) PRINT a (6) total=3:PRINT total (2) PRINT "2" (7) x=4:PRINT "x" (3) PRINT total (8) x=4:PRINT x (4) PRINT "h" (9) x=3:PRINT "x+1" (5) PRINT "t (10) x=3:PRINT x+1 INPUT INPUT a$ or INPUT;a$ This stops the program and waits for the user to enter a reply. The reply may either be a numeric or a string. A question mark appears on the screen which is the prompt for the user to key in a reply. This may or may not be preceded by some text. The reply will be displayed on the screen after the question mark. The program will then continue after the return(or enter)key is hit. If the wrong type is entered a message Redo from start? will be displayed. The variable will take on the value of whatever was keyed in e.g. input;a A number must be keyed in , because a is numeic. `a' will then equal that number input a$ A string must be entered a$ will then equal it. As in PRINT, the ; may be omitted. e.g. input a Also as in PRINT, strings and variables can be combined in the same line e.g. input"A number";x Strings are usually added to help the user know what is to be typed in e.g. input"Number of apples";x LINE INPUT This is the same as input, except that a whole line can be typed in at a time. Input continues until the return key is pressed. Whatever is typed in will become the given string, and will include any spaces between words. e.g. LINE INPUT text$ or LINE INPUT,"Type a line";text$ q 0100110101 QUIZ ON INPUT Type (1) The program continues if the user does not resppond (2) The return key needs to be pressed after replying to an input statement (3) An input statement only requires a numeric reply (4) The reply to an input statement is not displayed (5) An input statement gives a value to a variable (6) An input statement requires a string OR numeric reply (7) An input statement gives a value to a constant (8) Input displays a question mark on the screen (9) Text is not displayed with an input statement (10) Input is one way the program interacts with the user CALCULATION Numeric constants and variables can be used in calculations. +,-,/,* represent plus,minus,divided by, mutiplied by, in that order can all be used e.g. x=2+2 y=2*3 z=4/2 Many other calculations can be used and will be mentioned later. q 4362144232 QUIZ ON CALCULATION Give the value of x in the following questions (1) x=2+2 (6)x=3:x=x+1 (2) x=5-2 (7)x=2:x=x*2 (3) x=3*2 (8)x=3:x=2 (4) x=8/4 (9)x=3:2=x (5) x=6/6 (10)x=3:x=x-1 ORDER OF OPERATIONS Several calculations may be performed in the same line. As in ordinary arithmetic, some operators have a higher precedence than others. Brackets have precedence over everything else. There may be any number of brackets on a line. Every opening bracket must be matched by a closing bracket. If in doubt of the order, use brackets. Here is the order of operations of Arithmetic: (1) Multipication (2) Divison (3) Addition and subtaction q 8181422111 QUIZ ON ORDER OF OPERATIONS What is the value of each of the following? (1) 2+3*2 (6) 16/2* 4 (2) 2+3-4 (7) 12/2+4 (3) 2*3+2 (8) 2+2/ 4 (4) 7-3*2 (9) 2*2/ 4 (5) 2*3-2 (10) 4-2/ 2 BRANCHING Uninterrupted, a program is executed line by line until the end of the program is reached. Different commands can be used to alter the course of program control. Program control may jump forward, jump backwards, repeat sections etc. Branching makes use of labels and/or line numbers. GOTO Branches to a label or a line number. The colon after a label is omitted. Can be anywhere in the program, either before or after. e.g. goto 23, goto test The first would goto line 23. The second would goto a label called test. q 1010100000 QUIZ ON GOTO Which of the following are valid statements Type 1 if it is valid, 0 if it is not (1) goto 23 (6) goto 2a (2) goto 23: (7) goto print (3) goto test (8) goto (4) goto test: (9) goto x (5) goto a1 (10) goto 2+3 GOSUB ..RETURN This branches to a given label and then returns to where control was before branching to this label on encountering a RETURN statement. This allows one section in the program to be reached from different points in the program (and executed any number of times). The label may be followed by any number of statements which must be followed by a return statement. The program code between the label and the return statement is called a subroutine. You may return to somewhere different in the program by using a RETURN label/linenumber statement. If a return is reached without being preceded by a gosub, there will be an error message. q 1011100010 QUIZ ON GOSUB Which of the following are true statements Type (1) A gosub statement can branch to a label (2) A return statement is optional (3) A gosub statement cannot exist without a return statement (4) RETURN should not be reached, except via GOSUB (5) There may be any number of statements between the label and RETURN (6) There will be an equal number of RETURNs and GOSUBS in a program (7) A return statement can be before its corresponding gosub statement (8) A subroutine can only be reached from one point in the program (9) A return statement returns control to the line after the gosub (10) A gosub cannot branch to a line number IF THEN Branches to a line number or label depending whether a given condition is true or not. e.g IF x=3 then check If the condition is not true control continues at the next line or performs a function. e.g. IF x=3 THEN y=6 A variable can be compared with another variable e.g. IF x=y THEN ... or a constant LOGICAL OPERATORS The following symbols can be used to compare 2 numbers: = is equal to > is greater than < is less than <> is not equal to >= is equal to or greater than =< is equal to or less than q 431526 QUIZ ON COMPARISIONS Match the symbol with the description (1) <> (1) is equal to (2) < (2) is greater than (3) = (3) is less than (4) >= (4) is not equal to (5) > (5) is equal to or greater than (6) =< (6) is equal to or less than q 3146124517 QUIZ ON IF THEN Type the value of y a=6:b=9:c=4:d=3:e=7:f=2:y=1 (1) if c>d then y=3 (6)if d=d then y=4 (3) if ce then y=6 (9) if c>e then y=6 (5) if c>=b then y=7 (10) if b>=a then y=7 NOT Branching(or the function) occurs, if the given condition is not true. e.g. IF NOT X=3 THEN CHECK ELSE Branches(or performs a function) if the given condition was not true. e.g.if a$="y" then yes else no q 01101 QUIZ ON ELSE True or false (1) The block after the ELSE is always executed (2) ELSE can the only word on a line (3) An ELSE must be after a corresponding IF..THEN statement (4) ELSE cannot be followed by a linenumber (5) ELSE can be followed by a label IF THEN ..END IF Allows a block of actions to be performed if a given condition is true. e.g. if x=6 then y=7 z=8 END IF Notice there is nothing on the same line after the then. The lines between the if and the if then will be ignored if the given condition is false. Each END IF statement must correspond with a IF THEN statement. q 10101 QUIZ ON IF THEN...END IF True or false (1) In practice, an IF THEN..END IF block can be any size (2) END IF does not need to be used with IF THEN (3) The THEN must be the last word on that line (4) The lines after the IF..THEN are always executed (5) the END IF are the only words in that statement ON .. GOTO Branches to a different line number depending on the value of a variable. e.g. on x goto 21,22,23 Labels can be used instead of line numbers. Any numeric variable can be used instead of x. if x is greater or less than the number of labels/line numbers you will get an error message. There may be many labels or line numbers. e.g. on total goto walk,run,jump If x=1 you goto the first label/line number If x=2 you goto the second label/line number If x=3 you goto the third label/line number ON .. GOSUB This is very similar to ON .. GOTO but branches to a subroutine rather than a label or linenumber. You return to the line after the ON .. GOSUB statement (unless you used a RETURN label statement). q 1100011 QUIZ ON ON.X..GOTO AND ON X ..GOSUB True or false (1) Either linenumbers or labels can be used (2) Any numeric variable can be used instead of x (3) Any string variable can be used instead of x (4) X must be used (5) Only linenumbers can be used (6) This can be more efficient than using IF THEN (7) ON GOSUB branches to a subroutine NESTING This means to have one action being performed in the middle of another action. (Examples explain this better). Branching can be nested. Statements may be of the same type. e.g. if x=1 then if y=2 then z=3 END IF END IF Or the statements may be of different types. LOOPS Allow the same procedure to be repeated. Loops are started with a certain statement and must end with a corresponding statement. Several different statements may be between the start and end of a loop matching pairs are for example; FOR NEXT WHILE WEND FOR NEXT Performs the same function for increasing values of a variable. A loop will be passed through until the maximum value(the number after the TO) is reached. e.g. for x=1 to 3 PRINT x next This loop will be passed through three times The first time x will be 1 The second time x will be 2 The third time x will be 3 A loop will be passed through a minmum of one time, e.g. for x=1 to 1 PRINT x next will pass through the loop once. Any variable(in place of x) can be used. Starting and ending values can be any 2 numbers(even minus). The amount the variable is changed is called the increment. The default of the increment is 1 but can be altered by using STEP. The increment can be any value,even minus. e.g. for x=50 to -100 step -10 PRINT x NEXT q 10011 QUIZ ON FOR...NEXT true or false (1) When the variable reaches the maximum value, the loop stops (2) The value of the increment is always one (3) The variable used must be x (4) All lines between the FOR and the NEXT are executed (5) The increment can be negative WHILE ... WEND The loop is repeated while a certain condition is true. When the condition is no longer true the program continues at the next line. If the condition is never true the loop will not be executed at all. The block must be followed by WEND. e.g. while x>6 x=X+1 ? x wend WHILE x>6:x=x+1:PRINT x:WEND WHILE 1 continues indefinitely as the condition "1" is always true. q 00111 QUIZ ON WHILE...WEND How many times will the following loops be passed through? (1) A WHILE statement must be immediately followed by a WEND (2) The loop will be executed at least once (3) If the condition is true, the next block will be executed (4) WHILE 1 -is always true (5) Every WHILE statement must have a corresponding WEND statement BRANCHING OUT OF LOOPS Control may leave a loop before it is finished e.g. for x=1 to 12 PRINT x goto pass Next loops NESTING LOOPS Loop FOR NEXT,WHILE WEND,IF THEN,IF THEN..END IF statements can all be nested together. q 10001 QUIZ ON NESTING True or false Type 1 for true 0 for false (1) Nesting means to have one loop inside another (2) Nested loops can only be few in number (3) Loops can overlap (4) Different kinds of loops cannot be nested (5) Program control may leave a loop before the end is reached