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

An overview of selection statements, equality operators, relational operators, and logical operators in java programming. It covers topics such as forming conditions, if/else statements, comparing floating-point numbers, comparing objects, and the switch statement. It also explains the use of logical operators and short-circuit evaluation.

Typology: Assignments

Pre 2010

Uploaded on 09/17/2009

koofers-user-s6c
koofers-user-s6c 🇺🇸

10 documents

1 / 54

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
Flow of Control Part 1:
Selection
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

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