# Programming - AQA Computer Science Cheat Sheet by [deleted]

### Structured Progra­mming

 The stru­ctured approach to progra­mming means that code is more logical and readable, making it easier to debug and maintain. It also makes it more efficient. The most important part of this approach is modu­larised progra­mmi­ng, which is the use of subrou­tines. Other components include detailed code annota­tion and clearly named variables.

### Data Types

 Data Type Defi­nit­ion Exam­ple­(s) Inte­ger A whole, positive number. 134 Real or Float Any number, including decimals and negatives. -19.21 Char­acter A letter, number, space, etc. that can be typed, in accordance with ASCII or Unicode. @ String A string of charac­ters. "­Hello, world!­" Bool­ean Logical values based on binary (1 and 0). True, False
Data Type: A specific type of value (and variable) that must be handled in a certain way.

### Relational Operators

 Symbol Mean­ing = equal to ≠ not equal to < less than > greater than ≤ less than or equal to ≥ greater than or equal to

### Maths (in Python)

 Oper­ation Exam­ple Addition Pseu­doc­ode: 11 + 2 = 13 Python example: `11 + 2 = 13` Subtra­ction Pseu­doc­ode: 11 - 2 = 9 Python example: `11 - 2 = 9` Multip­lic­ation Pseu­doc­ode: 11 x 2 = 22 Python example: `11 * 2 = 22` Real or Float Division Pseu­doc­ode: 11 DIV 2 = 5.5 Python example: `11 / 2 = 5.5` Integer Division Pseu­doc­ode: 11 // 2 = 5 Python example: `11 // 2 = 5` Remainder of a Division Pseu­doc­ode: 11 MOD 2 = 1 Python example: `11 % 2 = 1`

### Selection

 Sele­ction is when a program makes a decision, usually taking the form of IF statement. For example (pseud­ocode): `IF x = TRUE THEN` ` PRINT "­Yes."` `ELSE` ` PRINT "­No."­`

### Variables

 What is the 'scope' of a variable?The part of the program where a variable is valid and access­ible. Why is it important to give variables distin­ctive identi­fiers?This makes debugging and mainte­nance easier, as it is easier for the programmer to understand the purpose of the variable.

### Robust & Secure Progra­mming

 There are two main ways to make programs more robust. Firstly, using data valida­tion, which is most easily done using an IF/ELI­F/ELSE statement or a TRY/EXCEPT statement. This makes sure the data inputted by the user is valid, preventing a runtime error. Secondly, auth­ent­ica­tion. This primarily takes the form of passwords. However, test­ing is also key in ensuring that a program is robust. A programmer must test typical (normal), boundary (extreme), and erroneous data to guarantee that the program deals with data correctly.

### Data Structures

 What are data struct­ures?A data structure is a way of storing data. What is an array?An array is a collection of related data (of the same data type). Each piece of data is an elem­ent with a specific index. They may also be called 'lists'. What is a two dimens­ional array?Essent­ially, a 2D array is just an array made of arrays. They can be thought of as a matrix. Possible uses include a bitmap for an image. What is a reco­rd?A record is very similar to an array, except multiple data types can be stored together.

### Progra­mming Languages

 High-Level Languages vs Low-Level LanguagesMost programs are initially written in high-level progra­mming languages (e.g. Python) because these are more like human languages, meaning that they are easier for progra­mmers to unders­tand, code in, and debug. They are said to provide a 'higher level of abstra­ction' from machine code. Low-level langages are very different and much more difficult. Despite this, progra­mming in machine code allows for the optimi­sation of code and avoid code having to be transl­ated. Machine Code vs Assembly LanguageBoth machine code and assembly language are low-level languages, with assembly code having a 1:1 corres­pon­dance with machine code. However, each type of processor has its own specific machine code instru­ction set, which is expressed in binary. Assembly language is generally used for software for embedded systems and for contro­lling specific hardware compon­ents. Transl­ationAll programs written in high-level languages or assembly language must be translated into machine code before they can be run. Interp­retors, compilers, and assemblersAn asse­mbler translates assembly language to machine code. An inte­rpr­eta­tor translates high-level languages into machine code. It does so line­-by­-li­ne, which makes debugging easier. A comp­iler also translates high-level languages to machine code, but it tran­slates the whle program before running. While the compli­ation process is slow, the machine code can then be stored and run quickly in future.

### Types of Iteration

 What is definite iterat­ion?There is a set number of iterat­ions. Example of definite iteration (Python):`for i in range(5):   x = x + 1` What is inde­finite iterat­ion?There is not set number of iterat­ions, as it is instead dependent on when a certain condition becomes true. Example of indefinite iteration (Python):`while x < 5:   x = x + 1` What is nested iterat­ion?A loop (itera­tion) within a loop. Examples of nested iteration (Python):`while n == False:   for i in range(5):     x = x + 1`
Note that selection statements can also be nested.

### Subrou­tines

 What is are subr­out­ines?Named ‘out of line’ blocks of code that are executed (called) by writing its name in a program statement. Advantages of subrou­tines1. It makes programs more effici­ent, because blocks of code can be reused. 2. It means that code is easier to debug, because it is shorter. Para­met­ersA value that is passed to a subrou­tine. These may also be called 'argum­ents'. Proc­edu­res vs Func­tionsBoth take parame­ters, but functions return a value, whereas procedures don't. Local Variab­lesSubrou­tines can declare local variables, the scope of which is limited to the subrou­tine. Using local variables is good practice because it allows the program to be simpler. Global variab­les, on the other hand, make a program much more complex, as they may change freque­ntly.

### String Handling

 Pseu­doc­ode Python Example Purp­ose LEN() Find the length of a string. POSITION() print(­str­ing­_to­_se­arc­h.f­ind­("a")) To find the position (similar to an index) of a specific character in a string. If the character appears multiple times, only the first instance will be returned. SUBSTR­ING­(In­tExp, IntExp, StringExp) To create a substring. The first paramter indicates the start of the substring, the second the end, and the final parameter being the string itself. + firstname + lastname To concat­enate (join) two strings together. ORD() ASCIIcode = ord('a') To convert a character to character code. CHR() character = chr(97) To convert character code to character. STR() str(bi­rth­date) To convert a string to an integer. INT() int(ph­one­number) To convert an integer to a string.
Note that the pseudocode is not set. Different people will write their psuedocode differ­ently.

### Input, Output, and File Handling (Python)

 In Python, the input() function allows a program to receive data from the user. Similarly, print() allows the program to output inform­ation to the user. File handling is slightly more complex. A typical file-o­pening statement would take this format: `file_­object = open(“­fil­ename”, “mode”)` The 'mode' could be 'read' ('r' - allows access only), 'write' ('w' - allows access and editing), or 'appen­ding' ('a' - allows inform­ation to be added to the end of the file. Once the program has finished using a file, the "­fil­ena­me".c­lose() function should be used, as this frees up resources.

### Random Number Generation

 In Python, random number generation requires the import­ation of the RANDOM module. For example: `import random` `x = random.ra­ndi­nt(­0,10)` `print(x)`

4 Pages
//media.cheatography.com/storage/thumb/deleted-56036_programming-aqa-computer-science.750.jpg

PDF (recommended)