You are on page 1of 18

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
 Ordering strings that is based only on alphabetical letters
‘a’—‘z’ and ‘A’—‘Z’ is called alphabetical ordering and Never use the logic
al
,
operators ==, !=, <
ordering that is based on ASCII code (or Unicode) is <=, >, >= to comp
are
called lexicographic ordering. strings

 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 9


Comparing Strings (Cont.)

ITCS 113 ─ Dr. Ali Alsaffar 10


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 11


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 12


Examples

ITCS 113 ─ Dr. Ali Alsaffar 13


Examples (Cont.)

ITCS 113 ─ Dr. Ali Alsaffar 14


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 15


The switch Statement
 It is used to test the equality of an expression against multiple constants.

ITCS 113 ─ Dr. Ali Alsaffar 16


The switch Statement (Cont.)
 Example

ITCS 113 ─ Dr. Ali Alsaffar 17


Examples

Click for More


Examples

ITCS 113 ─ Dr. Ali Alsaffar 18

You might also like