Uploaded by fatimaaloun5

Chapter 3 Selection

advertisement
Flow of Control
Branching
ITCS 113 Computer Programming I
Dr. Ali Alsaffar
Department of Computer Science
ITCS 113 ─ Dr. Ali Alsaffar
1
Logical Expressions
 In Java, a condition is represented by a logical (Boolean)
expression. An expression that has a value of either true or false
is called a logical (Boolean) expression.
 A relational operator allows you to make comparisons in your
program.
ITCS 113 ─ Dr. Ali Alsaffar
2
Logical (Boolean) Operators
 Logical (Boolean) operators enable you to combine logical
expressions.
 Truth Tables of && and ||
ITCS 113 ─ Dr. Ali Alsaffar
3
The Single if Statement
 The if structure is called a single-selection structure because it
selects or ignores a single action.
 Example
ITCS 113 ─ Dr. Ali Alsaffar
4
The if-else Statement
 The if-else structure is called a double-selection structure because
it selects between two different actions.
 Example
ITCS 113 ─ Dr. Ali Alsaffar
5
Compound Statement
 If according to a Boolean expression we would like to execute
multiple statements instead of a single statement, we use the
compound statement rule:
 Example: if x > y, then swap x and y
ITCS 113 ─ Dr. Ali Alsaffar
6
Examples of if/else statements.
ITCS 113 ─ Dr. Ali Alsaffar
7
Examples
ITCS 113 ─ Dr. Ali Alsaffar
8
Comparing Strings
Reference
 How to compare two strings s1 and s2?
 Strings are compared according to their
places when they are ordered.
 Either
a) s1 is equal to s2
b) s1 comes before s2 or
c) s1 comes after s2
Address
ASCII code
of ‘H’
 Example
String s1 = “Hi”;
String s2 = “Hello”;
int a = 34;
ITCS 113 ─ Dr. Ali Alsaffar
Memory
s1
6001
s2
9000
a
34
:
6001
72
‘H’
6002
105
‘i’
:
9000
72
‘H’
9001
101
‘e’
9002
108
‘l’
9003
108
‘l’
9004
111
‘o’
:
9
Comparing Strings
 Ordering strings that is based only on
alphabetical letters ‘a’—‘z’ and ‘A’—‘Z’ is called
alphabetical ordering and ordering that is based
on ASCII code (or Unicode) is called lexicographic
ordering.
 The words “Hello” and “hello” are equal in
alphabetical order. However, “Hello” is smaller
than “hello” in lexicographic order because ‘H’
comes before ‘h’ in the ASCII table !!!
 To compare strings in Java use the methods
equals(), equalsIgnoreCase() and compareTo().
These methods compare strings based on
lexicographic order.
ITCS 113 ─ Dr. Ali Alsaffar
10
Comparing Strings (Cont.)
ITCS 113 ─ Dr. Ali Alsaffar
11
Nested if statements
 A nested if statement occurs when the true or false statement of
an if statement is itself an if statement. It can be used to
implement decisions with several alternatives. For example,
ITCS 113 ─ Dr. Ali Alsaffar
12
Multibranch if-else Statements
 If each else in a nested if-else statement is itself an if-else
statement, then this if-else is a Multibranch if-else. For example,
ITCS 113 ─ Dr. Ali Alsaffar
13
Examples
ITCS 113 ─ Dr. Ali Alsaffar
14
Examples (Cont.)
ITCS 113 ─ Dr. Ali Alsaffar
15
The Type boolean
 The type boolean is a primitive type, just like the types int,
double, and char. It specifies only two values: true and false.
 Input and Output of Boolean Values. The values true and false of
the type boolean can be read as input or written as output in the
same way as values of the other primitive types, such as int and
double.
ITCS 113 ─ Dr. Ali Alsaffar
16
The switch Statement
 It is used to test the equality of an expression against multiple
constants.
ITCS 113 ─ Dr. Ali Alsaffar
17
The switch Statement (Cont.)
 Example
ITCS 113 ─ Dr. Ali Alsaffar
18
Examples
Click for More
Examples
ITCS 113 ─ Dr. Ali Alsaffar
19
Download