(Week 03) Flow of Control (Selection)
(Week 03) Flow of Control (Selection)
Fundamentals of Programming
Semester 1 2015/2016
Chapter 3
Flow of Control (Selection)
Contents
Introduction
Relational Operator
Logical Operator
if
if-else
ultiway if-else
switch
Ternary Operator
Common Error
Introduction
Flow control is to used to specify the order of the
statements to be executed.
Program can be written in three types of flow control
namely the sequence, selection and repetition.
When the statements are executed one after the other
in order, it is called the sequence flow.
A selection flow chooses among alternative courses
of action.
A repetition flow specifies that an action is to be
repeated while some condition remains true.
Introduction
Computer programs often need to make decisions,
taking different actions depending on the condition.
In Java, if and switch statement are used to carry out
the decision.
The statement is controlled by the boolean expression.
Relational/Conditional operator can be used in the
boolean expression.
If there is more than one constraint/condition in the
decision making, the logical operators are used to
merge multiple constraint/conditions.
Relational Operator
The relational operator tests the relationship between
two values.
== Equal a==b
&& AND (true && true is true, others false) a==b && c==d
String.equalsIgnoreCase(other_string)
String.compareToIgnoreCase(other_string)
s1.compareTo(s2)
Return negative value if s1 comes before s2.