測試版本平台正在積極開發中;可能有錯誤、缺少功能,以及資料遺失的風險。感謝你的支持!

Programming Concepts

邊玩邊學

回答這些題目賺取能量,接著就能釣魚、探索。不需要帳號。

給老師: 為 Programming Concepts(Computer Science、CIE)準備好可直接使用的課程投影片, 複習筆記——用於你的課程,或把這個主題當成互動班級活動,讓學生以即時遊戲的方式進行。

課程筆記

Variables & Constants

  • A variable is an identifier that can change during program execution; declared with `DECLARE <id> : <datatype>` in pseudocode.
  • Identifiers should use PascalCase, contain only letters/digits, and start with a capital letter.
  • A constant is an identifier set once; named in UPPERCASE to improve readability and maintainability.
  • Constants are declared with `CONSTANT <id> ← <value>`; changing a constant only requires alteration in one place.

Data Types

  • Basic data types: Integer (whole numbers), Real (decimal), Character (single char), String (sequence of chars), Boolean (True/False).
  • Choosing the correct data type ensures accuracy and efficiency; casting converts between types.
  • Pseudocode examples: `DECLARE Age : INTEGER`, `DECLARE Price : REAL`, `DECLARE GameOver : BOOLEAN`.

Input & Output

  • Input reads data from an input device (e.g., keyboard) using `INPUT <identifier>` in pseudocode.
  • Output sends data to an output device (e.g., monitor) using `OUTPUT <identifier(s)>`.
  • Without input, programs cannot interact with the outside world; without output, results cannot be seen.

Sequence

  • Sequence means lines of code run one at a time in the order they are written.
  • Incorrect sequence can cause unexpected behaviour or runtime errors (e.g., returning a variable before it is assigned).

Selection

  • Selection changes program flow based on conditions; uses `IF...THEN...ELSE` or `CASE` statements.
  • `IF` statements: `IF <condition> THEN <statements> ELSE <statements> ENDIF`.
  • Nested selection places one IF inside another; CASE compares multiple values of the same variable.
  • Boolean operators `AND`, `OR`, `NOT` combine conditions; `AND` true if both true, `OR` true if at least one true, `NOT` inverts.

Iteration

  • Count-controlled loops repeat a fixed number of times using `FOR <id><start> TO <end> [STEP <inc>]`.
  • Condition-controlled loops repeat until a condition is met: `WHILE` (pre-condition) or `REPEAT...UNTIL` (post-condition).
  • Nested iteration is a loop inside another loop, e.g., a FOR inside another FOR.

Totalling & Counting

  • Totalling adds up values in a loop: initialise total to 0, then update inside loop.
  • Counting tracks occurrences: initialise count to 0, increment when condition met.
  • Both are commonly used with loops to accumulate results.

String Handling

  • Case conversion: `UCASE`/`LCASE` (pseudocode) or `.upper()`/`.lower()` (Python).
  • Length: `LENGTH(string)` (pseudocode) or `len(string)` (Python).
  • Substring: `SUBSTRING(string, start, length)` in pseudocode (start at 1); Python uses slicing `string[start:end]` (start at 0).

Arithmetic, Logical & Boolean Operators

  • Arithmetic operators: `+`, `-`, `×`, `/`, `MOD` (remainder), `DIV` (quotient), `^` (exponent).
  • Logical operators compare values: `==`, `<>`, `<`, `≤`, `>`, `≥`.
  • Boolean operators: `AND`, `OR`, `NOT` used to combine conditions.

Procedures & Functions

  • Procedures do not return a value; defined with `PROCEDURE ... ENDPROCEDURE` and called with `CALL`.
  • Functions return a value; defined with `FUNCTION ... RETURNS <type> ... ENDFUNCTION` and called in expressions.
  • Parameters pass data into sub-programs; multiple parameters allowed.
  • Sub-programs improve readability, maintainability, and avoid code duplication.

Local & Global Variables

  • Local variables declared inside a sub-program; only accessible there; destroyed when the sub-program ends.
  • Global variables declared at the outermost level; accessible from anywhere in the program.
  • Use local variables to avoid unintended side-effects; global variables can be modified by any part of the program.

Library Routines

  • Library routines are pre-written, reusable code (e.g., `RANDOM`, `ROUND`) that save time and are already tested.
  • `RANDOM` generates random numbers; in pseudocode `RANDOM(lower, upper)`; in Python `import random; random.randint(a,b)`.
  • `ROUND` rounds a number to a specified number of decimal places: `ROUND(value, places)`.

Maintaining Programs

  • Use layout, indentation, comments, meaningful variable names, white space, and sub-programs to make code easy to maintain.
  • Consistent use of these techniques improves readability and reduces errors when modifying code.

投影片

Sign up free to view the lesson slides

Step through every slide for this topic — plus flashcards and revision notes — with a free account.

練習題

免費預覽——54 題中的 8 題。註冊即可查看全部。
  1. 1.Which of the following is the correct way to declare a variable in pseudocode?

    Easy
    • ADECLARE Age : INTEGER
    • BDECLARE Age = INTEGER
    • CAge : INTEGER
    • DINTEGER Age
  2. 2.Which data type would be most appropriate to store the value 'True'?

    Easy
    • ABOOLEAN
    • BSTRING
    • CINTEGER
    • DCHAR
  3. 3.What is the output of the following pseudocode? Total ← 0 FOR X ← 1 TO 3 Total ← Total + X NEXT X OUTPUT Total

    Medium
    • A6
    • B3
    • C1
    • D0
  4. 4.Which of the following correctly represents a condition-controlled loop in pseudocode?

    Medium
    • AWHILE Colour ≠ 'Red' DO INPUT Colour ENDWHILE
    • BFOR X ← 1 TO 10 OUTPUT X NEXT X
    • CREPEAT INPUT Colour UNTIL Colour = 'Red'
    • DIF Colour = 'Red' THEN OUTPUT 'Stop' ENDIF
  5. 5.What is the value of Result after executing the following pseudocode? Result ← 10 MOD 3

    Easy
    • A1
    • B3
    • C0
    • D10
  6. 6.Which of the following is a valid constant declaration in pseudocode?

    Easy
    • ACONSTANT π ← 3.142
    • BCONSTANT π = 3.142
    • CDECLARE π : CONSTANT ← 3.142
    • Dπ CONSTANT ← 3.142
  7. 7.What is the purpose of a procedure in programming?

    Easy
    • ATo perform a task without returning a value
    • BTo return a value
    • CTo store data permanently
    • DTo handle user input
  8. 8.Which of the following is the correct pseudocode to output the length of a string stored in variable Word?

    Easy
    • AOUTPUT LENGTH(Word)
    • BOUTPUT LEN(Word)
    • COUTPUT Word.LENGTH
    • DOUTPUT LENGTH Word

Unlock all 54 questions & more

建立免費帳號,即可查看這個主題的所有題目、投影片、字卡與複習筆記。

歷屆試題

這個主題的歷屆試題練習即將推出。
即將推出