Logical Conditions in Computational Thinking (OCR A Level Computer Science)

Revision Note

Last updated

Determine the Logical Conditions that Affect the Outcome of a Decision

  • Most errors in a program occur when evaluating a Boolean condition, whether in a sequence as part of a statement, iteration or selection. It is therefore important to be careful when creating Boolean conditions, especially long, complex conditions involving multiple clauses

What are Boolean Conditions?

  • Boolean conditions use combinations of several operators:

    • == (equal to)

    • > (greater than)

    • >= (greater than or equal to)

    • < (less than)

    • <= (less than or equal to)

    • != (not equal to)

    • AND (both conditions must be true)

    • OR (either or both conditions must be true)

    • NOT (the condition is not true)

    • XOR (only one or the other condition must be true)

  • An example of a condition could be a teenager wanting to see a 15 rated movie at a cinema that costs £8. If the teenager isn’t at least 15 years old and doesn’t have enough money then they cannot watch the movie

if age >= 15 and money >= 8 then

entry = True

endif

  • Another example could be checking whether a list contains any numbers and if so, removing them. In order to do this, each item must be checked one by one, checked to see if it is numeric then remove it if so

i = 0

valid_element = True

while len(my_list) > 0:

   valid_element = True

   element = str(my_list[i])

   for index in list_item

      if list_item[index] in [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] then

         valid_element = False

         break

      endif

   next index

   if valid_element == False then

      my_list.pop(i)

   endif

endwhile

  • The above algorithm is a pseudocode solution to removing numbers from a list of strings

  • The decision points are:

    • While loop: the list has to have at least one element in order for the while loop to run

    • For loop: all elements in the list “list_item” are iterated over one at a time. The stopping condition is reaching the end of the list

    • First if statement: each character in the list “list_item” is checked. If it is a numeric digit then the string contains a number and therefore must be removed. Be aware that in this implementation, a string is considered numeric if it contains even a single digit. Valid_element is set to false and the program breaks out of the for loop

      • Be careful using “break” instructions. They are powerful and help control the flow of control but can be misused and cause unintended problems

      • Second if statement: if the item in “my_list” contains a digit, it is removed from the front of the list

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?