1/23
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
Many App Lab projects run in the following way.
The user interacts with a screen element (like clicking a button or typing in a text box). This triggers an event handler.
The event handler changes the values stored in variables.
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.
Counter Pattern with Event
|
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 |
| ![]() |
Local | Temporary. Can be used only in the part of the code where it was created, like inside an |
| ![]() |
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
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?
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
Boolean Expression
A expression that produces a true or false when evaluated
Logical Operator
A symbol or word used to connect two or more expressions
&&
This is the word “And” for coding
II
This is a word “Or” for coding not a number.
!
This is the word “Not” for coding
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?
If-Else-If
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.
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.
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.
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.
You Win!
What will be displayed after this code segment is run?
score ← 4
lives ← 3
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
The UpdateScreen() Pattern
How does the UpdateScreen() pattern work
The user interacts with a screen element (like clicking a button or typing in a text box). This triggers an event handler.
The event handler changes the values stored in variables.
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.
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. |
School Day
What will be displayed after this code segment is run?