Docsity

Flow of Control: Selection and Equality Operators - Prof. Ramakrishna Varadarajan, Assignments of Javascript programming

Partial preview of the text

Download Flow of Control: Selection and Equality Operators - Prof. Ramakrishna Varadarajan and more Assignments Javascript programming in PDF only on Docsity!

Chapter 5

Flow of Control Part 1:

Selection

Topics

•^

Forming Conditions

-^

if/else

Statements

•^

Comparing Floating-Point Numbers

-^

Comparing Objects– The

equals

Method

String

Comparison Methods

•^

The Conditional Operator ( ?: )

-^

The

switch

Statement

Equality Operators

•^

Used to determine if values of two expressions areequal or not equal

-^

Result is

true

or

false

is not equal to

binary

!=

is equal to

binary

= =

Meaning

Type (numberof operands)

Equalityoperators

Examples

•^

If

int

variable

age

holds the value 32:

( age == 32 )

evaluates to

true

( age != 32 )

evaluates to

false

Use the equality operators only with primitive

types and object references, not to compareobject data!

Relational Operators

-^

Used to compare the values of two expressions

-^

Result is

true

or

false

is greater than orequal to

binary

=

is greater than

binary

is less than orequal to

binary

<=

is less than

binary

<

Meaning

Type (number ofoperands)

RelationalOperators

Example

•^

If

int

variable

age

holds value 32:

( age < 32 )

evaluates to

false

( age <=

32 )

evaluates to

true

( age > 32 )

evaluates to

false

( age >=

32 )

evaluates to

true

Logical Operators

-^

The NOT operator (! ) inverts the value of its operand. Ifthe operand is

true

, the result will be

false

; and if the

operand is

false

, the result will be

true

.

-^

The AND operator ( && ) takes two

boolean

expressions

as operands; if both operands are

true,

the result will be

true

, otherwise it will be

false

.

-^

The OR operator ( || ) takes two

boolean

expressions as

operands. If both operands are

false

, the result will be

false

; otherwise it will be

true

.

Truth Table

false

false

true

false

false

true

false

true

true

false

true

false

false

false

true

true

true

false

true

true

a || b

a && b

!a

b

a For operator precedence, see Appendix B

Suppose we have three

ints x, y,

and

z,

and we want

to test if

x

is less than both

y

and

z

. A common

error is to express the condition this incorrect way:

x < y && z

// compiler error

Each operand of a logical operator must be a boolean

expression. This is correct:

x < y

&&

x < z

Equivalence of Expressions

DeMorgan's Laws: 1.

NOT( A AND B ) = ( NOT A ) OR ( NOT B )

NOT( A OR B ) = ( NOT A ) AND ( NOT B )

-^

Thus to find an equivalent expression:–^

change

&&

to

||

-^

change

||

to

&&

-^

negate each operand expression

Examples

These expressions are equivalent:

( age <= 18 || age >= 65 )!( age > 18 && age < 65 )!( age > 18 ) || !( age < 65 )

Simple

if

Statement

•^

Used when program should perform an operationfor one set of data, but do nothing for all otherdata

-^

Syntax: if ( condition ){

// true block// executed if condition is true }

-^

Curly braces are optional if true block contains only onestatement

•^

Indent the true block of the

if

statement for clarity

•^

Line up the open and closing curly braces underthe "i" in

if

Simple

if

Example

•^

See Example 5.2 PassingGrade.java