Selection Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Three basic programming constructs

A

Sequence
Selection
Iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sequence

A

ensures that commands are executes in the correct order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Selection

A

used in an algorithm to choose between two or more options

Uses if, else if, and else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When is if/else or else if used?

A

if/else is used if there are only two possible outcomes
it isn’t efficient to keep repeating them, as each separate if statement is checked after the selection has been made

if there are more than two outcomes, else if is used
this is more efficient, as after the correct condition is found, non of the other options are checked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When are else statements used?

A

used to state what should happen if none of the options in the if or else if statements are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Nested if statements

A

If statements that are completely within another if statement

Every if statement needs it’s own endif statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Switch/case

A

A programming construct that can also be used for selection

answer = input ("Please enter your choice")
switch answer:
    case "A":
        print("That is incorrect")
    case "B":
        print("That is incorrect")
    case " C":
        print("That is correct")
    default:
        print("That is not recognised")
endswitch

The default statement is used to trap errors in the same way as an else statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A bowling alley gives a 5% reduction in price if there are four or more people on a lane and a further 10% reduction if they are members.
Write the code that will calculate the final charge. [5]

A
if numberOfPeople >= 4 then
    charge = charge - (charge/100*5)
    if membership == "Yes" then
        charge = charge - (charge/100*10)
    endif
endif

print(“The charge is: “ + charge)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A shop gives a discount of 10% for purchase of £200 and over, up to a total discount of £300.
Wrote an algorithm, in pseudocode, that would allow a user to calculate the discount. [4]

A
charge = input("Please enter the money spent")
if charge >= 200 then
     discount = charge/100*10
     if discount > 300 then
         discount = 300
     endif
endif
How well did you know this?
1
Not at all
2
3
4
5
Perfectly