Computer Science

Subdecks (7)

Cards (513)

  • ELIF
    It is simple, efficient, and easier to read when there are fewer conditions
  • Indentation in programming

    Indentation refers to the spaces at the beginning of a code line
  • Algorithm to ask user to enter a number between 1 and 10 and output if it is low or high
    OUTPUT "enter a number"
    nom = USERINPUT
    IF nom <= 5
    OUTPUT (nom) "is a low number"
    ELSE
    OUTPUT (nom) "is a high number"
  • Boolean operators
    Named after George Boole, a 19th century English mathematician who formulated an algebraic system of logic, can be represented as true or false / 1 or 0
  • Boolean operators
    • AND - Logical AND operator, if all operands are true then condition is true
    OR - Logical OR operator, if any operands are true then condition is true
    NOT - Logical NOT operator
  • Indentation
    Spaces at the beginning of a code line
  • Algorithm to ask user to enter a number between 1 and 10
    1. OUTPUT "enter a number"
    2. nom = USERINPUT
    3. IF nom <= 5
    4. OUTPUT (nom) "is a low number"
    5. ELSE
    6. OUTPUT (nom) "is a high number"
  • Boolean operators
    • AND
    • OR
    • NOT
  • AND operator

    Logical AND operator, if all operands are true then the condition becomes true
  • OR operator

    Logical OR operator, if any of the operands are true then the condition becomes true
  • NOT operator
    Logical NOT operator, used to reverse the logical state of the operand
  • Algorithm to input student year and tutor group
    1. year ← USERINPUT
    2. OUTPUT "Please enter the tutor group."
    3. tutorGroup ← USERINPUT
    4. IF year < 7 AND > 13 THEN
    5. OUTPUT "The year group is out of range"
    6. ENDIF
    7. IF tutorGroup ≠ "red"OR tutorGroup ≠ "green" THEN
    8. OUTPUT "There is no tutor group with this colour"
    9. ENDIF
  • Nested IF statement
    An 'IF' statement inside another 'IF' statement
  • Algorithm to find the largest of 3 numbers
    1. OUTPUT "enter a number"
    2. A = USERINPUT
    3. OUTPUT "enter a number"
    4. B = USERINPUT
    5. OUTPUT "enter a number"
    6. C = USERINPUT
    7. IF A > (B OR C)
    8. OUTPUT A "is the biggest number"
    9. ENDIF
    10. IF B > (A OR C)
    11. OUTPUT B "is the biggest number"
    12. ENDIF
    13. IF C > (B OR A)
    14. OUTPUT C "is the biggest number"
    15. ENDIF
  • Algorithm to output month name based on number input
    1. OUTPUT "enter a number"
    2. A = USERINPUT
    3. CASE A
    4. 1: OUTPUT "January"
    5. 2: OUTPUT "February"
    6. ...
    7. 12: OUTPUT "December"
    8. ENDCASE
  • Top-down problem solving

    Breaking down a difficult problem into sub-tasks to make it easier
  • Computational thinking involves decomposition, abstraction, pattern recognition and algorithm design
  • Bottom-up problem solving
    Starts with a collection of small problems and tries to combine them together to form a larger system
  • Benefit of structured programming

    Subroutines can be called and used in the program as many times as needed without having to rewrite the code each time, reused in different programs
  • Sub-routines in 'User login' process
    • Enter a username
    • Check if username exists
    • Enter password
    • Check password
  • Procedure
    A process to do an activity till completion
  • Function
    A command
  • SUBROUTINE count()
    FOR x ← 5 TO 10
    OUTPUT "x"
    ENDFOR
    ENDSUBROUTINE
  • In Python, functions do not have to return values, but in languages with both functions and procedures, functions must return values
  • Python game to throw 3 dice and keep highest score
    import random
    import time
    HS = 0
    while True:
    A = input("Will you roll the dice? (yes/no) ")
    if A == "yes":
    print("rolling....")
    time.sleep(1)
    print("still rolling...")
    time.sleep(1)
    input ("you still there? ")
    N1 = random.randint(1, 6)
    time.sleep(1)
    print("dice 1 =", N1)
    N2 = random.randint(1, 6)
  • Iteration
    Repeating steps or instructions over and over again until a particular condition is met (LOOP)
  • Why use iteration
    • It simplifies an algorithm, it is quicker and does not include lots of unnecessary steps (Abstraction)
  • Condition
    A situation that is checked every time. If the condition is false the iteration repeats and if it is true the condition stops
  • Counter
    They can keep count of how many iterations have been completed
  • Iteration is the act of repeating a process until there is a desired outcome
  • Real-world examples of humans iterating
    • Practising sports
    • Boiling water
    • Practising an instrument
  • WHILE loop
    1. Index = 0
    2. WHILE index <= 10
    3. OUTPUT("The number is " + index)
    4. Index = index + 1
    5. ENDWHILE
  • Python WHILE loop
    1. Print("Please insert £8 for a ticket")
    2. While total < 8
    3. coins = int(input("Enter pound coins"))
    4. total = total + coins
    5. print("Amount entered", total)
    6. print("Thank you!")
  • FOR loop
    • Pseudocode uses a variable name and has to show the start and end e.g 0-10
    • Python uses a string and you only have to show the end but it doesn't go up to the end number e.g (14) it only goes from 0-13
  • WHILE loop
    • Pseudocode similar to FOR loop but you use < or .
    • Python once you do what is asked e.g put money in the ticket machine, the loop is dropped and it proceeds with the code
  • Definite iteration
    The number of iterations that are going to happen before the iteration of the loop e.g 5 lessons in a day, definitely going to happen
  • WHILE loop algorithm
    1. number = int(input('Please enter your number.'))
    2. Index = 1
    3. While index < = 12:
    4. print(index + 'x' + number + '=' + number + index
    5. INDEX = INDEX + 1
  • Python algorithm for entering numbers
    1. a = int(input("Enter a low number: ")
    2. b = int(input("Enter a higher number: ")
    3. while a <= b
    4. print(a)
    5. a = a+1
  • Indefinite iteration
    The number of iterations is not known before the loop is started
  • Definite iteration
    The number of iterations is known before the execution of the loop is started