Object - Oriented System Design
Object - Oriented System Design
Lab #2
Contents
• Flow Control
– Branching Statements
– Looping Statements
2
Flow Control
• An application may use various flow control statements to direct
the logic of its execution
– Looping Statements
• while
• do-while
• for
3
Branching Statements
• Branch statements are used to divert the execution based on the
Boolean results of a condition
Program flow
Program flow
Switch
If condition condition
Case 1 Default
Case 2 Case 4
True False
Case 3
4
if Statements
• If statements allow one of two branch direction
– if (boolean condition)
statement;
• Multiple statements can be executed by including them within { }
– if (boolean condition)
{
statement1;
statement2;
statement3;
}
• The statements within the if statement are executed if the
condition is true
• If the condition is false, the statements are ignored and the flow
of the program continues after the statements.
5
if else-if Statements
• It is sometimes necessary to combine multiple if statements.
• Use if else-if statements to combine if conditions
– if (boolean condition)
statement1;
else if (boolean condition2)
statement2;
else
statement3;
6
if else Statements
7
switch Statements
• Switch statements allow for multiway branching
• Switch statements comprise of specific parts
– The switch: switch and control statements
– The cases: each case that must be considered by the switch
– The default case: if none of the case fit then the default case will match
8
switch Statements (Contd)
Controlling Expression
Case Labels
9
switch Statements (Contd)
• There are special considerations when using switch statements.
– The controlling expression must evaluate to one of the following types
• Char
• Int
• Short
• Byte
• Enum
• String (As of Java 7. Not available if you use an older version of java)
– The case labels must all be of the same type as the controlling
expression
– The case labels are followed by colons :
10
switch Statements (Contd)
• Omitting the breaks;
– Execution “falls through” to the next case statement after the end of the
statement is reached
Case 2
Case 3
Case 4
11
Self-Test (1)
• Template 폴더 업데이트
– 이전에 Clone 한 Template 폴더에 변경사항을 업데이트 하기 위해서는 다
시 Clone 하지 않고 다른 명령어를 사용한다.
– 설치한 Git 프로그램을 통해 Clone 한 Template 폴더에 접근
– git pull 명령어 입력
– 이후 Template 폴더를 확인하면 웹사이트와 동일하게 파일이 업데이트 된
것을 확인할 수 있다.
12
Self-Test (1)
❖ 프로젝트 명: Project02_1
❖ git commit -m “Project02_1”
❖ 당일 밤 12시까지 제출
13
Loop Statements
• Loops are control structures that allow groups of code to be
repeated a number of times
• The statement or group of statements to be repeated is called
body of the loop
• Each time a loop repeats is called an iteration of the loop
14
Loop Statements (Contd)
• Control of loop: ICU
1. Initialization
2. Condition for termination (continuing)
3. Updating the condition
• Body of loop
15
Loop Statements (Contd)
• the while loop
• the do-while loop
• the for loop
16
while Statement
• Also called a while loop
17
while Statement (Contd)
• Syntax
while (boolean_expression)
body_statement
/*-------------------------------------------------------------------------*/
while (boolean_expression)
{
first_statement
second_statement
...
}
18
while Statement (Contd)
19
while Statement (Contd)
• class WhileDemo
20
do-while Statement
• Also called do-while loop (repeat-until loop)
• similar to a while statement
– except that the loop body is executed at least once
• syntax
do
body_statement
while (boolean_expression);
21
do-while Statement (Contd)
• First, the loop body is executed
• Then the boolean expression is checked
– As long as it is true, the loop is executed again
– If it is false, the loop exits
• Equivalent while statement
body_statement(s)
while (boolean_condition)
body_statement(s)
22
do-while Statement (Contd)
23
do-while Statement (Contd)
• class DoWhileDemo
24
Infinite Loops
• A loop which repeats without ever ending
25
for Statement
• A for statement executes the body of a loop a fixed number of
times
• example
for (count = 1; count < 3; count++)
System.out.println(count);
System.out.println(“Done”);
26
for Statement (Contd)
• Syntax
for (Initialization; Condition; Update)
body_statement
body_statement
• a simple statement or
• a compound statement in {}
• Corresponding while statement
Initialization
while (Condition)
body_statement_including_update
27
for Statement (Contd)
28
for Statement (Contd)
• class ForDemo
--
29
Choosing a Loop Statement
• If you know how many times the loop will be iterated, use a for
loop
• If you don’t know how many times the loop will be iterated, but
– It could be zero, use a while loop
– It will be at least once, use a do-while loop
30
Programming with Loops: Outline
• The Loop Body
• Initializing Statements
• Ending a Loop
31
Loop Body
• To design the loop body, write out the actions the code must
accomplish
32
Initializing Statements
• Some variables need to have a value before the loop begins
– Sometimes this is determined by what is supposed to happen after one
loop iteration
– Often variables have an initial value of zero or one, but not always
33
Ending a Loop
• If the number of iterations is known before the loop starts, the
loop is called a count-controlled loop
– Use a for loop
• Asking the user before each iteration if it is time to end the loop
is called the ask-before-iterating technique
– Appropriate for a small number of iterations
– Use a while loop or a do-while loop
34
Ending a Loop (Contd)
• For large input lists, a sentinel value can be used to signal the
end of the list
– The sentinel value must be different from all the other possible inputs
– A negative number following a long list of non-negative exam score
could be suitable
• 90
• 0
• 10
• -1 <= exam’s score cannot be a negative, so this score is sentinel value
35
Ending a Loop (Contd)
• Example – reading a list of scores followed by a sentinel value
36
Ending a Loop (Contd)
• class ExamAverager
37
Nested Loops
• The body of a loop can contain any kind of statements, including
another loop
38
Nested Loops (Contd)
System.out.print(‘*’);
System.out.println(); body of
inner loop
}
• Each time the outer loop body is executed, the inner loop body
will execute 5 times
• 20 times total
*****
*****
Output: *****
*****
39
Programming Example:
Output:
40
Precedence and Associativity Rules
41
Flow of Control
• As in most programming languages, flow of control in Java
refers to its branching and looping mechanisms
• Java has several branching mechanisms: if-else, if, and
switch statements
• Java has three types of loop statements: the while, do-
while, and for statements
• Most branching and looping statements are controlled by
Boolean expressions
– A Boolean expression evaluates to either true or false
– The primitive type boolean may only take the values true or
false
System.out.println("is acceptable.");
break;
default:
System.out.println("I didn't plan for");
System.out.println(numberOfFlavors + " flavors.");
break;
}
}
}
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-45
Display 3.2 A switch Statement
if (s1.equalsIgnoreCase(s2))
System.out.println("But the lines are equal, ignoring case.");
else
System.out.println("Lines are not equal, even ignoring case.");
53
Self-Test (2)
54