<< 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.

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