02 Section 5 - Program flow, Boolean operators and Arrays Flashcards
What do IF statements allow a programmer to do?
To check if a condition is true or false, and carry out different actions depending on the outcome
What structure do IF statements normally have?
IF … THEN:
ElSE:
When is ELSE not needed in an IF statement?
If there is nothing for the program to do when the condition is false then leave out the ELSE part
What is a nested IF statement?
A more complex IF statement, made by putting one IF statement inside of another
What do nested IF statements allow you to do?
-check more conditions once you have established the previous condition is true
What are IF-ELSEIF statements used for?
To check multiple conditions
-only check more conditions if the previous condition is false
What are SWITCH-CASE statements?
statements that can check whether a variable has specific values
When are SWITCH-CASE statements used?
When you want a program to perform different actions for different values of the same variable
What is the structure of SWITCH-CASE statements?
SWITCH … :
CASE … :
CASE … :
ENDSWITCH
What is the drawback of SWITCH-CASE statements?
They can only check the value of one variable
IF-ELSEIF statements can check if multiple conditions are true
What are FOR loops?
An example of count-controlled loops
They will repeat the code inside of them for a fixed number of times
-the number of times they are repeated depends on an initial value, end value and step count (can also be set as the program runs by using a variable as the number)
What is the structure of FOR loops?
FOR i in RANGE():
NEXT i
What are the different types of loops you can have that are controlled by conditions?
REPEAT … UNTIL
WHILE
DO WHILE
What is an example of a count-controlled loop?
FOR loops
What are the features of REPEAT loops?
- controlled by a condition at the end of the loop
- keep going until the condition is true (i.e. while it’s false)
- always runs the code inside at least once
- you can get an infinite loop if the condition is never true
What are the features of WHILE loops?
- controlled by a condition at the start of the loop
- keep going while the condition is true (i.e. until it is false)
- never runs the code inside it if the condition is initially false
- get an infinite loop if the condition inside is always true
What are the features of DO WHILE loops?
- controlled by a condition at the end of the loop
- keep going while the condition is true (i.e. until it’s false)
- always runs the code inside it at least once
- get an infinite loop if the condition is always true
What is the structure of a REPEAT loop?
REPEAT
…
UNTIL condition
What is the structure of a WHILE loop?
WHILE condition
…
ENDWHILE
What is the structure of a DO WHILE loop?
DO
…
WHILE condition
Whant are the 3 different Boolean operators?
AND
OR
NOT
Why are Boolean operators used in conditions?
Boolean operators can be used to make all the different selection and iteration statements more efficient and versatile
What are examples of selection statements?
IF-THEN-ELSE statements
Nested IF statements
IF-ELSEIF statements
SWITCH-CASE statements
What are examples of iteration statements?
FOR loops
REPEAT loops
WHILE loops
DO WHILE loops
What is an array?
It’s a data structure that can store a collection of data values all under one name
LISTS
What is each piece of data in an array called?
an element
How can an element of an array be accessed?
-each element can be accessed using its position (or index) in an array
When should arrays be used?
When you have lots of related data that you want to store and it doesn’t make sense to use separate variables
How do you create an array?
ARRAY nameofarray[numberofvaluesinarray] nameofarray[0] = data nameofarray[1] = data nameofarray[2] = data ...
How do you retrieve values from an array?
print(nameofarray[element’sposition])
How do you change elements in an array?
nameofarray[element’sposition] = newdata
What are two-dimensional arrays?
Like lists of lists, where each element of the one-dimensional array also has a one-dimensional array
What is the position of an element in a two-dimensional array written as?
[a, b] or [a][b]
where a represents the position in the one-dimensional array that the element is in
and where b represents its position within that one-dimensional array