Algorithms Writing instructions in the order they should execute.
-
Upload
kenneth-tyler -
Category
Documents
-
view
217 -
download
4
Transcript of Algorithms Writing instructions in the order they should execute.
Creating a method
Exercise 1 • Move the sprite 25 steps,
• Have the sprite say your name for 3 seconds
• Turn the sprite around and move it back where it came from
• Have the sprite say “I love BYOB!” for 5 seconds
Some commands require additional information to execute.
pointInDirection(90);
90 is the argument (value) The are where you put arguments is called parameter( );
Drawing
• Exercise 1: Write an algorithm to make your sprite draw a square with 50 step sides.• Exercise 2: Once
you’ve done that, write an algorithm to draw two squares next to each other• The squares should
not be connected by a line
Sequence
• Exercise: Rewrite your square drawing algorithm to draw a square whenever the space bar is pressed. Clear the screen before each new square.
User Interaction
• Exercise: Write scripts to allow the user to move a sprite around the stage with the arrow keys.
Loops Repeated Instructions • Exercise 1: Rewrite the
script to draw a square using loops. Try not to repeat any code.
• Exercise 2: Now rewrite the script to draw two squares next to each other using loops. Again, try not to repeat code.• This is tricky!
User Input
• Exercise 1: Write an algorithm to do the following:• Ask the user for a number
between 1 and 10• Draw that many squares
• Exercise 2: Write an algorithm to do the following:• Ask the user for a number
between 1 and 10• Ask the user for a number
between 1 and 255• Draw the first number of
squares with the pen color set to the second number
Color • Color has values between 1 and 255
RGB Model for color
Color(r,g,b)
Color(255,0,0) red Color(0,255,0) green Color(0,0,255) blue
Creating Variables • Variables store
information that can change.
•
Create the variable
Must set the variable to some amount
Assign the variable new information
Color • Exercise 2: Write an
algorithm to do the following:
• Ask the user for a number between 1 and 10
• Ask the user for a number between 1 and 255
• Draw the first number of squares with the pen color set to the second number
Arithmetic Operators
• Exercise: Write an algorithm to do the following:• Ask the user for a
number between 1 and 5• Draw twice that many
squares
Boolean expressions evaluate to either true or false
Relational Operators They show a relation between 2 things. 6 < 7 5 > 4 25 == 7 72 <= 52
They are used in conditionals to execute either the if or the else part of nothing.
Boolean Expressions
AND T F
T T F
F F F
OR T F
T T T
F T F
NOT
T F
F T
Compare two complete conditions
Draw a square and a rectangle. --- Must do both AND Draw a square or a rectangle -- Can do either OR Draw a square or a rectangle (NOT)
Conditionals – Booleans Random
• Exercise 1: Write an algorithm to do the following:• Generate a random number
between 1 and 10• Draw a red square if the
number is less than 6• Draw a blue square if the
number is 6 or greater
• Exercise 2: Write an algorithm to do the following:• Generate two random
numbers between 1 and 10• If both are less than 6, draw
a red square• If both are 6 or greater,
draw a blue square• Otherwise, draw a purple
square
Relational Operators
Greater than >Less than <Equal to == >= <=
Project: Create an algorithm that will generate a random number and say the number and whether it is even or odd.
If the random number is even it will say the random number and I am an even number.
If it is odd it will say the random number and I am odd
Procedure: 1. Create a variable called random2. Set the random variable to a random number using the random operator3. Check to see if it is even or odd by creating if statements and using the modulus operator. 4. Create if statements that You will need to use the modulus operator to see if the number is even or odd.
5. Copy your code into a word document.