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

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

FOR <identifier> ← <value1> TO <value2> <statements(s)>
NEXT <identifier>
FOR <identifier> ← <value1> TO <value2> STEP <increment> <statements(s)>
NEXT <identifier>
  • 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 i from 1 to 3

  • For each value of i, the inner loop runs j from 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

for i in range(1, 6): print(i)
For i As Integer = 1 To 5 Console.WriteLine(i)
Next
for (int i = 1; i <= 5; i++) { System.out.println(i);
}

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

REPEAT <statement(s)>
UNTIL <condition>

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

WHILE <condition> DO <statement(s)>
ENDWHILE

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 "admin"

password = "" while password != "admin": password = input("Enter the password: ") if password != "admin": print("Incorrect, try again.") print("Access granted!")
Module PasswordCheck Sub Main() Dim password As String = "" While password <> "admin" Console.Write("Enter the password: ") password = Console.ReadLine() If password <> "admin" Then Console.WriteLine("Incorrect, try again.") End If End While Console.WriteLine("Access granted!") Console.ReadLine() ' Keeps the console open End Sub
End Module
import java.util.Scanner; public class PasswordCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String password = ""; while (!password.equals("admin")) { System.out.print("Enter the password: "); password = scanner.nextLine(); if (!password.equals("admin")) { System.out.println("Incorrect, try again."); } } System.out.println("Access granted!"); scanner.close(); }
}

When to use each type of loop

Loop type

When to use

Example scenario

Count-controlled loop (FOR)

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 (WHILE)

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 (REPEAT ... UNTIL)

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

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