












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 20
This page cannot be seen from the preview
Don't miss anything!
New Mexico Tech: CSE/IT 111
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
New Mexico Tech: CSE/IT 111
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
Ù
th
Ù
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^ Ù
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_
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^ Ù
Ù
Ù
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
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; }
Each
else
is paired with the nearest preceding unmatched
if
REMEMBER: compiler ignores indentation
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”);
} }
Multibranch
if-else
statement
System.out.println(“Positive”);
} else if (balance < 0) {
System.out.println(“Negative”);
} else
// Default action
System.out.println(“Zero”);
First true condition is executed; subsequentconditions ignored Ù
Important if conditions are not
mutually
exclusive
New Mexico Tech: CSE/IT 111
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
When execution reaches a
break
statement, the
switch
statement’s
execution ends
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
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