Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Flow of Control: Branching - Computer Science and Programming | IT 111, Lab Reports of Information Technology

Material Type: Lab; Class: Intro: Computer Sci & Program; Subject: Information Technology; University: New Mexico Institute of Mining and Technology; Term: Spring 2009;

Typology: Lab Reports

Pre 2010

Uploaded on 08/08/2009

koofers-user-8mx-2
koofers-user-8mx-2 🇺🇸

10 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LECTURE 10
02/09/2009
New Mexico Tech: CSE/IT 111
Flow of Control:
Branching
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Flow of Control: Branching - Computer Science and Programming | IT 111 and more Lab Reports Information Technology in PDF only on Docsity!

L E C T U R E 1 0

New Mexico Tech: CSE/IT 111

Flow of Control:

Branching

Class Maintenance

y New Mexico Tech: CSE/IT 111

Lab 2 will be posted by tomorrow morning

No prelab

y

HW 2 will be posted by Wednesday

Due: 2/18 (Wed.)

Flow of Control

y New Mexico Tech: CSE/IT 111

Flow of control

The order in which a program performs actions

y

Control structures

are used to regulate flow of control

Branching statement (Chapter 3)^ Ù

A statement that chooses one action from a list of two or more possibleactions Ù

Also called

decision structures

Loop statement (Chapter 4)^ Ù

A statement that repeats an action again and again until some stoppingcondition is met

T H E B A S I C

i f - e l s e

S T A T E M E N T

B O O L E A N E X P R E S S I O N S

C O M P A R I N G S T R I N G S

N E S T E D

i f - e l s e

S T A T E M E N T S

M U L T I B R A N C H

i f - e l s e

S T A T E M E N T S

T H E C O N D I T I O N A L O P E R A T O R

T H E

e x i t

M E T H O D

New Mexico Tech: CSE/IT 111

The

if-else

Statement

Putting together the

if-else

Statement

y New Mexico Tech: CSE/IT 111

Boolean expression

An expression composed of variables, constants, boolean operators, and/orcomparison operators whose value is either

true

or

false

Ù

Based on Boolean algebra developed by George Boole (

th

century)

Ù

Example:

balance

y

Compound statement

One or more programming-language statements enclosed in a pair of braces andoften used as a part of a branching statement or a loop statement^ Ù

Allows us to include more than one statement (enclosed in

) in each branch

if (balance >= 0) {

System.out.println("Good for you. You earned interest."); balance = balance + (INTEREST_RATE * balance) / 12; } else {

System.out.println("You will be charged a

penalty.");

balance = balance - OVERDRAWN_PENALTY; }

Semantics of the

if-else

Statement

y New Mexico Tech: CSE/IT 111

The basic

if-else

statement

Syntax^ Ù

if

(

Boolean_Expression

)

Statement_

else

Statement_

If the expression Boolean_Expression istrue, Statement_1 is executed; otherwise, Statement_2 is executed

y

An

if

statement without an

else

Program

skips

Statement_

when the

if

statement’s expression is

false In other words:

the statement either performs an action or

does not

Evaluate

Boolean_Expression

Execute

Statement_

Execute

Statement_

Start

False

True

Boolean Expressions: Logical Operators

y New Mexico Tech: CSE/IT 111

Logical operator

One of the operators

&&

,^

||

, and

!

used to

combine boolean expressions

to

form a larger boolean expression^ Ù

Sub_Expression_

Sub_Expression_

True

if and only if both

Sub_Expression_

and

Sub_Expression_

are true

Ù

Sub_Expression_

Sub_Expression_

True

if either

Sub_Expression_

or

Sub_Expression_

is true

or both

are true

Ù

Boolean_Expression

True if

Boolean_Expression

is

false

Name

Java Notation

Java Examples

Logical

and

(sum > min) && (sum < max)

Logical

or

(answer == ‘y’) || (answer == ‘Y’)

Logical

not

!(number < 0)

Comparing Strings: Equality

New Mexico Tech: CSE/IT 111 y

Equality operator

The operator

==

is used to

compare two primitive values(such as two numbers) Has a different meaning whenapplied to objects (such asstrings)^ Ù

Tests whether the objects arestored in the

same memory

location

y

Instead, use the method equals

to compare strings

Returns true if strings have equal values^ String

.equals(

Other_String

)

if

(

s1.equals(s

))

System.out.println(“Equal"); else

System.out.println(“Not equal.");

y

Can also use the method equalsIgnoreCase

String

.equalsIgnoreCase(

Other_String

)

if

(

“Hello”.equalsIgnoreCase(“hello”

))

System.out.println(“Equal");

Nested

if-else

Statements

y New Mexico Tech: CSE/IT 111

Nest

: to place a statement within a statement of the same kind if (balance >= 0) {

if (INTEREST_RATE >= 0) {

balance = balance + (INTEREST_RATE * balance) / 12;

} else {

System.out.println(“Cannot have a negative interest.");

else {

balance = balance - OVERDRAWN_PENALTY; }

y

Each

else

is paired with the nearest preceding unmatched

if

To clarify your intent, use indentation that is consistent with the meaning of the statement^ Ù

REMEMBER: compiler ignores indentation

Use braces to make the statement’s meaning EXPLICIT

Multibranch

if-else

Statements

New Mexico Tech: CSE/IT 111

if (balance >= 0) {

System.out.println(“Positive”);

} else {

if (balance < 0) {

System.out.println(“Negative”);

} else {

if (balance == 0) {

System.out.println(“Zero”);

} }

y

Multibranch

if-else

statement

A style of nested

if-else

statements that

provide a choice among several actions^ if (balance >= 0)^ {

System.out.println(“Positive”);

} else if (balance < 0) {

System.out.println(“Negative”);

} else

// Default action

System.out.println(“Zero”);

} At execution, tested in order from top Ù

First true condition is executed; subsequentconditions ignored Ù

Important if conditions are not

mutually

exclusive

New Mexico Tech: CSE/IT 111

The

switch

Statement

The

switch

Statement: Syntax

New Mexico Tech: CSE/IT 111 y

Switch statement

A Java statement that provides a choice of severaloutcomes according to the value of a

controlling

expression

y

Controlling expression

An integer or character expression whose value isused to determine the outcome of a

switch

statement

y

Case label

A value associated with the keyword case within a switch

statement

that is a possible value of the

controlling expression

y

Break statement

A statement used either to indicate the end of acase within a

switch

statement

or to end the

iteration within a loop

y

Default case

An optional case within a

switch

statement

that

indicates the action taken when the value of the controlling expression

does not match any of

the

case labels

switch

Controlling_Expression

case

Case_Label

Statement

... Statement

break

case

Case_Label

Statement

... Statement

break

default

Statement

... Statement

break

The

break

Statement

New Mexico Tech: CSE/IT 111

y

When execution reaches a

break

statement, the

switch

statement’s

execution ends

y

If a case has no

break

statement,

execution continues on with the nextcase until a

break

statement is

encountered or the

switch

statement

ends

Will execute statements in followingcases, even if the value of thecontrolling expression does notmatch the case

y

Omitting a

break

statement can be

useful when you have multiple casesthat produce the same case action

switch

(numberOfBabies)

{

case

1:

System.out.println("Congrats."); break

;

case

2:

System.out.println("Wow.

Twins.");

break

;

/*

If

we

omit

the

previous

break,


any

time

the

statements

in

case

2


are

executed,

the

statements

in


case

3

will

also

be

executed.

*/

case

3:

System.out.println("Wow.

Triplets.");

break

;

case

4:

//

case

with

no

break

statement

case

5:

//

cases

4

&

5

produce

same

action

System.out.print("Unbelievable;

");

System.out.println(numberOfBabies

babies.");

break

;

default

:

//

executes

when

no

case

matches

System.out.println("I

don't

believe

you.");

break

;

}

Next Time

y New Mexico Tech: CSE/IT 111

Java

: Ch. 3.

y

CSI

: Continue Ch. 3