Chapter 3 - Control
Chapter 3 - Control
[email protected]
Mantra:
Coding is fun!
Selection Statements
– Using if and if...else
– Nested if Statements
– Using switch Statements
– Conditional Operator
Repetition Statements
– Looping: while, do-while, and for
– Nested loops
– Using break and continue
if Statements
switch Statements
Conditional Operators
if Statements
if (booleanExpression) {
statement(s);
}
Example:
if ((i > 0) && (i < 10)) {
System.out.println("i is an " +
"integer between 0 and 10");
}
Caution
if (booleanExpression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
if...else Example
if (radius >= 0) {
area = radius*radius*PI;
grade = ‘C’;
else
grade = ‘D’;
else
grade = ‘F’;
Note
The else clause matches the most recent if clause in the
same block. For example, the following statement
int i = 1; int j = 2; int k = 3;
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
is equivalent to
int i = 1; int j = 2; int k = 3;
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
Note, cont.
Nothing is printed from the preceding statement. To
force the else clause to match the first if clause,
you must add a pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
else
System.out.println("B");
7 default
numOfYears
15 30
Next
Statement
switch Statement Rules
The switch-expression must yield a value of char, byte, short, or
int type and must always be enclosed in parentheses.
The value1, ..., and valueN must have the same data type as the
value of the switch-expression. The resulting statements in the
case statement are executed when the value in the case statement
matches the value of the switch-expression. (The case statements
are executed in sequential order.)
if (x > 0) y = 1
else y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
Ternary operator
Binary operator
Unary operator
Conditional Operator
if (num % 2 == 0)
System.out.println(num + “is even”);
else
System.out.println(num + “is odd”);
System.out.println(
(num % 2 == 0)? num + “is even” :
num + “is odd”);
Conditional Operator, cont.
while Loops
do-while Loops
for Loops
while (continuation-condition) {
// loop-body;
}
false
Continuation
condition?
true
Statement(s)
Next
Statement
while Loop Flow Chart, cont.
i = 0;
int i = 0;
while (i < 100) { false
(i < 100)
System.out.println(
"Welcome to Java!");
i++;
true
}
System.out.println("Welcoem to Java!");
i++;
Next
Statement
Example 3.2: Using while Loops
if (data == 0)
System.out.println("data is zero");
else
System.out.println("data is not zero");
do-while Loop
do { Statement(s)
// Loop body;
} while (continue-condition);
true
Continue
condition?
false
Next
Statement
for Loops
for (initial-action; loop-continuation-condition;
action-after-each-iteration) {
//loop body;
}
int i = 0;
while (i < 100) {
System.out.println("Welcome to Java! ” + i);
i++;
}
Example:
int i;
for (i = 0; i < 100; i++) {
System.out.println("Welcome to Java! ” + i);
}
for Loop Flow Chart
for (initial-action;
loop-continuation-condition;
action-after-each-iteration) { Initial-Action
//loop body;
}
false
Action-After- Continuation
Each-Iteration condition?
true
Statement(s)
(loop-body)
Next
Statement
for Loop Example
int i;
for (i = 0; i<100; i++) { i=0
System.out.println(
"Welcome to Java");
} false
i++ i<100?
true
System.out.println(
“Welcom to Java!”);
Next
Statement
for Loop Examples
The three forms of loop statements, while, do, and for, are
expressively equivalent; that is, you can write a loop in any of
these three forms.
I recommend that you use the one that is most intuitive and
comfortable for you. In general, a for loop may be used if the
number of repetitions is known, as, for example, when you
need to print a message 100 times. A while loop may be
used if the number of repetitions is not known, as in the case
of reading the numbers until the input is 0. A do-while loop
can be used to replace a while loop if the loop body has to be
executed before testing the continuation condition.
Caution
false
Continuation
condition?
true
Statement(s)
break
Statement(s)
Next
Statement
The continue Keyword
false
Continue
condition?
true
Statement(s)
continue
Statement(s)
Next
Statement
Using break and continue