Top Banner
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1
146

BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Jul 05, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

BOOLEAN OPERATIONSAND CONDITIONALS

CHAPTER 20

1

Page 2: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics

2

Page 3: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

2

Page 4: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting

2

Page 5: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

2

Page 6: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

2

Page 7: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

§ Comparison Operators

2

Page 8: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

§ Comparison Operators

§ Conditional Statements

2

Page 9: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

§ Comparison Operators

§ Conditional Statements– if

2

Page 10: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

§ Comparison Operators

§ Conditional Statements– if– if…else if…else

2

Page 11: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Topics§ Boolean Operations

– Shorting vs. Non-Shorting– Combination of Boolean Operations

• Order of Operations

§ Comparison Operators

§ Conditional Statements– if– if…else if…else– switch

2

Page 12: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

3

Page 13: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Operations that combine and compare bools

3

Page 14: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Operations that combine and compare bools

– !! The NOT Operator

3

Page 15: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Operations that combine and compare bools

– !! The NOT Operator

– &&! The AND Operator

3

Page 16: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Operations that combine and compare bools

– !! The NOT Operator

– &&! The AND Operator

– ||! The OR Operator

3

Page 17: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

4

Page 18: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

4

Page 19: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

– Pronounced either "not" or "bang"

4

Page 20: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

– Pronounced either "not" or "bang"– Reverses value of the bool

4

Page 21: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

– Pronounced either "not" or "bang"– Reverses value of the bool

! print( !true ); // Outputs: false! print( !false ); // Outputs: true! print( !(!true) ); // Outputs: true (the double negative of true)

4

Page 22: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

– Pronounced either "not" or "bang"– Reverses value of the bool

! print( !true ); // Outputs: false! print( !false ); // Outputs: true! print( !(!true) ); // Outputs: true (the double negative of true)

– Also called the "logical negation operator"

4

Page 23: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ !! The NOT Operator

– Pronounced either "not" or "bang"– Reverses value of the bool

! print( !true ); // Outputs: false! print( !false ); // Outputs: true! print( !(!true) ); // Outputs: true (the double negative of true)

– Also called the "logical negation operator"• This differentiates it from ~, the bitwise not operator

4

Page 24: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

5

Page 25: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ &&! The AND Operator

5

Page 26: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ &&! The AND Operator

– Returns true only if both operands are true

5

Page 27: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ &&! The AND Operator

– Returns true only if both operands are trueprint( false && false ); // falseprint( false && true ); // falseprint( true && false ); // falseprint( true && true ); // true

5

Page 28: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

6

Page 29: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ ||! The OR Operator

6

Page 30: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ ||! The OR Operator

– Returns true if either operand is true

6

Page 31: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ ||! The OR Operator

– Returns true if either operand is trueprint( false && false ); // falseprint( false && true ); // trueprint( true && false ); // trueprint( true && true ); // true

6

Page 32: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ ||! The OR Operator

– Returns true if either operand is trueprint( false && false ); // falseprint( false && true ); // trueprint( true && false ); // trueprint( true && true ); // true

– | (the pipe) is Shift-Backslash

6

Page 33: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ ||! The OR Operator

– Returns true if either operand is trueprint( false && false ); // falseprint( false && true ); // trueprint( true && false ); // trueprint( true && true ); // true

– | (the pipe) is Shift-Backslash• Just above the return or enter key on a US keyboard

6

Page 34: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

7

Page 35: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

7

Page 36: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators

7

Page 37: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated

7

Page 38: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

7

Page 39: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

– & and | are non-shorting operators

7

Page 40: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

– & and | are non-shorting operators• Both operands are evaluated regardless of value

7

Page 41: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

– & and | are non-shorting operators• Both operands are evaluated regardless of value

– & and | are also bitwise operators

7

Page 42: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

– & and | are non-shorting operators• Both operands are evaluated regardless of value

– & and | are also bitwise operators• & and | compare each bit of the values passed into them

7

Page 43: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Shorting vs. Non-Shorting Boolean Operators

– && and || are shorting operators• If the first operand of && is false, the second is not evaluated• If the first operand of || is true, the second is not evaluated

– & and | are non-shorting operators• Both operands are evaluated regardless of value

– & and | are also bitwise operators• & and | compare each bit of the values passed into them• Bitwise operators will be used much later when dealing with Unity

layers and collisions

7

Page 44: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations

8

Page 45: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

8

Page 46: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line

8

Page 47: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

8

Page 48: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

– Must follow order of operations

8

Page 49: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

– Must follow order of operations!! NOT&! Non-Shorting AND / Bitwise AND|! Non-Shorting OR / Bitwise OR&&! AND||! OR

8

Page 50: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

– Must follow order of operations!! NOT&! Non-Shorting AND / Bitwise AND|! Non-Shorting OR / Bitwise OR&&! AND||! OR

– The line above would be interpreted as:

8

Page 51: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

– Must follow order of operations!! NOT&! Non-Shorting AND / Bitwise AND|! Non-Shorting OR / Bitwise OR&&! AND||! OR

– The line above would be interpreted as:! bool tf = true || (false && true);! // true

8

Page 52: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Boolean Operations§ Combining Boolean Operations

– Can combine several on a single line! bool tf = true || false && true;

– Must follow order of operations!! NOT&! Non-Shorting AND / Bitwise AND|! Non-Shorting OR / Bitwise OR&&! AND||! OR

– The line above would be interpreted as:! bool tf = true || (false && true);! // true

– It's best to always use parentheses to enforce the order in which you want the evaluation to take place!

8

Page 53: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators

9

Page 54: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

9

Page 55: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)

9

Page 56: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To

9

Page 57: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To!=! Not Equal To

9

Page 58: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To!=! Not Equal To>! Greater Than

9

Page 59: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To!=! Not Equal To>! Greater Than<! Less Than

9

Page 60: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To!=! Not Equal To>! Greater Than<! Less Than>=! Greater Than or Equal To

9

Page 61: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ Allow the comparison of two values

§ Return a bool (either true or false)==! Is Equal To!=! Not Equal To>! Greater Than<! Less Than>=! Greater Than or Equal To<=! Less Than or Equal To

9

Page 62: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

10

Page 63: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value

10

Page 64: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value– bool, int, float, char, string, Vector3, Color, Quaternion

10

Page 65: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value– bool, int, float, char, string, Vector3, Color, Quaternion

§ More complex variables are compared by reference

10

Page 66: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value– bool, int, float, char, string, Vector3, Color, Quaternion

§ More complex variables are compared by reference– When variables are compared by reference, the comparison

is not of their internal values but of whether they point to the same location in memory

10

Page 67: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value– bool, int, float, char, string, Vector3, Color, Quaternion

§ More complex variables are compared by reference– When variables are compared by reference, the comparison

is not of their internal values but of whether they point to the same location in memory

– GameObject, Material, Renderer, HelloWorld (and other C# classes you write)

10

Page 68: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

COMPARISON BY VALUE OR REFERENCE

§ Simple variables are compared by value– bool, int, float, char, string, Vector3, Color, Quaternion

§ More complex variables are compared by reference– When variables are compared by reference, the comparison

is not of their internal values but of whether they point to the same location in memory

– GameObject, Material, Renderer, HelloWorld (and other C# classes you write) 1 GameObject go0 = Instantiate( boxPrefab ) as GameObject; 2 GameObject go1 = Instantiate( boxPrefab ) as GameObject; 3 GameObject go2 = go0; 4 print( go0 == go1 ); // Output: false 5 print( go0 == go2 ); // Output: true

10

Page 69: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators

11

Page 70: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

11

Page 71: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

– Returns true if the values or references compared are equivalent

11

Page 72: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

– Returns true if the values or references compared are equivalentprint( 10 == 10 ); // Outputs: Trueprint( 20 == 10 ); // Outputs: Falseprint( 1.23f == 3.14f ); // Outputs: Falseprint( 1.23f == 1.23f ); // Outputs: Trueprint( 3.14f == Mathf.PI ); // Outputs: False // Mathf.PI has more decimal places than 3.14f

11

Page 73: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

– Returns true if the values or references compared are equivalentprint( 10 == 10 ); // Outputs: Trueprint( 20 == 10 ); // Outputs: Falseprint( 1.23f == 3.14f ); // Outputs: Falseprint( 1.23f == 1.23f ); // Outputs: Trueprint( 3.14f == Mathf.PI ); // Outputs: False // Mathf.PI has more decimal places than 3.14f

– Do NOT confuse == and =

11

Page 74: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

– Returns true if the values or references compared are equivalentprint( 10 == 10 ); // Outputs: Trueprint( 20 == 10 ); // Outputs: Falseprint( 1.23f == 3.14f ); // Outputs: Falseprint( 1.23f == 1.23f ); // Outputs: Trueprint( 3.14f == Mathf.PI ); // Outputs: False // Mathf.PI has more decimal places than 3.14f

– Do NOT confuse == and = ==! The comparison operator

11

Page 75: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ ==! Is Equal To

– Returns true if the values or references compared are equivalentprint( 10 == 10 ); // Outputs: Trueprint( 20 == 10 ); // Outputs: Falseprint( 1.23f == 3.14f ); // Outputs: Falseprint( 1.23f == 1.23f ); // Outputs: Trueprint( 3.14f == Mathf.PI ); // Outputs: False // Mathf.PI has more decimal places than 3.14f

– Do NOT confuse == and = ==! The comparison operator =! The assignment operator

11

Page 76: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators

12

Page 77: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ !=! Not Equal To

12

Page 78: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ !=! Not Equal To

– Returns true if the values or references compared are NOT equivalent

12

Page 79: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ !=! Not Equal To

– Returns true if the values or references compared are NOT equivalentprint( 10 != 10 ); // Outputs: Falseprint( 20 != 10 ); // Outputs: Trueprint( 1.23f != 3.14f ); // Outputs: Trueprint( 1.23f != 1.23f ); // Outputs: Falseprint( 3.14f != Mathf.PI ); // Outputs: True

12

Page 80: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators

13

Page 81: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

13

Page 82: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

– Returns true if the first operand is greater than the second

13

Page 83: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

– Returns true if the first operand is greater than the secondprint( 10 > 10 ); // Outputs: Falseprint( 20 > 10 ); // Outputs: Trueprint( 1.23f > 3.14f ); // Outputs: Falseprint( 1.23f > 1.23f ); // Outputs: Falseprint( 3.14f > 1.23f ); // Outputs: True

13

Page 84: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

– Returns true if the first operand is greater than the secondprint( 10 > 10 ); // Outputs: Falseprint( 20 > 10 ); // Outputs: Trueprint( 1.23f > 3.14f ); // Outputs: Falseprint( 1.23f > 1.23f ); // Outputs: Falseprint( 3.14f > 1.23f ); // Outputs: True

§ <! Less Than

13

Page 85: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

– Returns true if the first operand is greater than the secondprint( 10 > 10 ); // Outputs: Falseprint( 20 > 10 ); // Outputs: Trueprint( 1.23f > 3.14f ); // Outputs: Falseprint( 1.23f > 1.23f ); // Outputs: Falseprint( 3.14f > 1.23f ); // Outputs: True

§ <! Less Than– Returns true if the first operand is less than the second

13

Page 86: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >! Greater Than

– Returns true if the first operand is greater than the secondprint( 10 > 10 ); // Outputs: Falseprint( 20 > 10 ); // Outputs: Trueprint( 1.23f > 3.14f ); // Outputs: Falseprint( 1.23f > 1.23f ); // Outputs: Falseprint( 3.14f > 1.23f ); // Outputs: True

§ <! Less Than– Returns true if the first operand is less than the second

print( 10 < 10 ); // Outputs: Trueprint( 20 < 10 ); // Outputs: Falseprint( 1.23f < 3.14f ); // Outputs: Trueprint( 1.23f < 1.23f ); // Outputs: Trueprint( 3.14f < 1.23f ); // Outputs: False

13

Page 87: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators

14

Page 88: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

14

Page 89: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

– True if the 1st operand is greater than or equal to the 2nd

14

Page 90: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

– True if the 1st operand is greater than or equal to the 2nd print( 10 >= 10 ); // Outputs: Trueprint( 20 >= 10 ); // Outputs: Trueprint( 1.23f >= 3.14f ); // Outputs: Falseprint( 1.23f >= 1.23f ); // Outputs: Trueprint( 3.14f >= 1.23f ); // Outputs: True

14

Page 91: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

– True if the 1st operand is greater than or equal to the 2nd print( 10 >= 10 ); // Outputs: Trueprint( 20 >= 10 ); // Outputs: Trueprint( 1.23f >= 3.14f ); // Outputs: Falseprint( 1.23f >= 1.23f ); // Outputs: Trueprint( 3.14f >= 1.23f ); // Outputs: True

§ <=! Less Than or Equal To

14

Page 92: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

– True if the 1st operand is greater than or equal to the 2nd print( 10 >= 10 ); // Outputs: Trueprint( 20 >= 10 ); // Outputs: Trueprint( 1.23f >= 3.14f ); // Outputs: Falseprint( 1.23f >= 1.23f ); // Outputs: Trueprint( 3.14f >= 1.23f ); // Outputs: True

§ <=! Less Than or Equal To– True if the 1st operand is less than or equal to the 2nd

14

Page 93: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Comparison Operators§ >=! Greater Than or Equal To

– True if the 1st operand is greater than or equal to the 2nd print( 10 >= 10 ); // Outputs: Trueprint( 20 >= 10 ); // Outputs: Trueprint( 1.23f >= 3.14f ); // Outputs: Falseprint( 1.23f >= 1.23f ); // Outputs: Trueprint( 3.14f >= 1.23f ); // Outputs: True

§ <=! Less Than or Equal To– True if the 1st operand is less than or equal to the 2nd

print( 10 <= 10 ); // Outputs: Trueprint( 20 <= 10 ); // Outputs: Falseprint( 1.23f <= 3.14f ); // Outputs: Trueprint( 1.23f <= 1.23f ); // Outputs: Trueprint( 3.14f <= 1.23f ); // Outputs: False

14

Page 94: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

15

Page 95: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

15

Page 96: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

15

Page 97: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

if / else!

15

Page 98: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

if / else!

if / else if / else

15

Page 99: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

if / else!

if / else if / else

switch

15

Page 100: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

if / else!

if / else if / else

switch

§ Can be combined with Boolean operations

15

Page 101: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Control Flow Within Your Programs

if!

if / else!

if / else if / else

switch

§ Can be combined with Boolean operations

§ Make use of braces { }

15

Page 102: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

16

Page 103: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if! Performs code within braces if the argument

within parentheses is true

16

Page 104: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if! Performs code within braces if the argument

within parentheses is true if (true) { print( "This line will print." );}

if (false) { print( "This line will NOT print." );}

// The output of this code will be:

16

Page 105: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if! Performs code within braces if the argument

within parentheses is true if (true) { print( "This line will print." );}

if (false) { print( "This line will NOT print." );}

// The output of this code will be:

// This line will print.

16

Page 106: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if! Performs code within braces if the argument

within parentheses is true if (true) { print( "This line will print." );}

if (false) { print( "This line will NOT print." );}

// The output of this code will be:

// This line will print.

§ All the code within the braces of the if statement executes

16

Page 107: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

17

Page 108: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with boolean operations

17

Page 109: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with boolean operations

bool night = true;bool fullMoon = false;

if (night) { print( "It's night." );}if (!fullMoon) { print( "The moon is not full." );}if (night && fullMoon) { print( "Beware werewolves!!!" );}if (night && !fullMoon) { print( "No werewolves tonight. (Whew!)" );}

// The output of this code will be:

17

Page 110: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with boolean operations

bool night = true;bool fullMoon = false;

if (night) { print( "It's night." );}if (!fullMoon) { print( "The moon is not full." );}if (night && fullMoon) { print( "Beware werewolves!!!" );}if (night && !fullMoon) { print( "No werewolves tonight. (Whew!)" );}

// The output of this code will be:// It's night.// The moon is not full.// No werewolves tonight. (Whew!)

17

Page 111: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

18

Page 112: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with comparison operators

18

Page 113: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with comparison operators

if (10 == 10 ) { print( "10 is equal to 10." );}if ( 10 > 20 ) { print( "10 is greater than 20." );}if ( 1.23f <= 3.14f ) { print( "1.23 is less than or equal to 3.14." );}if ( 1.23f >= 1.23f ) { print( "1.23 is greater than or equal to 1.23." );}if ( 3.14f != Mathf.PI ) { print( "3.14 is not equal to "+Mathf.PI+"." ); // + can be used to concatenate strings with other data types. // When this happens, the other data is converted to a string.}

18

Page 114: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Combining if statements with comparison operators

if (10 == 10 ) { print( "10 is equal to 10." );}if ( 10 > 20 ) { print( "10 is greater than 20." );}if ( 1.23f <= 3.14f ) { print( "1.23 is less than or equal to 3.14." );}if ( 1.23f >= 1.23f ) { print( "1.23 is greater than or equal to 1.23." );}if ( 3.14f != Mathf.PI ) { print( "3.14 is not equal to "+Mathf.PI+"." ); // + can be used to concatenate strings with other data types. // When this happens, the other data is converted to a string.}

§ Don't accidentally use = in an if statement!!!

18

Page 115: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

19

Page 116: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else

19

Page 117: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else

– Performs one action if true, and another if false

19

Page 118: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else

– Performs one action if true, and another if falsebool night = false;

if (night) { print( "It's night." );} else { print( "What are you worried about?" );}

// The output of this code will be:

19

Page 119: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else

– Performs one action if true, and another if falsebool night = false;

if (night) { print( "It's night." );} else { print( "What are you worried about?" );}

// The output of this code will be:// What are you worried about?

19

Page 120: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

20

Page 121: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else if / else

20

Page 122: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else if / else

– Possible to chain several else if clauses

20

Page 123: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else if / else

– Possible to chain several else if clausesbool night = true;bool fullMoon = true;

if (!night) { // Condition 1 (false) print( "It’s daytime. What are you worried about?" );} else if (fullMoon) { // Condition 2 (true) print( "Beware werewolves!!!" );} else { // Condition 3 (not checked) print( "It's night, but the moon is not full." );}

// The output of this code will be:

20

Page 124: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ if / else if / else

– Possible to chain several else if clausesbool night = true;bool fullMoon = true;

if (!night) { // Condition 1 (false) print( "It’s daytime. What are you worried about?" );} else if (fullMoon) { // Condition 2 (true) print( "Beware werewolves!!!" );} else { // Condition 3 (not checked) print( "It's night, but the moon is not full." );}

// The output of this code will be:// Beware werewolves!!!

20

Page 125: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

21

Page 126: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Nested if statements

21

Page 127: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Nested if statements

bool night = true;bool fullMoon = false;

if (!night) { print( "It’s daytime. Why are you worried about?" );} else { if (fullMoon) { print( "Beware werewolves!!!" ); } else { print( "It's night, but the moon isn't full." ); }}

// The output of this code will be:

21

Page 128: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Nested if statements

bool night = true;bool fullMoon = false;

if (!night) { print( "It’s daytime. Why are you worried about?" );} else { if (fullMoon) { print( "Beware werewolves!!!" ); } else { print( "It's night, but the moon isn't full." ); }}

// The output of this code will be:// It's night, but the moon isn't full.

21

Page 129: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

22

Page 130: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ switch! Alternative to several if statements

22

Page 131: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ switch! Alternative to several if statements

– Can only compare for equality

22

Page 132: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ switch! Alternative to several if statements

– Can only compare for equality– Can only compare against a single variable against literals

22

Page 133: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ switch! Alternative to several if statements

– Can only compare for equality– Can only compare against a single variable against literals int num = 3;switch (num) { // The variable in parentheses is being comparedcase (0): // Each case is a literal that is compared against num print( "The number is zero." ); break; // Each case must end with a break statement.case (1): print( "The number is one." ); break; case (2): print( "The number is two." ); break; default: // If none of the other cases are true, default will happen print( "The number is more than a couple." ); break;} // The switch statement ends with a closing brace.

22

Page 134: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ switch! Alternative to several if statements

– Can only compare for equality– Can only compare against a single variable against literals int num = 3;switch (num) { // The variable in parentheses is being comparedcase (0): // Each case is a literal that is compared against num print( "The number is zero." ); break; // Each case must end with a break statement.case (1): print( "The number is one." ); break; case (2): print( "The number is two." ); break; default: // If none of the other cases are true, default will happen print( "The number is more than a couple." ); break;} // The switch statement ends with a closing brace.

// The output of this code is: The number is more than a couple.

22

Page 135: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements

23

Page 136: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Switch can "fall through" to other cases

23

Page 137: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Switch can "fall through" to other cases

int num = 3;switch (num) {case (0): print( "The number is zero." ); break;case (1): print( "The number is one." ); break; case (2): print( "The number is a couple." ); break;case (3): // case (3) falls through to case (4)case (4): // case (4) falls through to case (5)case (5): print( "The number is a few." ); break;default: print( "The number is more than a few." ); break;}

23

Page 138: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Conditional Statements§ Switch can "fall through" to other cases

int num = 3;switch (num) {case (0): print( "The number is zero." ); break;case (1): print( "The number is one." ); break; case (2): print( "The number is a couple." ); break;case (3): // case (3) falls through to case (4)case (4): // case (4) falls through to case (5)case (5): print( "The number is a few." ); break;default: print( "The number is more than a few." ); break;}

// The output of this code is: The number is a few.

23

Page 139: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary

24

Page 140: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

24

Page 141: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

24

Page 142: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

§ Boolean operations can be combined

24

Page 143: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

§ Boolean operations can be combined

§ Comparison Operators: == != > < >= <=

24

Page 144: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

§ Boolean operations can be combined

§ Comparison Operators: == != > < >= <=

§ Conditional Statements: if if…else switch

24

Page 145: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

§ Boolean operations can be combined

§ Comparison Operators: == != > < >= <=

§ Conditional Statements: if if…else switch– if and switch statements can be combined in complex ways

24

Page 146: BOOLEAN OPERATIONS AND CONDITIONALS - Protobook.prototools.net/wp-content/uploads/2014/06/IGDPD C20-Boolea… · Boolean Operations! Shorting vs. Non-Shorting Boolean Operators –

Chapter 20 – Summary§ Boolean Operations: ! && || & |

§ Learned about "shorting operations"

§ Boolean operations can be combined

§ Comparison Operators: == != > < >= <=

§ Conditional Statements: if if…else switch– if and switch statements can be combined in complex ways

§ Next Chapter: Loops in C# code!

24