Uploaded by Mishun Miller

Algorithms and Programming (CS1) Lesson 4 - Variables

advertisement
Today’s Big Ideas Continued
Algorithms and Programming (AAP):
Programmers integrate algorithms and abstraction to create programs for
creative purposes and to solve problems. Using multiple program statements in a
specified order, making decisions, and repeating the same process multiple times
are the building blocks of programs. Incorporating elements of abstraction, by
breaking problems down into interacting pieces, each with their own purpose,
makes writing complex programs easier. Programmers need to think
algorithmically and use abstraction to define and interpret processes that are used
in a program.
Today’s Computational Thinking
Practices
Computational Solution Design:
●
1.A: Investigate a situation, context, or task.
●
1.B: Determine and design an appropriate method or approach to achieve the purpose.
●
1.C: Explain how collaboration affects the development of a solution.
●
1.D Evaluate solution options.
Algorithms and Program Development:
●
2.A: Represent algorithmic processes without using a programming language.
●
2.B: Implement an algorithm in a program.
Today’s Computational Thinking
Practices Continued
Abstraction in Program Development:
●
3.A: Generalize data sources through variables.
Code Analysis:
●
4.A: Explain how a code segment or program functions.
●
4.B: Determine the result of code segments.
●
4.C Identify and correct errors in algorithms and programs including error discovery
through testing.
CodeCombat
Introduction to Computer Science
Quest #4
Variables
Let’s Review…
1
CODING CONCEPTS
2
GUIDED PLAY AND INDEPENDENT PRACTICE
3
CHECK-IN & CONCEPT REVIEW
4
COMPLETE QUESTS
What did we talk about last time?
What activities are you still working on?
Are you having trouble with a concept?
Are there any quests you want to review?
Quest #4
By the end of my quest, I will be
able to…
� Create an algorithm that
features variables.
� Describe how the value
stored in a variable
changes over time.
� Debug a program that
features variables.
Variables
Variable
A variable allows us to save information for later use in our
programs! The values saved in a variable can change over time.
In order to save a value in a
variable, you must assign the
value to a unique name using
Variable
(the container)
the assignment operator (=).
The integer 3 is
assigned to the
variable named
coins.
Value
(stored inside
the container)
Variable Name
(name of container)
var coins = 3;
JavaScript
Warm-Up: Part 1
Let’s play a game of Madlibs
using our knowledge of
variables!
There is a secret Madlib story
on the next slide. But before
we can read the story, we
need to assign values to
these variables.
Assign values to the
variables seen to the right.
Our Variables
● var age = ?;
● var name = ?;
● var song = ?;
● var food = ?;
● var number = ?;
JavaScript
Warm-Up Part 1: Solution
Our Variables
● var age = 13;
● var name = “Ida”;
● var song = “Happy Birthday”;
● var food = “tacos”;
● var number = 1000;
Once upon a time, there was a
[age] year old coder named
[name].
[name] liked to hum the song
[song] while coding. It was so
annoying that their teammates
would throw [food] at [name]
until they would stop singing.
Still, [name] was the best coder
on the team and could write
[number] lines of code every
day. Maybe [song] was [name]’s
secret power?
No one will ever know.
JavaScript
Warm-Up: Part 2
Translate the following statements into their English
equivalents. In other words, what would you say if you read
these statements outloud.
1. var faveClass = “CS”;
2. var gpa = 3.5;
3. var isStudent = true;
The string “CS” is
assigned to the
variable named
faveClass.
JavaScript
Warm-Up Part 2: Solution
1. The string “CS” is assigned to the variable
named faveClass.
1. The float 3.5 is assigned to the variable
named gpa.
1. The boolean True is assigned to the
variable named isStudent.
To the Dungeon!
Let’s put these
concepts into
action!
Game Recap: Known Enemy
Starter Code
👀
Python
Game Recap: Known Enemy
Starter Code
👀
JavaScript
Concept Video: Variables
From Pseudocode to Syntax
JavaScript Syntax:
Variable names
can start with
letters, an
underscore, or a
dollar sign.
var name = value;
You choose
Keyword
Float, Integer,
Boolean, String
Variable names
cannot contain
spaces!
Assignment
Operator
JavaScript
Reassignment
You can change the value
stored in a variable
through reassignment.
The most recent
assignment is the value
stored in the variable – all
previous values have been
forgotten!
1. var name =
“Tharin”;
2. var hair = “Brown”;
3. var hasArmor =
False;
4. name = “Tharin the
Knight”;
5. hair = “Blue”;
6. hasArmor = True;
JavaScript
Referencing Variables
Sometimes you will need to use the value stored in a variable
somewhere in your program. In these situations, you need to
reference the variable. To do this, write the name of the
variable where you need the value.
1. var address = “2 Hex Lane”;
2. hero.goTo(address);
2 Hex Lane
3. address = “784 Pixie Place”
4. hero.goTo(address);
784 Pixie Place
JavaScript
Assigning to Method Calls
Some methods return information when called. In other
words, the method call gives back a value. You can assign
that value to a variable.
var enemy = hero.findNearestEnemy();
The name of the
closest enemy is
ASSIGNED to the
variable enemy.
2
1
Ursa
This method finds the enemy that is
closest to the hero and RETURNS
the name of that enemy.
Brak
JavaScript
Assigning to Method Calls
Here is another example of a method that returns
information when called.
var distance = hero.distanceTo(“Ursa”);
2
A float is ASSIGNED to the variable
distance.
1
This method RETURNS the distance
between the hero and the object
named “Ursa”.
5.3 Feet
Ursa
JavaScript
Reminder: Debugging!
You’re going to make mistakes. That’s a very NORMAL part of coding, even
for experts. Finding and fixing errors (debugging) is an important part of
programming. Consider using the following tools when debugging your
programs:
● Error messages
● Comments
● hero.say() or print()
● Tracing tools
● Visualization tools
● Debuggers
● Unit tests
We are going to
practice reading error
messages in CS1!
Debugging Debrief
The following program is
intended to make the hero
defeat 3 ogres. However, it
does not work as intended.
1. Where is the error?
1
1. How would you fix this
mistake?
2
Python
Debugging Debrief
The following program is
intended to make the hero
defeat 3 ogres. However, it
does not work as intended.
1. Where is the error?
1
1. How would you fix this
mistake?
2
JavaScript
Independent Practice
Log into CodeCombat
https://codecombat.com/
Play Levels
CS1 Levels 15 - 19
Bonus Activities:
Master of Names, Debug, and Safe Place
Need Help?
Always try it once before asking for help.
Summary of Key Terms
●
●
●
Variable: A unique name that stores a value.
Declare: To create a variable.
Assign: To store a value in a variable. The assignment
operator (=) is required to do this. Remember, you can
store the following values in a variable:
●
●
●
●
●
Float
Integer
●
●
Boolean
String
Reassign: To change the value stored in a variable
using the assignment operator.
Reference a variable: The process of using a
variable in your code by entering the variable name
(without quotes).
Return: This occurs when a method call gives back a
value.
Any Questions?
5
4
6
3
2
1
Wrap Up 1
Describe a scenario
when you would use
variables in your
program. Your response
should include an
example line of code to
be considered correct.
Wrap Up 2
The hero needs to defeat all
three enemies. However, the
algorithm does not work as
intended:
What is wrong? What would
you do to fix this problem?
JavaScript
Wrap Up 3
Consider the following algorithm:
Describe what would happen if you
were to run this program. It may
help to examine the dungeon to the
right.
JavaScript
Wrap Up
Add your own
formative question.
Download