0478 Computer Science
Chpater 1 Number System
1.1 Number systems
1.2 Text, sound and images
1.3 Data storage and compression
Chapter 2 Data transmission
2.1 Types and methods of data transmission
2.2 Methods of error detection
2.3 Encryption
Chapter 3 Hardware
3.1 Computer architecture
3.2 Input and output devices
3.3 Data storage
3.4 Network hardware
Chapter 4 Software
4.1 Types of software and interrupts
4.2 Types of programming language, translators and integrated development environments (IDEs)
Chapter 5 The internet and its uses
5.1 The internet and the World Wide Web (WWW)
5.2 Digital currency
5.3 Cyber security
Chapter 6 Automated and emerging technologies
6.1 Automated systems
6.2 Robotics
6.3 Artificial intelligence (AI)
Chapter 7 Algorithm design and problem solving
7.1 The program development life cycle
7.2 Computer systems, sub-systems anddecomposition
未命名
未命名
Chapter 8 Programming
8.1 Programming concepts
8.2 Arrays
Mr. Theo
-
+
首页
8.1 Programming concepts
## 8.1.2 Basic Data Types In order for a computer system to process and store data effectively, different kinds of data are formally given different types. This enables: - data to be stored in an appropriate way, for example, as numbers or as characters - data to be manipulated effectively, for example, numbers with mathematical - operators and characters with concatenation - automatic validation in some cases. The basic data types you will need to use for IGCSE Computer Science are: | Data Type | Explanation | Example | | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------: | | `INTEGER` | a positive or negative whole number. <br>can be used with mathematical operators. | `5, -3` | | `REAL` | a positive or negative number with a fractional part. <br>can be used with mathematical operators. | `4.7, 0.3, -4.0, 0.0` | | `CHAR` | a variable or constant that is a single character. | `‘x’, 'C', '@'` | | `STRING` | a variable or constant that is several characters in length.<br>Strings vary in length and may even have no characters (known as an empty string)<br>the characters can be letters and/or digits and/or any other printable symbol.<br>(If a number is stored as a string then it cannot be used in calculations.) | `“This is a string”, ""` | | `BOOLEAN` | a variable or constant that can have only two values TRUE or FALSE. | `TRUE, FALSE` | ## 8.1.1 Identifiers - Variables and Constants ### Identifiers the names given to variables, constants, procedures and functions #### Pseudocode identifiers conventions: - An identifier must start with a letter or the underscore character - An identifier cannot start with a number - An identifier name can only contain alpha-numeric characters (A-z, 0-9) - Identifiers are case-sensitive (age, Age and AGE are three different variables) - An identifier cannot be any of the pseudocode keywords. #### Python identifiers conventions: - A identifier must start with a letter or the underscore character - A identifier cannot start with a number - A identifier name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) - Identifiers are case-sensitive (age, Age and AGE are three different variables) - An identifier cannot be any of the Python keywords. ### Assignments | | Pseudocode | Python | | -------- | ------------------------ | ------------------------ | | Operator | `←` | `=` | | Format | `<identifier> ← <value>` | `<identifier> = <value>` | The identifier must refer to a variable (this can be an individual element in a data structure such as an array or an abstract data type). The value may be any expression that evaluates to a value of the same data type as the variable. #### Example: ```pseudocode Counter ← 0 Counter ← Counter + 1 TotalToPay ← NumberOfHours * HourlyRate ``` ```python Counter = 0 Counter = Counter + 1 TotalToPay = NumberOfHours * HourlyRate ``` ### Variables and Constants - A **variable** within a computer program refers to a named storage unit with a value that can be modified throughout the program's execution. To enhance comprehension for others, it is advisable to assign significant names to variables. ```pseudocode DECLARE FirstVar : INTEGER DECLARE SecondVar : INTEGER ``` ```python #python don't need to declare data type of variables. ``` - A **constant** within a computer program represents a named storage unit that holds a value which remains unchanged throughout the program's execution. Similar to variables, it is recommended to assign meaningful names to constants to enhance comprehensibility for others. ```pseudocode CONSTANT FirstConst <- 500 CONSTANT SecondConst <- 100 ``` ```python FirstConst = 500 SecondConst = 100 ``` ## 8.1.3 Input and Output - Programs require input and output statements to handle data. - In IGCSE Computer Science, algorithms and programs are designed to take input from a keyboard and output to a screen. - Prompting the user with clear instructions for input is necessary for the user to understand what is expected. - Users should be provided with information about the output/results for a program to be useful. - Each output should be accompanied by a message explaining the result's meaning or significance. - If an output statement has multiple parts, they can be separated by a separator character. ### Input #### pseudocode Values are input using the `INPUT` command as follows: `INPUT <identifier>` #### python Values are input using the `input()` command as follows: `<identifier> = input(<prompt>)` *prompt - A String, representing a default message before the input. Can be empty. The identifier should be a variable (that may be an individual element of a data structure such as an array, or a custom data type). ### Output #### pseudocode Values are output using the `OUTPUT` command as follows: `OUTPUT <value(s)>` #### python Values are output using the `print` command as follows: `print(<value(s)>)` Several values, separated by commas, can be output using the same command. #### Example: ```pseudocode DECLARE name : STRING OUTPUT "Please enter your name:" INPUT name OUTPUT "Hi", name, "! How are you?" ``` ```python print("Please enter your name:") name = input() print("Hi", name, "! How are you?") ``` #### Outcome:  #### Example ```python name = input("Please enter your name:") print("Hi", name, "! How are you?") ``` #### Outcome:  #### python - convert value to integer or real number data types - Input data in programming languages must match the required data type of the variable where it will be stored. - By default, inputs are treated as strings, but commands can convert input to integer or real number data types. ##### Covert value to integer Values are converted to integer using the `int()` command as follows: `int(<values>)` ##### Covert value to real Values are converted to real using the `float()` command as follows: `float(<values>)` ###### Example: ```python radius = float(input("Please enter the radius of the cylinder")) print(radius) radius = int(input("Please enter the radius of the cylinder")) print(radius) ``` ###### Outcome:  ### Mathematical operators | Pseudocode Operator | Python Operator | Action | | ------------------- | --------------- | --------------------- | | `+` | `+` | Addition | | `-` | `-` | Subtraction | | `*` | `*` | Multiply | | `/` | `/` | Divide | | `^` | `**` | Raise to the power of | | `()` | `()` | Group | | `MOD` | `%` | Get remainder | | `DIV` | `//` | Get quotient | #### Example: ```pseudocode DECLARE a,b : INTEGER a <- 2 b <- 6 OUTPUT a + b OUTPUT a - b OUTPUT a * b OUTPUT a / b OUTPUT a ^ b OUTPUT MOD(a,b) OUTPUT DIV(a,b) ``` ```python a = 2 b = 6 print(a + b) print(a - b) print(a * b) print(a / b) print(a ** b) print(a % b) print(a // b) ``` #### Outcome:  ## 8.1.4 Programming Fundamentals When writing the steps required to solve a problem, the following concepts need to be used and understood: - sequence - selection - iteration - counting and totalling - string handling - use of operators ### 8.1.4(a) Sequence - The ordering of the steps in an algorithm is very important. An incorrect order can lead to incorrect results and/or extra steps that are not required by the task. #### Examples of the complete programs are shown below: ```pseudocode CONSTANT PI <- 3.142 OUTPUT "Please enter the radius of the cylinder" INPUT Radius OUTPUT "Please enter the length of the cylinder" INPUT Length Volume <- Radius * Radius * Length * PI OUTPUT "Volume of the cylinder is", Volume ``` ```python CONSTPI = 3.142 Radius = float(input("Please enter the radius of the cylinder")) Length = float(input("Please enter the length of the cylinder")) Volume = Radius * Radius * Length * CONSTPI print("Volume of the cylinder is", Volume) ``` ### 8.1.4(b) Selection - Selection is a very useful technique, allowing different routes through the steps of a program. #### Logic operators | Action | Pseudocode operator | Python operator | | ------------------------ | ------------------- | --------------- | | Equals | `a = b` | `a == b` | | Not Equals | `a <> b` | `a != b` | | Less than | `a < b` | `a < b` | | Less than or equal to | `a <= b` | `a <= b` | | Greater than | `a > b` | `a > b` | | Greater than or equal to | `a >= b` | `a >= b` | | Both | `AND` | `and` | | Either | `OR` | `or` | | Not | `NOT` | `not` | #### IF statements `IF` statements may or may not have an `ELSE` clause. `IF` statements without an else clause are written as follows: ```pseudocode IF <condition> THEN <statements> ENDIF ``` ```python if <condition>: <statements> ``` ##### Example: ```pseudocode IF Age > 17 THEN OUTPUT "You are an adult" ENDIF ``` ```python if Age > 17: print ("You are an adult") ``` `IF` statements with an else clause are written as follows: ```pseudocode IF <condition> THEN <statements> ELSE <statements> ENDIF ``` ```python if <condition>: <statements> else <statements> ``` Note in pseudocode, that the `THEN` and `ELSE` clauses are only indented by two spaces. (They are, in a sense, a continuation of the IF statement rather than separate statements). ##### Example: ```pseudocode IF Age > 17 THEN OUTPUT "You are an adult" ELSE OUTPUT "You are a child" ENDIF ``` ```python if Age > 17: print ("You are an adult") else: print ("You are a child") ``` #### Case statements - Case statements are used when there are multiple choices to be made. `CASE` statements allow one out of several branches of code to be executed, depending on the value of a variable. `CASE` statements are written as follows: ```pseudocode CASE OF <identifier> <value 1> : <statement> <value 2> : <statement> ... ENDCASE ``` An `OTHERWISE` clause can be the last case: ```pseudocode CASE OF <identifier> <value 1> : <statement> <value 2> : <statement> ... OTHERWISE <statement> ENDCASE ``` ##### Example: ```pseudocode DECLARE Grade : STRING INPUT Grade CASE OF Grade A : OUTPUT "Excellent" B : OUTPUT "Not bad huh" C : OUTPUT "Fair" D : OUTPUT "Work harder" OTHERWISE : OUTPUT "Invalid input" ENDCASE ``` Python does not have a CASE statement. Alternatively, the statement "elif" (stands for else-if) is used. ```python Grade = input( "Enter your grade") if Grade == "A": print( "Excellent" ) elif Grade == "B": print( "Not bad huh" ) elif Grade == "C" : print( "Fair") elif Grade == "D": print( "Work harder") else: print( "Invalid input") ``` ### 8.1.4(c) Iteration #### Count-controlled (FOR) loops - For a set number of iterations ##### Pseudocode Count-controlled loops are written as follows: ```pseudocode FOR <identifier> ← <value1> TO <value2> <statements> NEXT <identifier> ``` The identifier must be a variable of data type INTEGER, and the values should be expressions that evaluate to integers. The variable is assigned each of the integer values from value1 to value2 inclusive, running the statements inside the `FOR` loop after each assignment. If `value1 = value2` the statements will be executed once, and if `value1 > value2` the statements will not be executed. ###### Example: ```pseudocode DECLARE Counter : INTEGER FOR Counter <- 1 TO 10 OUTPUT Counter NEXT Counter ``` ##### Outcome:  An increment can be specified as follows: ```pseudocode FOR <identifier> ← <value1> TO <value2> STEP <increment> <statements> NEXT <identifier> ``` The increment must be an expression that evaluates to an integer. In this case the identifier will be assigned the values from value1 in successive increments of increment until it reaches value2. If it goes past value2, the loop terminates. The increment can be negative. ###### Example: ```pseudocode DECLARE Counter : INTEGER FOR Counter <- 1 TO 10 STEP 2 OUTPUT Counter NEXT Counter ``` ###### Outcome:  ##### Python To loop through a set of code a specified number of times, we can use the `range()` function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. ###### Example - Using the range() function: ```python for x in range(6): print(x) ``` ###### Outcome:  Note that range(6) is not the values of 0 to 6, but the values 0 to 5. The `range()` function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: `range(2, 6)`, which means values from 2 to 6 (but not including 6): ###### Example - Using the start parameter: ```python for x in range(2,6): print(x) ``` ###### Outcome:  The `range()` function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: `range(2, 30, 3)`: ###### Example - Increment the sequence with 3 (default is 1): ```python for x in range(2,30,3): print(x) ``` ###### Outcome:  #### Condition-controlled loops - may have no iterations ##### Pseudocode Post-condition loops are written as follows: ```pseudocode REPEAT <Statements> UNTIL <condition> ``` The condition must be an expression that evaluates to a Boolean. The statements in the loop will be executed at least once. The condition is tested after the statements are executed and if it evaluates to `TRUE` the loop terminates, otherwise the statements are executed again. ###### Example - REPEAT UNTIL statement ```pseudocode DECLARE Password : STRING REPEAT OUTPUT "Please enter the password" INPUT Password UNTIL Password = "Secret" ``` ###### Outcome:  ##### Python There is no such syntax. #### Pre-condition loops - Always has at least one iteration ##### Pseudocode Pre-condition loops are written as follows: ```pseudocode WHILE <condition> DO <statements> ENDWHILE ``` The condition must be an expression that evaluates to a Boolean. The condition is tested before the statements, and the statements will only be executed if the condition evaluates to `TRUE`. After the statements have been executed the condition is tested again. The loop terminates when the condition evaluates to `FALSE`. The statements will not be executed if, on the first test, the condition evaluates to `FALSE`. ###### Example – WHILE loop ```pseudocode DECLARE Number : INTEGER INPUT Number WHILE Number > 9 DO Number ← Number - 9 OUTPUT Number ENDWHILE ``` ###### Outcome  ##### Python ```python while <condition> : <statement> ``` With the while loop we can execute a set of statements as long as a condition is `true`. Example ```python Number = int(input()) while Number > 9: Number = Number - 9 print(Number) ``` ###### Outcome  ### 8.1.4(d) Totalling and counting #### Totalling Totalling is used to add one number to an existing stored number, usually contained in two different variables. ```pseudocode DECLARE total, counter: INTEGER total <- 0 FOR counter <- 1 TO 10 total <- total + counter NEXT counter ``` ```python total = 0 for counter in range(1, 11): total = total + counter print (total) ``` #### Counting Counting keeps a check on how many iterations a program has performed in a loop. ```pseudocode DECLARE count, counter: INTEGER count <- 0 FOR counter <- 1 TO 10 count <- count + 1 NEXT counter ``` ```python count = 0 for counter in range (1, 11): # count how many time this loop has run count = count + 1 print (count) ``` ### 8.1.4(e) String Handling Strings are used to store text. Every string contains a number of characters, from an empty string, which has no characters stored, to a maximum number specified by the programming language. The characters in a string can be labelled by position number. The first character in a string can be in position zero or position one, depending on the language. #### length finding the number of characters in the string. For example, the length of the string _"Computer Science"_ is 16 characters as spaces are counted as a character. length are written as follows: ```pseudocode DECLARE value : INTEGER value ← LENGTH("abcdefghijk") OUTPUT value #The value "11" will be output. ``` ```python text = "abcdefghijk" value = len(text) value1 = len("abcdefghijk") print(value) #11 print(value1) #11 ``` #### substring extracting part of a string. For example, the substring _"Science"_ could be extracted from _"Computer Science"_. substring are written as follows: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | a | b | c | d | e | f | g | h | i | j | k | In Pseudocode, index starts at 1. pseudocode - substring(one character) ```pseudocode DECLARE text, shorten_text : STRING text <- "abcdefghijk" shorten_text <- SUBSTRING(text, 1, 1) OUTPUT shorten_text # "a" ``` pseudocode - substring(more character) ```pseudocode DECLARE text, shorten_text : STRING text <- "abcdefghijk" shorten_text <- SUBSTRING(text, 1, 3) OUTPUT shorten_text # "abc" ``` | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | a | b | c | d | e | f | g | h | i | j | k | In Python, index starts at 0. python - substring(one character) ```python text = "abcdefghijk" value = text[0] value2 = text[1] print(value) # "a" print(value2) # "b" ``` python - substring(more character) syntax variable[x:y], x is the starting position and y is the ending ```python text = "abcdefghijk" value = text [0:3] value2 = text[2:7] print(value) # "abc" print(value2) # "cdefg" ``` #### upper converting all the letters in a string to uppercase. For example, the string "Computer Science" would become "COMPUTER SCIENCE". upper are written as follows: ```pseudocode DECLARE text, upper_text : STRING text <- "abcdefghijk" upper_text <- UCASE(text) OUTPUT upper_text # "ABCDEFGHIJK" ``` ```python text = "abcdefghijk" text = text.upper() print(text) # "ABCDEFGHIJK" ``` #### lower converting all the letters in a string to lowercase. For example, the string "Computer Science" would become "computer science". lower are written as follows: ```pseudocode DECLARE text, lower_text : STRING text <- "ABCDEFGHIJK" lower_text <- LCASE(text) OUTPUT lower_text # "abcdefghijk" ``` ```python text = "ABCDEFGHIJK" text = text.lower() print(text)# "abcdefghijk" ``` ## 8.1.5 Nested Statements Sometimes, it is necessary to execute one IF statement, followed by another, and then followed by another. This is when nested selection is needed. One type of construct can be nested within another – for example, selection can be nested within a condition-controlled loop, or one loop can be nested within another loop. ### Nested Selection Sometimes, it is necessary to execute one IF statement, followed by another, and then followed by another. This is when nested selection is needed. #### Normal IF-statement ## 8.1.6 Procedures, Functions and Parameters ## 8.1.7 Library Routines ## 8.1.8 Creating a Maintainable Program
Theo
2025年5月30日 12:14
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
Word文件
PDF文档
PDF文档(打印)
分享
链接
类型
密码
更新密码
有效期