Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Variables & Constants

What is a variable?

  • A variable is an identifier that can change in the lifetime of a program

  • Identifiers should be:

    • In mixed case (Pascal case)

    • Only contain letters (A-Z, a-z)

    • Only contain digits (0-9)

    • Start with a capital letter and not a digit

  • A variable can be associated a datatype when it is declared

  • When a variable is declared, memory is allocated based on the data type indicated

Pseudocode

DECLARE <identifier> : <datatype>

  • To declare a variable, use the DECLARE keyword followed by the name and data type:

DECLARE Age : INTEGER
DECLARE Name : STRING
DECLARE IsLoggedIn : BOOLEAN
DECLARE Temperature : REAL
DECLARE DOB : DATE
  • You can then assign a value using the assignment operator :

Age ← 18
Name ← "Alice"
IsLoggedIn ← TRUE

What is a constant?

  • A constant is an identifier set once in the lifetime of a program

  • Constants are generally named in all uppercase characters

  • Constants aid the readability and maintainability

Pseudocode

CONSTANT <identifier> ← <value>

  • To declare a constant, use the CONSTANT keyword:

CONSTANT Pi ← 3.14159
CONSTANT MaxScore ← 100
CONSTANT SchoolName ← "Meridian Academy"
  • Constants are not reassigned during execution – they are fixed values used throughout the algorithm

Example

  • You are writing a program to calculate the area of a circle using the formula:

Area ← π × radius²

  • π is a constant (it never changes)

  • radius and area are variables (they change depending on the input)

CONSTANT Pi ← 3.14159 DECLARE Radius : REAL
DECLARE Area : REAL OUTPUT "Enter the radius of the circle:"
INPUT Radius Area ← Pi * Radius * Radius OUTPUT "The area of the circle is: ", Area

Variables and constants in different languages

Feature

Python

VB.NET

Java

Declare variable

Just assign it (no keyword needed)

Dim radius As Double

double radius;

Assign variable

radius = 5

radius = 5

radius = 5;

Declare constant

Convention: use UPPERCASE (PI = 3.14) (not enforced)

Const Pi As Double = 3.14159

final double PI = 3.14159;

Reassign constant?

Yes (not truly constant unless enforced)

Cannot change once set

Cannot change once set

Use in calc.

area = PI * radius * radius

area = Pi * radius * radius

area = PI * radius * radius;

Arithmetic & logical operators

Arithmetic operators

  • Arithmetic operators are used to perform basic maths operations in a program

  • These include adding, subtracting, multiplying and dividing values

Common arithmetic operators

Operator

Purpose

Example

Result

+

Addition or string concatenation

5 + 3

8

"John" + " " + "Doe"

"John Doe"

-

Subtraction

10 - 4

6

*

Multiplication

3 * 5

15

/

Division

10 / 2

5

^

Exponentiation (power of)

3 ^ 3

27

MOD

Modulus (remainder after division)

10 MOD 3

1

Operator precedence (BODMAS / BIDMAS)

  • Arithmetic operators follow operator precedence

  • Multiplication and division happen before addition and subtraction unless brackets are used

result ← 2 + 3 * 4 // gives 14
result ← (2 + 3) * 4 // gives 20

Logical operators

  • Logical operators are used to compare values

  • They return either TRUE or FALSE and are commonly used in conditions and loops

Common logical operators

Operator

Purpose

Example

Result

=

Equal to

5 = 6

FALSE

<>

Not equal to

5 != 7

TRUE

>

Greater than

5 > 10

FALSE

<

Less than

5 < 10

TRUE

>=

Greater than or equal to

5 >= 10

FALSE

<=

Less than or equal to

5 <= 10

TRUE

Example code

x ← 5
y ← 10 OUTPUT x == y // FALSE
OUTPUT x <> y // TRUE
OUTPUT x < y // TRUE
OUTPUT x > y // FALSE
OUTPUT x <= y // TRUE
OUTPUT x >= y // FALSE

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注