<< Chapter < Page Chapter >> Page >

Operator categories

There are several different kinds of operators. The easiest way to study them is to divide them into categories such as the following

  • arithmetic
  • relational
  • logical
  • bitwise
  • assignment

An earlier module explained arithmetic operators. This module will explain relational operators. Future modules will deal with the other kinds of operators.

A previous module introduced you to the selection structure. This module will expand on that concept.

Preview

In this module, I will present and explain a Scratch program named Relational01 . This program illustrates the use of the following relational operators:

<(less than) = (equal to)>(greater than)

The program creates the following five variables and a button and displays them on the Stage as shown in Image 1 and Image 6 :

  • LeftOperand - a slider
  • RightOperand - a slider
  • LessThan
  • Equals
  • GreaterThan

Image 1. reduced screen shot of program relational01 in operation.

Missing image.
Image 1. Reduced screen shot of program Relational01 in operation.

Program operation

When the user clicks the green flag, the values of the variables shown in Image 6 are set to 0.

The user slides the two sliders to set the values of LeftOperand and RightOperand .

When the user clicks the button, three separate event handlers on the button test the left operand against the right operand for less than , equal to , and greater than and display the results in the three variables having the corresponding names. A 0 is displayed if the value is false anda 1 is displayed if the value is true. Those three event handlers are shown as the bottomthree scripts in the center panel of Image 1 .

No boolean type available in Scratch

Many other programming languages have a boolean type. The boolean type can take on values of true and false . Scratch doesn't have a boolean type for variables. (The only variable types in Scratch are numeric and string.)

The boolean type is commonly used in some kind of a test to determine what to do next.

Because of the lack of a boolean type in Scratch, you must improvise. As you will see in this program, we will use a numeric value of 0 to represent false and a numeric value of 1 to represent true .

We could also have used the string "False" to represent false and the string "True" to represent true . However, 0 and 1 have historically been used to represent false and true in programming languages that didn't have a true boolean type so I decided to use thatconvention here also.

Discussion and sample code

A button and five variables

As you can see in the bottom right of Image 1 , a button was added to this program. If you look very carefully at the left panel in Image 1 , you will see that five variables were created for the program and that all fiveof the variables were displayed in the Stage area in the upper right. (The checkboxes for all five variables were checked causing them to appear in the Stage area.) A better view of the Stage is provided in Image 6 .

The names of the five variables were listed above . By this point, you should have no difficulty creating variables and causing them to bedisplayed in the Stage, so no explanation should be necessary.

Questions & Answers

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask