0% found this document useful (0 votes)
29 views10 pages

CS2

CS2

Uploaded by

Daniyal Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

CS2

CS2

Uploaded by

Daniyal Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
eV AA ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° o ° ° ° ° ° ° ° ° ° Important words You will ned to know and understand the following terms integer real numbers Boolean character string variable constants identifier declared assignment sequence selection iteration looping definite iteration nested selection nested iteration arithmetic operators ‘modulus (MOD) integer (DIV) relational operators comparison operators Boolean operators data structure array record field names string handling substring length (string) character code concatenation string conversion random numbers subroutines call (subroutines) parameters return values (subroutines) local variables 2.1 Data types | Integers are whole numbers, positive or negative, that have no __decimmal or fractional part. For example, 99 is an integer The real data type is used for numbers, positive or negative, that have (or may have) a decimal or fractional part. For example, 18:79 ie a real number. Boolean variables only ever store True or False values. For example, True is a Boolean value. A character is a single item from the character set used on ‘the computer. For example, @ is a character. When assigning a character to a variable, quotation marks are required. ‘A string data type stores a collection of characters. For example, “Hello world” is.a string. When assigning a string to a variable, quotation marks are required. 2.2 Programming concepts Variables, constants and assignments m_Avariable stores a single piece of data. It is a label for an allocated area of memory. The value of a variable can be changed during the execution of the program A constant is also a label for an allocated area of memory. Unlike a variable however, the value of a constant cannot change during the execution of the program. Variables and constants are given an identifier (or name). Their identifiers can be almost anything but must: not contain spaces not start with a number | = not be particular worde reserved for use in the programming language. In most high-level languages, variables and constants are declared (defined) at the beginning of the program. (Note, however, that Python does not use declarations.) Variables and constants have values assigned to their identifiers. = This io done using the =" operator in Python, VB.Net and C#. =_This done using the ‘~’ operator in AQA Peeudo-code. Variables can be assigned new values throughout a program, Important words which overwrite the previous value. modularised Constants can only be assigned a value once. programming = Sequence and seleciion robust program aa cee. A para ri trot ap tenon bark otro authentication completes fuly before the next one is executed. testing test data rornnel cess jlien program. These decisions are based on Boolean conditions boundary test data and the program then takes one of two paths based on this erroneous test data scion. syntax error logic error [ive Code to execute if Code to execute it ‘condition is True condition i False Selection can be implemented using TF, BLSE and ELSE IF statements: name_<— USERINPUT. IP name_= ‘George’ THEN. OUTPUT ‘Hello George’ ELSE IF name = ‘Lorne’ THEN OUTPUT ‘Great work Lorne’ ELSE IF name = ‘Kirstie! THEN OUTPUT ‘Nice to see you again’ ELSE OUTPUT ‘Hello stranger’ ENDIF Iteration Iteration is used to repeat sections of code. Iteration is commonly called looping. ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° Definite iteration repeats code a defined number of times. FOR loops can be used to implement count-controlled iteration. A step can also be defined. For example: FOR p «1 To 10 oureuT_p * a ENDFOR, Indefinite iteration checks a condition each time around the loop and decides whether to repeat the code again or continue. WHILE loops and REPEAT UNTIL loops can be used to implement condition-controlled iteration. [aa total =O total —O WHILE total < 20 REPEAT num — USERINPUT num <— USERINPUT total — total + num total — total + num ENDWHILE UNTIL total 2 20 OUTPUT done!" OUTPUT done!” Note that the WHILE loop will repeat while the total is less than 20 whereas the REPEAT UNTIL loop will repeat until the total is larger than or equal to 20. They produce exactly the same results but check different conditions at different times in the code. Nested selection and iteration Nesting is where multiple iteration or selection constructs are used inside each other. The following code nests an IF statement inside a FOR loop, meaning that the question is asked and evaluated ten times. FOR x — 1 70 10 OUTPUT ‘Enter a number! num — USERINPUT IF num MOD 2 = 0 THEN ouTpuT ‘even’ ELSE OUTPUT ‘odd’ ENDIF ENDFOR ° ° oO ° ° ° ° ° ° ° oO ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° 2.3 Arithmetic operations A computer program uses operators to perform some sort of action, Arithmetic operators can be used to carry out basic mathematical operations on numeric values. Cie Eo Gene Addition Subtraction Multiplication Division Modulus — returns the remainder r—5M0D2 after divigion would give the result that r=1 Integer returns the whole number |q<-5 DIV2 after divigion would give the reoult: that: 4 = 2 2.4 Relational operations Relational (or comparison) operators are used to evaluate expressions toa Boolean True or False outcome. = Equal to * Not equal to < Less than = Less than or equal to Greater than 2 Greater than or equal to 2.5 Boolean operations Boolean operators allow multiple conditions to be evaluated. The AND operator requires both conditions to be True for the overall condition to be True. m_The OR operator requires one or the other (or both) of the conditions to be True for the overall condition to be True. The NOT operator reverses the True or False outcome from a comparico 2.6 Data structures =A variable can be used to store a single item of datain a computer program. m_Data structures allow a programmer to store multiple items of data under a single identifier. = Common data structures are arrays and records. 2 Programming 9999900000009090099900900999099099990909 1D and 2D arrays A1-dimensional array allows a programmer to store multiple items of data under a single identifier. Array index [J 1 2 3 4 Pa ‘hello’ ‘world’ *how! ‘are! ‘you? The above 1D array can be created using the following code in AQA Pseudo-code: phra: *you?') A 2dimensional array allows a programmer to store multiple items of data using two identifiers = _[thello', ‘world’, ‘how!, ‘ars It.can be represented in a table form as shown belaw: Any exam question using a table for this wil tell you whether you access the array as [row, column] or [column, row]. Lists Python does not have simple arrays. Instead, it uses another data structure concept called a list. ists are.similar in operation to arrays but. have three key differences. Lists can contain a mix of different. data types. a_Lists are not of a fixed size and can be added to or reduced in size during the running of the program. There is no such thing as 2D list — however, you can create lists within lists that. perform the. same function as a 2D array. The use of records to store data = Arecord is.a data structure that allows multiple data items to be stored, using field names to identify each item of data. = Tocreate a record, we must first define the field names that will make up each record. a_We can then store data under these field names in a database management. system using a table. For example: Table called ‘Student’ showing three records, Snort a eae Bradley Jenkins 9 bjenkins @notreal.co.uk Ghita Cable 10 [email protected] Charlotte Pegg 9 | [email protected] ° ° ° ° ° ° ts) ° ° ° ° ° ° a) ° ° ° ° ° ° ° ° fs) ° ° ° ° ° ° ° ° ° ° ° ° 2.7 Input and output [tis useful to be able to input data from a user ard output data back to the user. = The keywords USERINPUT atid OUTPUT are used for these purposes in AGA Pecudo-code. 2.8 String-handling operations Length, substring ana character codes ‘The following table gives the AQA Paeudo-code for the following string-| LEN (string) To count how many characters are contained in a string name — ‘Seth Bottomley’ LEN(name) aives 14 SUBSTRING (xy, string) To extract characters from the middle of a string where x is the ‘starting point (beginning at 0) and 'y is the end point (beginning at 0). SUBSTRING (2,6,name) gives ‘th Bo! SUBSTRING (0,2,name) gives ‘Set CHAR_T0_CODE(char) Tofind the ASCII value of a character| CHAR_TO_CODE('D’) gives 6B CODE _TO_CHAR(int) To find the character that relates to the integer ASCII value given. (For| more on ASCII see section 3.5.) ‘CODE_TO_CHAR(68) gives “D” Concatenation = _Concatenation means joining multiple strings together. = _This is done using the + operator. When strings are concatenated, they are joined together in tthe order given: texta — ‘two! textb — ‘words! new < texta + text OUTPUT (new) The above code would join together the two strings to print out: ‘twowords. String/type conversion = String conversion (or casting) means to convert data from or to the string data type. The follo keyworde are used in AGA Paeudo-code: ° ° ° o ° ° ° ° ° ° ° ° ° ° ° ° ° ° o ° ° ° ° ° ° ° o oO ° ° ° ° ° ° ° na STRING_TO_INTO. | a<— STRING_TO_INT(123') | Converts a value stored as a string b — STRING 10_INT(‘7) ton integer STRING_TO_REAL() | c—STRING TO_REAL ("12.9") | Converts a value stored as a tring d —STRING_TO_REAL (46) __| toa real/floating point number. INT_TO_STRING() @ — INT_TO_STRING(17) Converts a value stored as an f — INT_TO_STRING(I40) integer to a string. REAL_TO_STRING() |g REAL_TO_STRING(I7Z2) | Converts a value stored as a real! he REAL_T0_STRING(3.8) _| floating point: number to a string. Not all values can be converted to the string data type! 2.9 Random number generation = Random numbers can be generated by a programming language. = Tyrically, upper and lower limits to the random number are passed in as parameters and a _random value between these two limits is returned by the eubroutine. In AQA Peeudo-code, the RANDOM_INT(x, y) subroutine allows us to do this. Programmers can pase in the lowest and highest number required as arguments (in brackets): RANDOM_INT(1, 5) chooses a random integer between 1 and 5 RANDOM_INT(20, 30) #chooses a random integer between 20 and 30 2.10 Structured programming and subroutines It is good practice to break programs down into subroutines. A subroutine is a section of code that is defined outside the main body of the program. It is given its own identifier which is then used to call the subroutine as many times as required. When a subroutine is called, control passes from the main program to the subroutine. Once: the subroutine has completed, control is passed back to the main program. Here is a subroutine and a call from the main program: fisubroutine definition with parameters SUBROUTINE timestable(tt, nuns) FOR x — 1 70 nuns ourpur tt * x ENDFOR ENDSUBROUTINE ° ° ° ° ° ° ° ° ° ° oO ° ° o ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° call the subroutine to run timestable(8, 10) timestable(9, 12) This subroutine is called timestable. The values in brackets are parameters that canbe passed from the main program into the subroutine. Some subroutines return a value back to the main program when it returns control. This value can then be stored, printed or otherwise used in the main program. For example: #subroutine definition that returns a value SUBROUTINE circle_area (radius! const pi — 3.14159 area — pi * radius*2 The area of a circle is calculated in this subroutine and passed to the variable new inthe main program. This is done by using the RETURN keyword, Local variables Variables used in the subroutine are called local variables. Local variables only exist within the subroutine and cannot be called by the main program. It is good practice use local variables because any errors associated with them will be limited ‘to the subroutine rather than across the whole program ~ this makes code easier to debug. Advantages of the structured approach to programming Structured programming is an approach that: involves modularised programming, using clear well-documented interfaces and retuming values from subroutines. Splitting programs down into multiple subroutines rather than having one large program is known a9 modularised programming. Awell-documented interface for a subroutine meane that parameters are passed in rather than values being asked for from the user, local variables are used to complete any processing and the overall result is returned back to the main program using the RETURN keyword. ‘The main advantages of the structured approach to programming include: Fe 6 the overall size of the program a6 joes not repeated in multiple places. _ = Makes the code much easier to maintain as it is easier to read and understand the purpose of each subroutine. = Reduces development and testing time as code is much easier to write and debug. a Allows reuse of code between programs, especially where pre-written and pre-teated ‘subroutines can be used. A function is a subroutine that returns a value. A procedure io a subroutine that does not retum a value. 2.11 Robust and secure programming _Arobust program will anticipate unusual user behaviour and still function correctly. =_A secure program is one that only allows authorised users to access it. Robust and secure programs oan be written using the following techniques. Data validation ‘This is used to ensure that users have entered data in the correct form, for instance: of the correct type within a sensible range of the correct length not empty. Authentication This is used to ensure that only authorised users access a system by establishing a user's identity. This can be one in a number of ways: using usernames and passwords through possession of an electronic key, device or account. using biometrics. ‘Two-factor authentication is where two of the above are checked simultaneously. Testing The purpose of testing is to ensure that the program functions 26 expected and meets all __ requirements. = Testing should not simply aim to prove that the program works, but also try to break it. Only by knowing that it cannot easily be broken can we be satisfied that it works. Test data chould be chosen 90 that the system as a whole can be tested destructively, checking for errore wherever they may occur. Test data should be chosen to include ae many of ‘the following as possible: - = Normal test data is data of the correct type that would typically be expected from a user who is correctly using the system. This should be accepted by the program without causing errors. o ° ° ° ° o ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° oO ° o ° ° ° ° ° ° ° ° oO ° ° ° ° ° ° ° °o o ° ° ° oO ° ° ° ° ° ° = Boundary test data is test data that is of the correct type but is on the very edge of being valid. Boundary test data should be accepted by the program without causing errors. = Erroneous test data is test data that is outside the expected values or of the incorrect type, __and should be rejected by the system. A test plan liste all of the teste that will be carried out, the expected reeult and the actual result in each case. For example: Normal Boundary Erroneous A syntax error is one that breaks the grammatical rules of the programming language. Examples include misspelling a keyword, missing a bracket or using a keyword in the wrong way. Syntax errors will stop the program from running. A logic error is one that: causes the program to produce an unexpected or incorrect output but will not stop the program from running,

You might also like