Unit 4 CSP AP 10/28/2024

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

24 Terms

1
New cards

Score is 4

What will the value of score be at the end of the program?

Note: <- is used to represent a left facing arrow

var score
score <- 3
score <- score + 1
score <- "The score is: " + score

2
New cards

Many App Lab projects run in the following way.

  1. The user interacts with a screen element (like clicking a button or typing in a text box). This triggers an event handler.

  2. The event handler changes the values stored in variables.

  3. The information on the screen is updated to reflect the change to the variable.

Typically every one of your event handlers (onEvent blocks) will include identical code for step 3, updating the screen. To avoid this problem, make a single function called updateScreen that includes the code to change every element on your screen once variables have been updated.

You will usually want to call updateScreen inside of every event handler and will also want to call it once at the beginning of your program. This ensures that the same code is always being used to update the information that shows up on the screen.

knowt flashcard image
3
New cards

Counter Pattern with Event


var myVar = 0;


onEvent("id", "click", function() {
    myVar = myVar + 1;
});

<table style="min-width: 25px"><colgroup><col></colgroup><tbody><tr><td colspan="1" rowspan="1" style=" line-height: 18px;"><p><code><br>var myVar = 0;</code><br><br><code>onEvent("id", "click", function() {</code><br>&nbsp;&nbsp;&nbsp;&nbsp;<code>myVar = myVar + 1;</code><br><code>});</code></p></td></tr></tbody></table><p></p>
4
New cards

Global vs. Local Variables

There's two types of variables, global and local, and so far we've only used global variables. Here's the main difference between global and local variables.

Type of VariableHow It WorksHow CreatedPicture

Global

Permanent. Can be used anywhere in your code

var used outside an onEvent

Local

Temporary. Can be used only in the part of the code where it was created, like inside an onEvent. Deleted once the onEvent is done running.

var used inside an onEvent

5
New cards

Avoiding Local Variables and Debugging

Local variables will eventually be useful but for now they're most likely to just be confusing. The biggest issue you'll run into right now with local variables is accidentally using var inside of an onEvent. Here's what the code usually looks like:

This code is pretty confusing. While it looks like there's only one variable being used, it actually has two variables, one local, and one global, and they're both named count! Changing the value of one will have no impact on the other. This can cause unexpected behavior in your code and it can get tricky to debug.

The best way to avoid these issues is to make sure for now that you're not using var inside of an onEvent. If you run into a tricky debugging problem, check if you're accidentally creating a local variable.

6
New cards

6

Some programming languages provide an abstraction through which the size of representable numbers is limited only by the size of the computer's memory. This is the case for the language defined in the exam reference sheet. Remember overflow from Unit 1? You don't need to worry about that when using this pseudocode!

What will be displayed after this code segment is run?

<p><em>Some programming languages provide an abstraction through which the size of representable numbers is limited only by the size of the computer's memory. This is the case for the language defined in the exam reference sheet. Remember overflow from Unit 1? You don't need to worry about that when using this pseudocode!</em></p><p>What will be displayed after this code segment is run? </p>
7
New cards

Answer:10, 5,10, 5

The program below is run. Which of the following COULD NOT possibly be the output from that program?

a <- RANDOM(1,10)
b <- RANDOM(10,20)
DISPLAY(a)
DISPLAY(a)
DISPLAY(b)
DISPLA


8
New cards

Boolean Expression

A expression that produces a true or false when evaluated

9
New cards

Logical Operator

A symbol or word used to connect two or more expressions

10
New cards

&&

This is the word “And” for coding

11
New cards

II

This is a word “Or” for coding not a number.

12
New cards

!

This is the word “Not” for coding

13
New cards

Yes the computer can evaluate an expression to something between true and false. A computer can't deal with a maybe answer. This will give an expression to deal with a "maybe" answer for humans because somebody might have a different opinion.

Can a computer evaluate an expression to something between true and false? Can you write an expression to deal with a "maybe" answer?

14
New cards

If-Else-If

knowt flashcard image
15
New cards

How does If-else-If work?

The if-else-if command lets you check multiple boolean conditions. The computer will check conditions in the order they are written until one of the boolean expressions evaluates to true. The code associated with that boolean expression will be run, but all others will be skipped. If none of the expressions evaluates to true then the code inside the else command will be run.

16
New cards

Most Specific cases on if-else-if

When writing an if-else-if statement you want to put the most specific cases first. In the temperature example above you want to check for temperatures over 100 degrees first. Afterwards the code checks for temperatures above 90 degrees, but because of the order the code is written you know that none of temperatures you'll find there are above 100 degrees. After all you would have caught them in the previous if-statement. This means you can be confident any temperatures you catch there will be between 90 and 100 degrees. As you continue through the if-else-if statement you use the same logic to check for different ranges of temperature.


17
New cards

A Broken example for if-else-if specific cases

This is a broken example that shows what happens if you start checking for temperatures in reverse order, most specific cases last. Think through what would happen when this code runs for the temperature 82 degrees. You would want the output to say "It's hot". If you look at the first boolean expression, however, you'll notice that 82 is higher than 60 degrees, making that boolean expression evaluate to true. As a result the code will output "It's cool". This is because the first expression is not actually the most specific.

<p><span>This is a broken example that shows what happens if you start checking for temperatures in reverse</span> order, most specific cases last. <span>Think through what would happen when this code runs for the temperature 82 degrees. You would want the output to say </span><code>"It's hot"</code><span>. If you look at the first boolean expression, however, you'll notice that 82 is higher than 60 degrees, making that boolean expression evaluate to </span><code>true</code><span>. As a result the code will output </span><code>"It's cool"</code><span>. This is because the first expression is not actually the most specific.</span></p>
18
New cards

When creating an if-else-if statement you should always make your first condition the most specific. Write a short paragraph responding to the questions below.

  • What does it mean to put the most specific case first?

  • Why is it important to put the most specific case first? What types of errors does it help avoid?

"Putting the most specific case first" in an if-else-if statement means prioritizing the condition that applies to the narrowest set of possible input values; essentially, checking for the most detailed scenario before moving on to broader possibilities. This practice is crucial because it prevents potential errors by ensuring that if a value matches a highly specific condition, it will be handled correctly before being evaluated against more general conditions, thus avoiding unintended outcomes where a broader case might mistakenly capture a more precise scenario. 

19
New cards

You Win!

What will be displayed after this code segment is run?

score ← 4

lives ← 3

<p>What will be displayed after this code segment is run?</p><p>score ← 4</p><p>lives ← 3</p>
20
New cards

5

The program below asks a user to type in a number and then will output a message. What number will a user need to input for the message "COLD" to be displayed?

number <- INPUT()

IF (number >= 10)
{
  IF (number <= 20)
  {
    DISPLAY("MEDIUM")
  }
  ELSE
  {
    DISPLAY("HOT")
  }
}
ELSE
{
  DISPLAY("COLD")
}



A.   5

B.   10

C.   15

D.   20

Submit


21
New cards

The UpdateScreen() Pattern

knowt flashcard image
22
New cards

How does the UpdateScreen() pattern work

  1. The user interacts with a screen element (like clicking a button or typing in a text box). This triggers an event handler.

  2. The event handler changes the values stored in variables.

  3. The information on the screen is updated to reflect the change to the variable.

Typically every one of your event handlers (onEvent blocks) will include identical code for step 3, updating the screen. To avoid this problem, make a single function called updateScreen that includes the code to change every element on your screen once variables have been updated.

You will usually want to call updateScreen inside of every event handler and will also want to call it once at the beginning of your program. This ensures that the same code is always being used to update the information that shows up on the screen.

23
New cards

When to make a function

When You Create FunctionsDescriptionComments

Never

You never create functions at all.

Your code will be difficult to read and debug and have lots of repeated code. Aim to move on at least to the "After" step.

After

You write your entire program without functions. Once you're done you look for repeated code and move those into a function.

Your code is much easier to read and debug now. You're also getting better at seeing how your program is organized. As you get more comfortable, try to move on to "During" or "Before".

During

As you write your code you notice when you're about to rewrite code you already wrote somewhere else in your program. Before moving on you declare a function and call that function instead.

You have a good understanding of your program and are able to keep it organized as you develop it. See if sometimes you can move on to "Before".

Before

Before you write your program you make a plan and identify places you're likely to use repeated code. You create your function at the beginning.

This level reflects a strong understanding of how your program is going to be designed. You can almost "see it in your head" before you begin writing. That said, it's OK if sometimes you realize you need a function while you program and work in the "During" stage.

24
New cards

School Day

What will be displayed after this code segment is run?

<p>What will be displayed after this code segment is run?</p>