X - AS Nida
X - AS Nida X - AS Nida
Outline Conditional Statements • if • if-else • switch Logical Operators More Operators Repetition Statements • while • do • for Flow of Control Flow of Control Original Authors: John Lewis and William Loftus Modified by Patrawadee Tanawongsuwan 2 Conditional Statements Conditional Statements Flow of Control Flow of Control A conditional statement lets us choose which statement will be executed next Unless specified otherwise, the order of statement execution through a method is linear: one statement after the other in sequence Java's conditional statements are • the if statement • the if-else statement • the switch statement Some programming statements modify that order, allowing us to: • decide whether or not to execute a particular statement, or • perform a statement over and over, repetitively The order of statement execution is called the flow of control 4 3
- Page 2 and 3: The if Statement The if Statement T
- Page 4 and 5: The switch Statement The switch Sta
- Page 6 and 7: Logical OR Logical OR Logical AND L
- Page 8 and 9: Increment and Decrement Increment a
- Page 10 and 11: The for Statement The for Statement
- Page 12 and 13: The do Statement The do Statement I
Outline<br />
Conditional Statements<br />
• if<br />
• if-else<br />
• switch<br />
Logical Operators<br />
More Operators<br />
Repetition Statements<br />
• while<br />
• do<br />
• for<br />
Flow of Control<br />
Flow of Control<br />
Original Authors: John Lewis and William Loftus<br />
Modified by Patrawadee Tanawongsuwan<br />
2<br />
Conditional Statements<br />
Conditional Statements<br />
Flow of Control<br />
Flow of Control<br />
A conditional statement lets us choose which<br />
statement will be executed next<br />
Unless specified otherwise, the order of statement<br />
execution through a method is linear: one statement<br />
after the other in sequence<br />
Java's conditional statements are<br />
• the if statement<br />
• the if-else statement<br />
• the switch statement<br />
Some programming statements modify that order,<br />
allowing us to:<br />
• decide whether or not to execute a particular statement, or<br />
• perform a statement over and over, repetitively<br />
The order of statement execution is called the flow of<br />
control<br />
4<br />
3
The if Statement<br />
The if Statement<br />
The if Statement<br />
The if Statement<br />
An example of an if statement:<br />
The if statement has the following syntax:<br />
if (sum < 0)<br />
System.out.print (“less”);<br />
System.out.println (“ OK”);<br />
The condition<br />
must be a boolean<br />
expression. It<br />
must evaluate to<br />
either true or false.<br />
if is a Java<br />
reserved word<br />
if ( condition )<br />
statement;<br />
The condition sum < 0 is evaluated.<br />
If the condition is true, less OK is printed.<br />
If it is not, OK is printed.<br />
If the condition is true, the statement is executed.<br />
If it is false, the statement is skipped.<br />
6<br />
5<br />
The if-else if else Statement<br />
Boolean Expressions<br />
Boolean Expressions<br />
An else clause can be added to an if statement to<br />
make an if-else statement<br />
A condition often uses one of Java's equality<br />
operators or relational operators, which all return<br />
boolean results:<br />
if ( condition )<br />
statement1;<br />
else<br />
statement2;<br />
== equal to<br />
!= not equal to<br />
< less than<br />
> greater than<br />
= greater than or equal to<br />
If the condition is true, statement1 is executed; if<br />
the condition is false, statement2 is executed<br />
One or the other will be executed, but not both<br />
Note the difference between the equality operator<br />
(==) and the assignment operator (=)<br />
8<br />
7
Block Statements<br />
Block Statements<br />
The if-else if else Statement<br />
Several statements can be grouped together into a<br />
block statement<br />
A block is delimited by braces : { … }<br />
if (sum < 0)<br />
System.out.print (“less”);<br />
else<br />
System.out.print (“more”);<br />
System.out.println (“ OK”);<br />
When the value of sum is -99 , it prints less OK<br />
When the value of sum is 23 , it prints more OK<br />
10<br />
9<br />
Nested if Statements<br />
Nested if Statements<br />
Block Statements<br />
Block Statements<br />
There could be an if statement within another if or<br />
an else clause<br />
Example<br />
These are called nested if statements<br />
if (sum < 0)<br />
System.out.print (“less”);<br />
System.out.println (“ OK”);<br />
An else clause is matched to the last unmatched if<br />
(no matter what the indentation implies)<br />
Braces can be used to specify the if statement to<br />
which an else clause belongs<br />
if (sum < 0) {<br />
System.out.print (“less”);<br />
System.out.println (“ OK”);<br />
}<br />
When the value of sum is 5,<br />
The 1st prints OK.<br />
The 2nd prints nothing at all.<br />
12<br />
11
The switch Statement<br />
The switch Statement<br />
Nested if Statements<br />
Nested if Statements<br />
The switch statement evaluates an expression, then<br />
attempts to match the result to one of several<br />
possible cases<br />
Examples<br />
Each case contains a value and a list of statements<br />
The flow of control transfers to statement associated<br />
with the first value that matches<br />
if (sum < 10)<br />
myType = 1;<br />
else if (sum < 20)<br />
myType = 2;<br />
else if (sum < 30)<br />
myType = 3;<br />
else<br />
myType = 4;<br />
if (sum < 20)<br />
if (sum < 10)<br />
myType = 1;<br />
else<br />
myType = 2;<br />
else<br />
if (sum < 30)<br />
myType = 3;<br />
else<br />
myType = 4;<br />
14<br />
13<br />
The switch Statement<br />
The switch Statement<br />
The switch Statement<br />
The switch Statement<br />
switch (max) {<br />
case 10:<br />
System.out.println (“Ten");<br />
break;<br />
case 9:<br />
System.out.println (“Nine");<br />
break;<br />
case 8:<br />
System.out.println (“Eight");<br />
break;<br />
default:<br />
System.out.println (“Don’t know.");<br />
}<br />
The general syntax of a switch statement is:<br />
switch ( expression ) {<br />
case value1:<br />
statement-list1<br />
case value2:<br />
statement-list2<br />
case ...<br />
}<br />
If expression matches value2,<br />
control jumps to here<br />
16<br />
15
The switch Statement<br />
The switch Statement<br />
The switch Statement<br />
The switch Statement<br />
A switch statement can have an optional default<br />
case<br />
The expression of a switch statement must result in<br />
either int or char type<br />
The default case has no associated value and simply<br />
uses the reserved word default<br />
Often a break statement is used as the last statement<br />
in each case's statement list<br />
If the default case is present, control will transfer to it<br />
if no other case value matches<br />
A break statement causes control to transfer to the<br />
end of the switch statement<br />
Though the default case can be positioned anywhere<br />
in the switch, usually it is placed at the end<br />
If a break statement is not used, the flow of control<br />
will continue into the next case<br />
If there is no default case, and no other value<br />
matches, control falls through to the statement after<br />
the switch<br />
18<br />
17<br />
Logical NOT<br />
Logical NOT<br />
Logical Operators<br />
Logical Operators<br />
The logical NOT !<br />
Boolean expressions can use the following logical<br />
operators:<br />
if (!(x < 0))<br />
System.out.println (x);<br />
! Logical NOT<br />
&& Logical AND<br />
|| Logical OR<br />
They all take boolean operands and produce boolean<br />
results<br />
If x is not less than 0, x is printed.<br />
20<br />
19
Logical OR<br />
Logical OR<br />
Logical AND<br />
Logical AND<br />
The logical OR expression<br />
The logical AND expression<br />
a || b<br />
a && b<br />
is true if a or b or both are true, and false otherwise<br />
is true if both a and b are true, and false otherwise<br />
if ((x < 5) || (x > 100))<br />
System.out.println (x);<br />
if ((x >= 1) && (x MAX && count != 0)<br />
System.out.println ("Testing…");<br />
If condition1 is false, it ignores condition2.<br />
24<br />
23
Comparing Strings<br />
Comparing Strings<br />
Comparing Characters<br />
Comparing Characters<br />
Remember that a character string in Java is an object<br />
We can use the relational operators on character data<br />
We cannot use the relational operators to compare<br />
strings<br />
The results are based on the Unicode character set<br />
The equals method can be called with strings to<br />
determine if two strings contain exactly the same<br />
characters in the same order<br />
The following condition is true because the character<br />
+ (Unicode 43) comes before the character J<br />
(Unicode 74) in the Unicode character set:<br />
if ('+' < 'J')<br />
System.out.println ("+ is less than J");<br />
The String class also contains a method called<br />
compareTo to determine if one string comes before<br />
another (based on the Unicode character set)<br />
The uppercase alphabet (A-Z) followed by the<br />
lowercase alphabet (a-z) appear in alphabetical order<br />
in the Unicode character set<br />
26<br />
25<br />
More Operators<br />
More Operators<br />
Lexicographic Ordering<br />
Lexicographic Ordering<br />
In particular, we will examine<br />
• the increment and decrement operators<br />
Because comparing characters and strings is based<br />
on a character set, it is called a lexicographic<br />
ordering<br />
• the assignment operators<br />
This is not strictly alphabetical when uppercase and<br />
lowercase characters are mixed<br />
For example, the string “Box" comes before the<br />
string “art" because all of the uppercase letters<br />
come before all of the lowercase letters in Unicode<br />
Also, short strings come before longer strings with<br />
the same prefix (lexicographically)<br />
Therefore "book" comes before "bookcase"<br />
28<br />
27
Increment and Decrement<br />
Increment and Decrement<br />
Increment and Decrement<br />
Increment and Decrement<br />
The increment and decrement operators can be<br />
applied in prefix form (before the operand) or postfix<br />
form (after the operand)<br />
The increment operator (++) adds one to its operand<br />
The decrement operator (--) subtracts one from its<br />
operand<br />
When used alone in a statement, the prefix and<br />
postfix forms are functionally equivalent. That is,<br />
The statement<br />
count++;<br />
count++;<br />
is equivalent to<br />
is functionally equivalent to<br />
++count;<br />
count = count + 1;<br />
When used in a larger expression, the prefix and<br />
postfix forms have different effects<br />
30<br />
29<br />
Assignment Operators<br />
Assignment Operators<br />
Increment and Decrement<br />
Increment and Decrement<br />
Often we perform an operation on a variable, and<br />
then store the result back into that variable<br />
If count currently contains 45, then the statement<br />
total = count++;<br />
Java provides assignment operators to simplify that<br />
process<br />
assigns 45 to total and 46 to count<br />
For example, the statement<br />
If count currently contains 45, then the statement<br />
num += count;<br />
total = ++count;<br />
is equivalent to<br />
assigns the value 46 to both total and count<br />
num = num + count;<br />
32<br />
31
Assignment Operators<br />
Assignment Operators<br />
Assignment Operators<br />
Assignment Operators<br />
The right hand side of an assignment operator can be<br />
a complex expression<br />
There are many assignment operators, including the<br />
following:<br />
The entire right-hand expression is evaluated first,<br />
then the result is combined with the original variable<br />
Equivalent To<br />
Example<br />
Operator<br />
Therefore<br />
result /= (total-MIN) % num;<br />
is equivalent to<br />
x = x + y<br />
x = x - y<br />
x = x * y<br />
x = x / y<br />
x = x % y<br />
x += y<br />
x -= y<br />
x *= y<br />
x /= y<br />
x %= y<br />
+=<br />
-=<br />
*=<br />
/=<br />
%=<br />
result = result / ((total-MIN) % num);<br />
34<br />
33<br />
The for Statement<br />
The for Statement<br />
Repetition Statements<br />
Repetition Statements<br />
The for statement has the following syntax:<br />
Repetition statements allow us to execute a<br />
statement multiple times<br />
executed until the<br />
condition<br />
becomes false<br />
executed once<br />
before the<br />
loop begins<br />
Often they are referred to as loops<br />
Java has three kinds of repetition statements:<br />
for ( initialization ; condition ; update )<br />
statement;<br />
• the for loop<br />
• the while loop<br />
• the do loop<br />
Each kind of loop works slightly differently<br />
executed at the end<br />
of each iteration<br />
The condition-statement-update cycle is executed repeatedly<br />
36<br />
35
The for Statement<br />
The for Statement<br />
The for Statement<br />
The for Statement<br />
The condition of a for statement is tested prior to<br />
executing the loop body<br />
public class MyCounterFor {<br />
public static void main (String[] args) {<br />
If the condition is false initially, the statement is<br />
never executed<br />
final int LIMIT = 2;<br />
int count;<br />
Therefore, the body of a for loop will execute zero or<br />
more times<br />
for (count = 0; count
The while Statement<br />
The while Statement<br />
The while Statement<br />
The while Statement<br />
Note that if the condition of a while statement is<br />
false initially, the statement is never executed<br />
public class MyCounterWhile {<br />
public static void main (String[] args) {<br />
Therefore, the body of a while loop will execute zero<br />
or more times<br />
final int LIMIT = 2;<br />
int count = 0;<br />
while (count
The do Statement<br />
The do Statement<br />
Infinite Loops<br />
Infinite Loops<br />
The do statement has the following syntax:<br />
public class Forever {<br />
public static void main (String[] args) {<br />
int count = 1;<br />
do {<br />
statement;<br />
} while ( condition )<br />
while (count
Choosing a Loop Structure<br />
Choosing a Loop Structure<br />
Comparing while and do<br />
Comparing while and do<br />
When you can’t determine how many times you want<br />
to execute the loop body, use a while statement or a<br />
do statement<br />
• If it might be zero or more times, use a while statement<br />
while ( condition ) {<br />
statement;<br />
}<br />
• If it will be at least once, use a do statement<br />
If you can determine how many times you want to<br />
execute the loop body, use a for statement<br />
do {<br />
statement;<br />
} while ( condition );<br />
50<br />
49