Download as pdf or txt
Download as pdf or txt
You are on page 1of 74

Introduction to Programming with RAPTOR

By Dr. Wayne Brown

What is RAPTOR?

RAPTOR is a visual programming development environment based on flowcharts. A flowchart


is a collection of connected graphic symbols, where each symbol represents a specific type of
instruction to be executed. The connections between symbols determine the order in which
instructions are executed. These ideas will become clearer as you use RAPTOR to solve
problems.

We use RAPTOR in CS110 for several reasons.


• The RAPTOR development environment minimizes the amount of syntax you must learn
to write correct program instructions.
• The RAPTOR development environment is visual. RAPTOR programs are diagrams
(directed graphs) that can be executed one symbol at a time. This will help you follow the
flow of instruction execution in RAPTOR programs.
• RAPTOR is designed for ease of use. (You might have to take our word for this, but
other programming development environments are extremely complex.)
• RAPTOR error messages are designed to be more readily understandable by beginning
programmers.
• Our goal is to teach you how to design and execute algorithms. These objectives do not
require a heavy-weight commercial programming language such as C++ or Java.

RAPTOR Program Structure

A RAPTOR program is a set of connected symbols that represent actions to be


performed. The arrows that connect the symbols determine the order in which the
actions are performed. When executing a RAPTOR program, you begin at the
Start symbol and follow the arrows to execute the program. A RAPTOR
program stops executing when the End symbol is reached. The smallest RAPTOR
program (which does nothing) is depicted at the right. By placing additional
RAPTOR statements between the Start and End symbols you can create meaningful
RAPTOR programs.

Introduction to RAPTOR Statements/Symbols

RAPTOR has six (6) basic symbols, where each symbol represents a unique
type of instruction. The basic symbols are shown at the right. The top four
statement types, Assignment, Call, Input, and Output, are explained
in this reading, The bottom two types, Selection and Loops, will be
explained in a future reading.

1 of 12
The typical computer program has three basic components:
• INPUT – get the data values that are needed to accomplish the task.
• PROCESSING – manipulate the data values to accomplish the task.
• OUTPUT – display (or save) the values which provide a solution to the task.
These three components have a direct correlation to RAPTOR instructions as shown in the
following table.

Purpose Symbol Name Description


input Allow the user to enter data. Each data value
INPUT statement is stored in a variable.

assignment Change the value of a variable using some


PROCESSING statement type of mathematical calculation.

procedure Execute a group of instructions defined in


call the named procedure. In some cases some of
PROCESSING
the procedure arguments (i.e., variables) will
be changed by the procedure's instructions.
output Display (or save to a file) the value of a
OUTPUT statement variable.

The common thread among these four instructions is that they all do something to variables! To
understand how to develop algorithms into working computer programs, you must understand
the concept of a variable. Please study the next section carefully!

RAPTOR Variables

Variables are computer memory locations that hold a data value. At any given time a variable
can only hold a single value. However, the value of a variable can vary (change) as a program
executes. That's why we call them "variables"! As an example, study the following table that
traces the value of a variable called X.
Description Value of Program
X
• When the program begins, no variables exist. In
RAPTOR, variables are automatically created when
Undefined
they are first used in a statement.

• The first assignment statement, X←32, assigns the data


32
value 32 to the variable X.
• The next assignment statement, X←X+1, retrieves the
current value of X, 32, adds 1 to it, and puts the result, 33
33, in the variable X.
• The next assignment statement, X←X*2, retrieves the
current value of X, 33, multiplies it by 2, and puts the 66
result, 66, in the variable X.

2 of 12
During the execution of the previous example program, the variable X stored three distinct
values. Please note that the order of statements in a program is very important. If you re-ordered
these three assignment statements, the values stored into X would be different.

A variable can have its value set (or changed) in one of three ways:
• By the value entered from an input statement.
• By the value calculated from an equation in an assignment statement.
• By a return value from a procedure call (more on this later).
It is variables, and their changing data values, that enable a program to act differently every time
it is executed.

All variables should be given meaningful and descriptive names by the programmer. Variable
names should relate to the purpose the variable serves in your program. A variable name must
start with a letter and can contain only letters, numerical digits, and underscores (but no spaces or
other special characters). If a variable name contains multiple "words," the name is more
"readable" if each word is separated by an underscore character. The table below shows some
examples of good, poor, and illegal variable names.

Good variable names Poor variable names Illegal variable names


tax_rate a (not descriptive) 4sale (does not start with a letter)
sales_tax milesperhour (add underscores) sales tax (includes a space)
distance_in_miles my4to (not descriptive) sales$ (includes invalid character)
mpg

IMPORTANT: If you give each value in a program a meaningful, descriptive variable name, it
will help you think more clearly about the problem you are solving and it will help you find
errors in your program.

One way of understanding the purpose of variables is to think of them as a means to


communicate information between one part of a program and another. By using the same
variable name in different parts of your program you are using the value that is stored at that
location in different parts of your program. Think of the variable as a place holder or storage
area for values between each use in your program computations.

When a RAPTOR program begins execution, no variables exist. The first time RAPTOR
encounters a new variable name, it automatically creates a new memory location and associates
this variable name with the new memory. The variable will exist from that point in the program
execution until the program terminates. When a new variable is created, its initial value
determines whether the variable will store numerical data or textual data. This is called the
variable's data type. A variable's data type cannot change during the execution of a program. In
summary, variables are automatically created by RAPTOR and can hold either:
• Numbers e.g., 12, 567, -4, 3.1415, 0.000371, or
• Strings e.g., “Hello, how are you?”, “James Bond”, “The value of x is ”

3 of 12
Common errors when using variables:
Error 1: "Variable ____ does not have a value"
There are two common reasons for this error:

1) The variable has not been given a value. 2) The variable name was misspelled.

Error 2: "Can't assign string to numeric variable _____"


"Can't assign numeric to string variable _____"
This error will occur if your statements attempt to change the data type of a variable.

4 of 12
RAPTOR Statements/Symbols

The following four sections provide details about each of the four basic statements: Input,
Assignment, Call, and Output.

Input Statement/Symbol

An input statement/symbol allows the user of a program to enter a data value into a program
variable during program execution. It is important that a user know exactly what type of value is
expected for input. Therefore, when you define an
input statement you specify a string of text that will
be the prompt that describes the required input. The
prompt should be as explicit as possible. If the
expected value needs to be in particular units (e.g.,
feet, meters, or miles) you should mention the units
in the prompt.

When you define an input statement, you must


specify two things: a prompt and the variable that
will be assigned the value enter by the user at run-
time. As you can see by the “Enter Input” dialog
box at the right there are two types of input
prompts: Text and Expression prompts. An
Expression prompt enables you to mix text and
variables together like the following prompt:
“Enter a number between ” + low +
“ and ” + high + “: ”.

At run-time, an input statement will display an


input dialog box, an example of which is shown
to the right. After a user enters a value and hits
the enter key (or clicks OK), the value entered
by the user is assigned to the input statement's
variable.

Make sure you distinguish between the "definition of a statement" and the "execution of a
statement". The dialog box that is used to define a statement is totally different from the dialog
box that is used at run-time when a program is executing.

5 of 12
Assignment Statement/Symbol

The assignment symbol is used to perform a computation and then store the results in a variable.
The definition of an assignment statement is performed using the dialog box shown on the right.
The variable to be assigned a value is entering
into the "Set" field, and the computation to
perform is enter into the "to" field. The
example on the right sets the value of the
variable x to 0.707106781186547.

An assignment statement is displayed inside its


RAPTOR symbol using the syntax:

Variable ← Expression

For example, the statement created by the


dialog box to the right is displayed as:

One assignment statement can only change the


value of a single variable, that is, the variable
on the left hand side of the arrow. If this
variable did not exist prior to the statement, a
new variable is created. If this variable did
exist prior to the statement, then its previous
value is lost and its new value is based on the
computation that is performed. No variables on
the right hand side of the arrow (i.e., the
expression) are ever changed by the
assignment statement.

Expressions
The expression (or computation) of an assignment statement can be any simple or complex
equation that computes a single value. An expression is a combination of values (either constants
or variables) and operators. Please carefully study the following rules for constructing valid
expressions.

A computer can only perform one operation at a time. When an expression is computed, the
operations of the equation are not executed from left to right in the order that you typed them in.
Rather, the operations are performed based on a predefined "order of precedence." The order that
operations are performed can make a radical difference in the value that is computed. For
example, consider the following two examples:
x ← (3+9)/3 x ← 3+(9/3)

6 of 12
In the first case, the variable x is assigned a value of 4, whereas in the second case, the variable x
is assigned the value of 6. As you can see from these examples, you can always explicitly control
the order in which operations are performed by grouping values and operators in parenthesis.
The exact "order of precedence" is
1. compute all functions, then
2. compute anything in parentheses, then
3. compute exponentiation (^,**) i.e., raise one number to a power, then
4. compute multiplications and divisions, left to right, and finally
5. compute additions and subtractions, left to right.

An operator or function directs the computer to perform some computation on data. Operators
are placed between the data being operated on (e.g. X/3) whereas functions use parentheses to
indicate the data they are operating on (e.g. sqrt(4.7) ). When executed, operators and
functions perform their computation and return their result. The following lists summarize the
built-in operators and functions of RAPTOR.
basic math: +, -, *, /, ^, **, rem, mod, sqrt, log, abs, ceiling, floor
trigonometry: sin, cos, tan, cot, arcsin, arcos, arctan, arccot
miscellaneous: random, Length_of

The following table briefly describes these built-in operators and functions. Full details
concerning these operators and functions can be found in the RAPTOR help screens.

Operation Description Example


+ addition 3+4 is 7
- subtraction 3-4 is -1
- negation -3 is a negative 3
* multiplication 3*4 is 12
/ division 3/4 is 0.75
^ exponentiation, raise a number to a 3^4 is 3*3*3*3=81
** power 3**4 is 81
rem remainder (what is left over) when 10 rem 3 is 1
mod the right operand divides the left 10 mod 4 is 2
operand
sqrt square root sqrt(4) is 2
log natural logarithm (base e) log(e) is 1
abs absolute value abs(-9) is 9
ceiling rounds up to a whole number ceiling(3.14159) is 4
floor rounds down to a whole number floor(9.82) is 9
sin trig sin(angle_in_radians) sin(pi/6) is 0.5
cos trig cos(angle_in_radians) cos(pi/3) is 0.5
tan trig tan(angle_in_radians) tan(pi/4) is 1.0
cot trig cotangent(angle_in_radians) cot(pi/4) is 1
arcsin trig sin-1(expression), returns radians arcsin(0.5) is pi/6
arcos trig cos-1(expression), returns radians arccos(0.5) is pi/3

7 of 12
arctan trig tan-1(y,x), returns radians arctan(10,3) is 1.2793
arccot trig cot-1(x,y), returns radians arccot(10,3) is 0.29145
random generates a random value in the random * 100 is some value
range [1.0, 0.0) between 0 and 99.9999
Length_of returns the number of characters in a Example ← "Sell now"
string variable Length_of(Example) is 8

The result of evaluating of an expression in an assignment statement must be either a single


number or a single string of text. Most of your expressions will compute numbers, but you can
also perform simple text manipulation by using a plus sign (+) to join two or more strings of text
into a single string. You can also join numerical values with strings to create a single string. The
following example assignment statements demonstrate string manipulation.
Full_name ← "Joe " + "Alexander " + "Smith"
Answer ← "The average is " + (Total / Number)

RAPTOR defines several symbols that represent commonly used constants. You should use
these constant symbols when you need their corresponding values in computations.
pi is defined to be 3.14159274101257.
e is defined to be 2.71828174591064

Procedure Call Statement/Symbol

A procedure is a named collection of programming statements that accomplish a task. Calling a


procedure suspends execution of your program, executes the instructions in the called procedure,
and then resumes executing your program at the
next statement. You need to know two things to
correctly use a procedure: 1) the procedure's
name and 2) the data values that the procedure
needs to do its work, which are called
arguments.

RAPTOR attempts to minimize the number of


procedure names you need to memorize by
displaying any procedure name that partially
matches what you type into the "Enter Call"
window. For example, after entering the single
letter "d," the lower portion of the window will
list all built-in procedures that start with the
letter "d". The list also reminds you of each
procedure's required arguments. In the example
to the right, the lower box is telling you that the
"Draw_Line" procedure needs 5 data values: the
x and y coordinates of the starting location of
the line, (x1, y1), the x and y coordinates of the
ending location of the line, (x2, y2), and the

8 of 12
line's color. The order of the argument values must match the arguments defined by the
procedure. For example, Draw_Line(Blue, 3, 5, 100, 200) would generate an error
because the color of the line must be the last argument value in the argument list.

When a procedure call is displayed in your RAPTOR program you can see the procedure's name
and the argument values that will be sent to the procedure when it is
called. For example, when the first procedure call on the right is
executed it will draw a red line from the point (1,1) to the point
(100,200). The second procedure call will also draw a line, but since the
arguments are variables, the exact location of the line will not be known
until the program executes and all the argument variables have a value.

RAPTOR defines too many built-in procedures to describe them all here. You can find
documentation on all built-in procedures in RAPTOR's help screens. In addition, your instructor
will introduce relevant procedures as we tackle various problem solving tasks in the coming
lessons.

Output Statement/Symbol

In RAPTOR, an output statement displays a value to the MasterConsole window when it is


executed. When you define an output statement,
the "Enter Output" dialog box asks you to
specify three things:
• Are you displaying text, or the results of
an expression (computation)?
• What is the text or expression to display?
• Should the output be terminated by a
new line character?
The example output statement on the right will
display the text, "The sales tax is" on the output
window and terminate the text with a new line.
Since the "End current line" is checked, any
future output will start on a new line below the
displayed text.

When you select the "Output Text" option, the


characters that you type into the edit box will be
displayed exactly as you typed them, including
any leading or trailing spaces. If you include
quote marks (") in the text, the quote marks will
be displayed exactly as you typed them.

9 of 12
When you select the "Output Expression" option, the text you type into the edit box is treated as
an expression to be evaluated. When the output statement is executed at run-time, the expression
is evaluated and the resulting single value that
was computed is displayed. An example output
statement that displays the results of an
expression is shown on the right.

You can display multiple values with a single


output statement by using the "Output
Expression" option and building a string of text
using the string plus (+) operator. When you
build a single string from two or more values,
you must distinguish the text from the values to
be calculated by enclosing any text in quote
marks ("). In such cases, the quote marks are not
displayed in the output window. For example,
the expression,

"Active Point = (" + x + "," + y + ")"

will display the following if x is 200 and y is 5:

Active Point = (200,5)

Notice that the quote marks are not displayed on


the output device. The quote marks are used to
surround any text that is not part of an
expression to be evaluated.

Your instructor (or a homework assignment) will


often say “Display the results in a user-friendly
manner”. This means you should display some explanatory text explaining any numbers that are
output to the MasterConsole window. An example of "non-user-friendly output" and "user-
friendly output" is shown below.

Non-user-friendly output User-friendly output

Example output: 2.5678 Example output: Area = 2.5678 square inches

10 of 12
Comments in RAPTOR

The RAPTOR development environment, like many other programming languages, allows
comments to be added to your program. Comments are used to explain some aspect of a program
to a human reader, especially in places where the program code is complex and hard to
understand. Comments mean nothing to the computer and are not executed. However, if
comments are done well, they can make a program much easier to understand for a human
reader.

To add a comment to a statement, right-click your mouse over the


statement symbol and select the "Comment" line before releasing
the mouse button. Then enter the comment text into the "Enter
Comment" dialog box, an example of which is shown to the right.
The resulting comment can be moved in the RAPTOR window by
dragging it, but you typically do not need to move the default
location of a comment.

There are three general types of comments:


• Programmer header – documents who wrote the program, when it was written, and a
general description of what the program does. (Add to the "Start" symbol)
• Section description – mark major sections of your program to make it easier for a
programmer to understand the overall program structure.
• Logic description – explain non-standard logic.
Typically you should not comment every statement in a program. An example program that
includes comments is shown below.

11 of 12
What you have hopefully learned…

• The basic structure and types of statements of a RAPTOR program.

• What a variable is and how variables are used.

• How to write computations (i.e., expressions) that calculate desired values.

• How to get input values into a program and how to display output values.

• How to add appropriate comments to make a program more readable.

Reading Self-Check

1. Label the following RAPTOR identifiers as (G) good, (P) poor, or (I) Illegal. If illegal then
explain why.
____ 1) This_Is_A_Test
____ 2) U_2
____ 3) Money$
____ 4) Thisisanawfullylongidentifiername
____ 5) Mickey-Mouse
____ 6) 365_Days
____ 7) Variable
____ 8) Is This Identifier Legal
____ 9) Why_Isn’t_This_One_Legal

2. Why are comments important?

3. True or False. In RAPTOR, a variable does not have a value (in its memory location) until a
program instruction gives it a value.

4. Calculate the result of the following expressions (or indicate if the expression contains errors)

Result
__________ 1) 46 / 2
__________ 2) 4 + 6 * 2
__________ 3) 12 / 3 / 2
__________ 4) (4 + 2) / (5 – 3) + 2
__________ 5) 46 / 3
__________ 6) 46 rem 3
__________ 7) 3(4 + 3)
__________ 8) 6 ** sqrt(4)
__________ 9) 77 + -11

12 of 12
Unit 1
1) Computational Thinking

Computational thinking is the step that comes before programming. It is the process of breaking down a
problem into simple enough steps that even a computer would understand. Computational thinking
means thinking or solving problems like computer scientists. CT refers to thought processes required
in understanding problems and formulating solutions. CT involves logic, assessment, patterns,
automation, and generalization.

2) Defining the following terms:

a) Stage

The stage is basically the background of your project. Like sprites, the stage can have different
costumes that change as the story plays out. The stage window is the main area where the
action of your program takes place.

b) Sprite

Sprites are the images on a Scratch computer program screen. Every Scratch program is made
up of sprites and the scripts (instructions) that control them. Scripts are programmed to make
the sprites do things.

c) The script

A script is defined within the Scratch program as one or a set of blocks. Even a single block can
qualify. However, scripts are usually referred to as sets of blocks that consist of at least two
blocks.

d) The programming palette

The block palette is where the different script blocks are located. The different types of
script blocks include motion, control, looks, sensing, sound, operators, pen, variables.

3) List the various code blocks in scratch and elaborate on any 4.

There are 10 categories:


Motion, Looks, Sound, Event, Control, Sensing, Operators, Variables, List, and My Blocks.

a) Motion

They are color-coded medium-blue and are used to control a sprite's movement. They are
available only for sprites.
b) Event

Events blocks are one of the ten categories of blocks. They are color-coded light yellow and are
used to sense events, which trigger scripts to run. Event blocks are essential for every project:
without the hat blocks from this category, a project would not be able to begin except by
manually running scripts.

There are currently 8 Events blocks: 6 Hat blocks (these blocks are used to start scripts) and
2 Stack blocks. It is the smallest category of blocks.

c) Control

Control blocks are color-coded gold and are used to control scripts.
Functions: The block pauses its script for the specified number of seconds — the wait can also
be a decimal number. Blocks held inside this block will loop a given number of times, before
allowing the script to continue.

d) Operator

Operators blocks is one of the ten categories of Scratch blocks. They are color-coded light-green
and are used to script math equations and string handling.

4) Differentiate between sensing and looks block.

Looks block are color-coded purple and are used to control a sprite's appearance.
Function: The block displays a speech bubble with the specified text for the sprite that runs it, which
appears on the screen for the specified number of seconds.

Sensing blocks are color-coded light-blue and are used to detect different factors of a project.
Function: The block checks if its sprite is touching the mouse-pointer, edge, or another sprite.

5) Explain the various operators available in scratch.

The () < () [less than] block is an Operators block and a Boolean block. The block reports true if the first
value is less than the second value and false otherwise.

The () = () [equals to] block is an Operators block and a Boolean block. The block checks if the first value
is equal to the second value. If the values are equal, the block returns true; otherwise, it returns false.
This block is case-insensitive, meaning that capital and lowercase letters are treated the same.

The () and () [and] Block is an Operators Block and a Boolean Block. The block joins two boolean blocks
so they both have to be true to return true. If they are both true, the block returns true; if only one is true
or none are true, it returns false.

The Join ()() [join] block is an Operators block and a Reporter block. The block concatenates, or "links"
the two values together and reports the result — for example, if "hello" and "world" were put in the block,
it would report "helloworld". To report "hello world", use either "hello " and "world" or "hello" and " world",
with a space.
6) Explain the motions block in detail

Motion blocks are used to control a sprite’s movement. They can be used to move a sprite a certain
number of steps, rotate a sprite in a certain direction to a specified degree, or they can tell a sprite to go
to a certain specified coordinate. They can also be used to change the rate at which the sprite moves,
such as the rate at which a sprite moves to another sprite. Additionally, these blocks can also tell a sprite
to bounce off the screen if it is touching the edge of the screen.

7) What are the different events that can occur in scratch

The When Green Flag Clicked block, commonly called the Start Block.

The when () key pressed block is a Hat block and an Events block. Scripts placed underneath this block
will activate when the specified key is pressed.

The When This Sprite Clicked block is an Events block and a Hat block. Scripts that wear the block will
activate once its sprite or clone of the sprite is clicked.

The When Backdrop Switches to () block is an Event block and a Hat block. Scripts that wear this block
will be triggered once the specified backdrop has been switched to on the Stage.

The When () > () block is an Events Block and a Hat Block. It starts the script below it when a value
(chosen by the dropdown menu) is greater than another value (entered by the number input). Its options
are loudness, timer.

The when I receive () block is a hat block and an events block. The block activates its script when the
specified broadcast has been sent by a calling script.

The Broadcast () block is an Events block and a stack block which sends a broadcast throughout the
whole Scratch program.

Any scripts in any sprites that are hatted with the when I receive () block that is set to a specified
broadcast will activate.

8) Explain how input is done in scratch

The Ask () and Wait block is a Sensing block and a Stack block. The block will make the sprite using the
block say the question and show an input box at the bottom of the screen. If the question is being asked
by the stage, the question will appear above the input instead. Scratchers can input text into it and submit
it, and the input is stored then in the Answer block. The Answer block automatically updates to most
recent input.

9) Define a variable? How value is assigned and changed for a variable in scratch

A variable is a changeable value recorded in Scratch's memory. Variables can only hold one value at a
time, unlike lists. These values can be either numbers or strings, their values can be input by the user via
the answer block. Clicking on an isolated variable in the scripts area displays a small bubble reporting the
value of the variable. Unlike many other programming languages, Scratch does not allow variables to be
created by a script as it runs. Instead, variables are created with the "Make a Variable" button in
the Block Palette. This results in only a small amount of RAM being used to store the value for use when
the project actually runs.
10) Illustrate the usage of my blocks in scratch

It holds procedures for the selected sprite. They are color-coded pink. Before any blocks are created, it is
empty, except for a "Make a Block" button.
Clicking Make a Block brings up a dialogue allowing the user to make a procedure. Once OK is pressed,
the new block appears in the palette and an empty definition appears in the code area. When the
procedure runs, Scratch will run the blocks below the corresponding Define block.
For example, suppose one wants a sprite to vanish and reappear. To vanish, the sprite should play a
sound and repeatedly increase the ghost effect; to reappear, it should repeatedly decrease the ghost
effect. This should happen at several points in the project. Without procedures, the Scratcher would have
to duplicate the same sequence of blocks at each point, but by making "vanish" and "appear" blocks, they
can write scripts in a way which matches how they think of them.

11) What are the options in pen block and explain how a geometrical shape is drawn in scratch?

 Erase All — Clears all pen marks on the screen.


 Pen Down — Turns the pen feature on inside a sprite; the sprite will pen on the screen
wherever it moves until the pen is turned off.
 Pen Up — Turns the pen feature off, stopping a sprite from penning.
 Set Pen Color to () — Sets the color of the pen to a predetermined color.
 Change Pen () by () — Changes a property of the pen by a given input.
 Set Pen () to () — Sets a property of the pen to a given input.
 Change Pen Size by () — Changes the size of the pen by a chosen number.
 Set Pen Size to () — Sets the size of the pen to a chosen number.
 Stamp — Draws a copy of the Sprite on the Stage.

By using Pen down, then with the use of a repeat control block, a move motion block and turn motion
block; the user sets the distance or number of steps the pen sprite should move and then sets the
degree to which angle it should turn, and the user sets how many times this process repeats. For
example, when drawing a triangle, the repeat block is set to ‘3’, the move block can be set to any
specified distance such as ‘80’ and the turn block is set to ‘120’ degrees.

12) Explain repeat and forever blocks with example

The Repeat () block is a Control block and a C block [A C block is a block that is shaped like a "C", so
other blocks can fit inside it. These blocks perform the conditions and loops]. Blocks held inside this block
will loop a given number of times, before allowing the script to continue.
If a decimal is put in, the number is rounded up. Furthermore, when a non-positive number is input, the
loop does not run, and if "Infinity" is input, then the block runs forever. For example:

Animation — rather than coding each costume change and delay individually, the Repeat () block
(with the Next Costume block) can be used. This can reduce unnecessary scripting.

Repeat (8)
wait (0.05) secs
next costume
The Forever block is a Control block, a C block, and a Cap Block [cap block is a block that is
designed to stop a block from being placed underneath it]. Blocks held inside this block will be in
a loop — just like the Repeat () block and the Repeat Until () block, except that the loop never
ends (unless the stop sign is clicked, the Stop All block is activated, or the stop script block is
activated within the loop). Due to this infinite loop, the block has no bump at the bottom; having a
bump would be pointless, as the blocks below it would never be activated. For example:

Keeping a sprite at another’s location:

Forever
go to (sprite1)

13) What is a backdrop? Explain the process of changing a backdrop.

A backdrop is an image that can be shown on the Stage. It is similar to a costume, except that it is
shown on the stage instead. They are in the backdrops library.
The Stage can change its look to any of its backdrops using the Switch Backdrop to () block. They can be
named, edited, created, and deleted in the Paint Editor.
Because of its simplicity, this block does not have a specific list of main uses; it is simply used to change
the background of the Stage. It can be used in animations, games, simulations—anything that requires
changes between backgrounds. For example:

# Starting a level

Switch backdrop to (level 1)

When backdrop switches to (level 1) # in the sprite


go to x:(), y:() # any specified coordinate
Unit II: Algorithms and Flowchart design through Raptor
Introduction to the idea of an algorithm. Pseudo code and Flow charts. Flow chart
symbols, Input/Output, Assignment, operators, conditional if, repetition, procedure and
sub charts.
Example problems – Finding maximum of 3 numbers, Unit converters, Interest
calculators, multiplication tables, GCD of 2 numbers
Example problems -- Fibonacci number generation, prime number generation.
Minimum, Maximum and average of n numbers, Linear search, Binary Search.
---------------------------------------------------------------------------------------------------------
ALGORITHMS

An algorithm is a step-by-step procedure to perform a given task. Each step in an algorithm is


known as instruction.

Characteristics of an algorithm:

1. INPUT: Any algorithm accepts zero or more inputs.


2. OUTPUT: Any algorithm generates one or more outputs.
3. FINITENESS: Algorithm should be terminated in finite number of steps.
4. DEFINITENESS: The steps in the algorithm should be defined clearly i.e. no ambiguity
should be raised
5. EFFECTIVENESS: Each step in the algorithm should be precise so that they can be
easily traced on a paper.

Instruction can be divided into 3 types:

1. Sequence(also known as process)


2. Decision (also known as Selection)
3. Iteration or Repetition(also known as looping)

Sequence : Sequence means that each step or process in the algorithm is executed in the
specified order.

Write an algorithm for to find sum of two numbers.

Step1 : Start

Step 2: Read num1,num2

Step 3: compute sumnum1+num2

Step 4: Print sum


Step 6: Stop

More Examples:

Write an algorithm for

1. To find product of two numbers.

2. To compute area of triangle.

3.To swap two and three numbers with using temporary variable.

4. To swap two and three numbers without using temporary variable.

Selection : Selection means that in the algorithm the steps will be executed based on the
condition. The selection staement is written using If (cond) then Task1 otherwise task2 .

If condition is true then statements after then clause will be executed, if condition is false
then statement after otherwise clause will be executed.

Example: Write an algorithm for to check whether entered number is positive or negative.

Step1: Start

Step 2: Read num

Step 3: If num>= 0 then

print “ entered value is positive”

otherwise

Print “ entered number is negative”

Step 4:Stop

More Examples:

Write an algorithm for the following

1.To check whether entered number is even or odd.

2.To check whether person is eligible for voting.

3.To check whether entered year is leap year.

4.To find maximum of two number.


5.To find maximum of three numbers.

Iteration: Iteration process is used when an instruction or set of instructions are to be


repeated until some condition is satisfied.

Iteration can be illustrated using 3 ways:

1.It has the general form 2. Second form

Repeat while Condition

Step 1 begin

Step 2 Step1

…… ……..

Until Condition end.

3. Third form

Step 1

Step 2

….

Step N

if cond then goto Step 1.

Example:

Write an algorithm to print your name for 5 times.

Step 1: Start

Step 2: Count0

Step 3: Print “XYZ”

Step 4:Count=Count+1

Step 5: If Count<=5 then goto Step 3

Step 6: Stop
More Examples:

1.Write an algorithm to print your name for N times where N is the value entered by user.

2.Write an algorithm to print first n natural numbers where value of n is entered by user.

3.Write an algorithm to find sum of first n natural numbers where value of n is entered by
user.

4.Write an algorithm to find sum of n different numbers .

5.Write an algorithm to count number of even numbers between given range.

6.write an algorithm to compute factorial of given number.

7.Write an algorithm to generate Fibonacci series.

FLOWCHART

Pictorial representation of algorithm is known as flowchart.

RAPTOR (Rapid Algorithmic Prototyping Tool for Ordered Reasoning) is a free graphical
authoring tool created by Martin C. Carlisle, Terry Wilson, Jeff Humphries and Jason Moore,
designed specifically to help students visualize their algorithms.

Symbols in RAPTOR:
Raptor has 6 types of symbols, each of which represents a unique kind of instruction. They are –
Assignment, Call, Input, Output, Selection and Loop symbols. The following image shows these
symbols-
The 6 primary graphical symbols that can be used in Raptor:
1. Assignment: The assignment symbol is used to give a variable a numeric or string value.
The assignment symbol is used to change the value of a variable. The right hand side of the
assignment is evaluated, and the resulting value is placed in the variable on the left hand side.
For example, consider the case where the value of x is currently 5, and the assignment "x <- x +
1” is executed. First "x+1" is evaluated, yielding the result 6. Then the value of x is changed to
be 6.
Note: The assignment is very different from mathematical equality. The statement should be read
"Set x to x+1" instead of "x equals x+1". The assignment symbol can also be used to assign a
string expression to a string variable.
2. Call: The call symbol is used to make calls to outside procedures, such as graphics routines.
The call symbol is used to invoke procedures such a s graphics routines and other
instructor-provided procedures. The call symbol is also used to run sub charts included in a
Raptor program.
3. Input: The input symbol is used for getting input from the user.
The input symbol is used to ask the user for a number or string while the flowchart is
executing. When an input symbol is executed, the user will be prompted with a dialog to
enter a value that can be interpreted as either a number or string, depending on what the user.
The user can also override the source for input by specifying a text file to be used in place of the
keyboard.
4. Output: The output symbol is used to display text to the Master Console.
The output symbol is used to either write a number or text to the Master Console window. The
user can also override the destination for output by specifying a text file to be used instead of the
Master Console.
5. Selection: The selection structure is used for decision making
The programmer enters in the diamond an expression that evaluates to Yes (True) or No (False).
Such expressions are formally referred to as Boolean expressions. Based on the result of the
expression in the diamond, control of the program will branch either left (Yes, or True) or right
(No, or False).
6 .Loop or Control: The loop structure is used for iteration and repetition. The loop structure is
used to repeat a sequence of symbols until a certain condition is met. When execution
reaches the bottom of the loop, it starts over again at the top. The loop is exited when the
diamond symbol is executed and the Boolean expression in the diamond evaluates to Yes (True).

A RAPTOR program consists of connected symbols that represent actions to be executed.


1. The arrows that connect the symbols determine the order in which the actions are
performed.
2. The execution of a RAPTOR program begins at the Start symbol and goes along the
arrows to execute the program.
3. The program stops executing when the End symbol is reached.
Unit III: Introduction to Python
Python – Numbers, Strings, Variables, operators, expressions,
statements, String operations, Math function calls, Input/Output
statements, Conditional If, while and for loops, User defined Functions,
parameters to functions, recursive functions, Turtle Graphics.

Questions:
1.Define a variable and list out the rules to name a variable.
A) A Python variable can be defined as a reserved memory location to store
values. In other words, a variable in a python program gives data to the
computer for processing.

2.Define operator. Write a short notes on the various operators available in


python?
3.Elaborate on the input and output statement?
A) A function is defined as a block of organized, reusable code used to perform a single, related
action. Python has many built-in functions; you can also create your own.
1)INPUT: Python has an input function which lets you ask a user for some text input. You call
this function to tell the program to stop and wait for the user to key in the data.We use the built-in
function input() to ask the user for input.
Code:

To capture the input in your program, you will need a variable. A variable is a container to hold
data. You can take the input and assign it to a variable. This is done using the = operator before
the input keyword and then putting the variable name before the = operator. For example, when
you give a sentence "generic input" as the input, this gets assigned to a variable, say, my_var.
You can then print the value stored in my_var.
2)Output: To output your data to the screen, use the print() function. You can
write print(argument) and this will print the argument in the next line.
For example, if your data is "Guido," you can put "Guido" inside the parentheses (
) after print.
4.Explain various string operations?
A) A string in Python is a sequence of characters. It is a derived data type.
Strings are immutable. This means that once defined, they cannot be changed.
Some string operations are:
1)

2)
3)

4)

5)
6)

7)

8)
9)

10)

11)
12)

5.Write a short note on various decision-making statements in python.


6.Write a short note on loops.

1)
Output:

Output:

Output:

Output:
2)FOR Loop

Output:
apple
banana
cherry

Output:
b
a
n
a
n
a

Output:
apple
banana

Output:
apple

Output:
apple
cherry
Output:
0
1
2
3
4
5

Output:
2
3
4
5

Output:
2
5
8
11
14
17
20
23
26
29

Output:
0
1
2
3
4
5
Finally finished!

Output:
0
1
2

Output:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
7.Define function? Elaborate on function call, function header , function
definition

This is the function header.


8.How recursive functions differ from functions.
9.Write a short notes on Turtle graphics?
A) “Turtle” is a Python feature like a drawing board, which lets us command a turtle
to draw all over it! We can use functions like turtle.forward(…) and turtle.right(…)
which can move the turtle around.Commonly used turtle methods are :

Some examples are:


10.List out the various math function calls.
UNIT-4

You might also like