Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
What is iteration?
-
Iteration is repeating a line or a block of code using a loop
-
Iteration can be:
-
Count-controlled
-
Condition-controlled
-
Count-controlled loops
What is a count-controlled loop?
-
A count-controlled loop is when the code is repeated a fixed number of times (e.g. using a FOR loop)
-
A count-controlled loop can be written as:
|
Pseudocode |
|
|---|---|
|
|
-
In a FOR loop, the increment must be an expression that evaluates to an integer
-
The loop starts at
value1 -
The identifier is updated by the increment value on each iteration
-
The loop continues until the identifier passes value2
-
-
The increment can be positive or negative, depending on the direction you want the loop to count
Nested count-controlled loops
FOR i ← 1 TO 3 FOR j ← 1 TO 3 OUTPUT i, " x ", j, " = ", i * j NEXT j
NEXT i
-
The outer loop runs
ifrom 1 to 3 -
For each value of
i, the inner loop runsjfrom 1 to 3 -
It outputs all combinations of
i × j
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
...
3 x 3 = 9
Count-controlled loops in different languages
|
Python |
VB.net |
Java |
|---|---|---|
|
Print the numbers 1 to 5 |
||
|
|
|
Condition-controlled loops
What is a condition controlled loop?
-
A condition controlled loop is when the code is repeated until a condition is met
-
There are two types of condition controlled loops:
-
Post-condition (REPEAT)
-
Pre-condition (WHILE)
-
Post-condition loops (REPEAT)
-
A post-condition loop is executed at least once
-
The condition must be an expression that evaluates to a Boolean (True/False)
-
The condition is tested after the statements are executed and only stops once the condition is evaluated to True
-
It can be written as:
|
Pseudocode |
|
|---|---|
|
Pre-condition loops (WHILE)
-
The condition must be an expression that evaluates to a Boolean (True/False)
-
The condition is tested and statements are only executed if the condition evaluates to True
-
After statements have been executed the condition is tested again
-
The loop ends when the condition evaluates to False
-
It can be written as:
|
Pseudocode |
|
|---|---|
|
Nested condition-controlled loops
DECLARE Username : STRING
DECLARE Count : INTEGER Count ← 0 WHILE Count < 3 Username ← "" WHILE Username = "" OUTPUT "Enter a username: " INPUT Username IF Username = "" THEN OUTPUT "Username cannot be blank." ENDIF ENDWHILE OUTPUT "Username accepted: ", Username Count ← Count + 1
ENDWHILE
-
Ask the user to enter up to 3 usernames
-
For each one, keep asking until a non-empty name is entered
-
Outer WHILE runs 3 times (for 3 usernames)
-
Inner WHILE ensures that each username is not blank
Condition-controlled loops in different languages
|
Python |
VB.net |
Java |
|---|---|---|
|
Keep asking the user to enter a password until they type |
||
|
|
|
When to use each type of loop
|
Loop type |
When to use |
Example scenario |
|---|---|---|
|
Count-controlled loop ( |
When you know in advance how many times you want the loop to run |
Repeating an action 5 times, generating a multiplication table |
|
Pre-condition loop ( |
When you want to check a condition before running the loop. It may run 0 or more times |
Keep asking for a valid password before granting access |
|
Post-condition loop ( |
When you want the loop to run at least once, and then stop when a condition becomes true |
Ask the user for input and validate it after first run |
Responses