• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Booleans and Logic [Rough Draft]

Status
Not open for further replies.
Level 40
Joined
Dec 14, 2005
Messages
10,532
Booleans are one of the most fundamental data types to any programming language and represent the truth or falsity of a statement. In JASS, the two possible values are written as true and false. The simplicity of this data type hides its true power.

Many structures are able to undergo execution based on a condition—that is, the truth or falsity of a boolean expression. These structures will be covered later, but this is the main use for booleans.

Booleans may be generated by several means (not including typing them out literally):

Comparisons

There are six comparative operators in JASS: == (equals, math: =), != (does not equal, math: ≠), > (greater than), < (less than), >= (greater than or equal to, math ≥), <= (less than or equal to, math ≤). The last four of these operators can only be used to compare integers and reals—that is, numbers. The first two may compare any two data types which are the same (numbers may be compared to one another regardless of whether they are both integers, reals, or a combination of the two).

For example: 5 > 4 would evaluate to true, while CreateTrigger()==CreateTrigger() would evaluate to false (as the two instances are different objects).

Logic Operators


The six comparative operators only operate on non-boolean types (with the exception of == and !=). However, there are three far more powerful operators for comparing booleans.

and

The and operator compares two booleans and returns true if and only if they are both true, as the name implies. The truth table for and thus looks like:

ABA and B

T
TT

F
TF

T
FF

F
FF

or

The or operator compares two booleans and returns true if either one is true. However, or is "inclusive" unlike the way we use the word in everyday speech—both booleans may be true. The truth table for or thus looks like:

ABA or B

T
TT

F
TT

T
FT

F
FF

not

The not operator negates a single boolean, as the name implies. The truth table is thus much simpler and looks like:

Anot A

T
F

F
T

Homework

  1. The xor operator, present in many languages, is not present in JASS. Standing for "exclusive or", it represents the way we use or in everyday English—it returns true if one of the two booleans it is comparing is true, but not both.
    • Write the truth table for A xor B

    • Assuming A and B are boolean variables, construct an expression equivalent to xor using any of and, or, and not.
      If you are stuck, try approaching it by using truth tables.

  2. The and and or operators are what is known as "short-circuit"—they will stop evaluating as soon as the answer is inevitable. Refer to their truth tables to see which values of A will guarantee a value of A <op> B regardless of the value of B where <op> is and or or.

    Suppose that printb() is a function which takes a boolean, displays it to the player (true="true",false="false") and then returns the boolean it took. Determine what output the player will see from the following statements:
    • printb(true) and printb(false)


    • printb(false) and printb(true)


    • printb(printb(false) or printb(true))


    • printb(false) or printb(false)


    • printb(printb(true) or printb(false))


  3. Using the printb() function defined in question 2, determine the output of the following pieces of code:
    • printb(1==1.2)


    • printb(1.0==1)


    • printb(CreateGroup() == CreateGroup())



    • JASS:
      local group g = CreateGroup()
      printb(g==g)
 
Status
Not open for further replies.
Top