8.0. Statement Level Control Structures
Todayโs Contents
- Statement-Level Control Structures
- Selection Statements
- Iterative Statements
- Unconditional Branching
- Guarded Commands
Statement-Level Control Structures
- Imperative: expressions + assignments
- Functional: expression + functions
- Without control statements
- Difficult to make programs flexible and powerful
- Early, single control (
goto) was used - Nowadays, selection and iteration statements
- More control statements โ writability enhanced but readability restricted
- What is the best collection of control statements?
- [Issue] Should the control structure have multiple entries?
- Multiple entries increase flexibility but decrease readability
- Inclusion of
gotoโ cause multiple exits
Selection Statements
- Means of choosing between two or more paths
- Two-way selection statements
- Multiple-selection statements
Two-way selection statements
- General form
if control_expression
then clause
else clause
- Design issues
- What is the form and type of the expression that controls the selection?
- How are the
thenandelseclauses specified? - How should the meaning of nested selectors be specified?
- Design issue: What is the form and type of the expression that controls the selection?
- Without
thenreserved word โ parentheses is needed - Without Boolean data type โ arithmetic expressions are used
if(์ด๋ค expression??) ๊ดํธ๊ฐ ํ์ํ๊ฐ?thenkeyword?- C / Java: โ ๋ฐ๋์
boolean(๋๋boolean์ฒ๋ผ ํด์ ๊ฐ๋ฅํ ๊ฐ) - Design issue: How are the
thenandelseclauses specified? - [Perl] use block
{}
if ($x > $y) {
$x = $y;
print "case 1";
} else {
print "case 2";
}
- Single or compound statements
- [Perl] Highly recommended to use compound statement -> ๋ชจํธ์ฑ ์ ๊ฑฐ
- Clause form have implications on meaning of nested selectors
- [C] use braces
if (x > y){
x = y;
printf("case 1");
}
- [Python] use indentation
if x > y :
x = y
print "case 1"
- [Ada] use
end if
if x > y then
x := y;
Put("case 1");
end if;
- Python โ indentation์ผ๋ก ๊ตฌ์กฐ ๊ฐ์ โ ๋ชจํธ์ฑ ์์
- Ada โ
end if๋ก ๋ช ํํ ๊ตฌ๋ถ - Perl โ block
{}์ฌ์ฉ ๊ถ์ฅ โ ์ฌ์ค์ ๋ฌธ์ ํํผ - Design issue: How should the meaning of nested selectors be specified?
- ambiguous โ
if-then-elseโ - When
<stmt>โ<if_stmt>, two parse trees are possible with the same sentential form. - [Java] ambiguous โ
if-then-elseโ
if (sum == 0)
if (count == 0)
result = 0;
else
result = 1;
- Indentation is matter of Python and F#
- Use indicator (
{}) to resolve ambiguity - [Java] unambiguous โ
if-then-elseโ
if (sum == 0) {
if (count == 0)
result = 0;
}
else
result = 1;
- Force to match with the first
- All
thenandelseclauses are compound (ex. Perl) - matched with the first:
if (sum == 0) {
if (count == 0) {
result = 0;
}
} else {
result = 1;
}
- matched with the second:
if (sum == 0) {
if (count == 0) {
result = 0;
}
else {
result = 1;
}
}
- Use special word (ex.
end) (Fortran 95+, Ada, Ruby, Lua) - [Ruby] use
end
if a > b then
sum = sum + a
acount = acount + 1
else
sum = sum + b
bcount = bcount + 1
end
- matched with the first:
if sum == 0 then
if count == 0 then
result = 0
end
else
result = 1
end
- matched with the second:
if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
end
- ์ธ์ดํน์ง ์์ฝ
- C/Java:
{}๋ก ๋ฌถ์ด์ ํด๊ฒฐ - Python: indentation์ผ๋ก ํด๊ฒฐ
- Ada/Ruby:
end๋ก ๊ตฌ์กฐ ๋ช ์ - Selector expressions
- An expression refers to a statement that can be evaluated as a value.
- Return a value
- Statement is the smallest independent element that makes up a program, that is, it is the basic unit and minimum execution unit
- Expression is included in statement
- In case of functional languages (ML, F#, LISP)
- There should be
elsebecause it is expression (return a value) - [F#]
let y =
if x > 0 then x
else 2 * x;;
if๊ฐ expression ์ฆ: ๊ฐ์ ๋ฐ๋์ ๋ฐํํด์ผ ํจ
Multiple-selection statements
- Allows the selection of one of any number of statements (generalized)
- Two-way selection (
if-else) ์ฌ๋ฌ ๊ฐ๋ณด๋ค ๊ตฌ์กฐ์ , ๊ฐ๋ ์ฑ ์ข์ - Possible to implement it using two-way selectors and
goto, but cumbersome and unreliable (๊ธธ์ด์ง, ๊ฐ๋ ์ฑ ๋ฎ์์ง๊ณ , ์ค์ ๊ฐ๋ฅ์ฑ ์ฌ๋ผ๊ฐ)
if (x == 1) ...
else if (x == 2) ...
else if (x == 3) ...
- Design issues
- What is the form and type of the expression that controls the selection?
- How are the case values specified?
- How are the selectable segments specified?
- How should unrepresented selector expression values be handled, if at all?
- Is execution flow through the structure restricted to include just a single selectable segment?
- General form
switch (expression) {
case constant_expression1: statement1;
...
case constantn: statement_n;
[default: statementn + 1]
}
expressionandconstant_expressioncan be discrete type (integer, character and enum types)- Expression์ ํํ/ ํ์ --> ํ์ ์ ํ vs ์ ์ฐ์ฑ์ด ์ด์
- C/Java:
int,enum๋ฑ ์ ํ์ - Python: ๊ฑฐ์ ๋ชจ๋ ํ์ ๊ฐ๋ฅ
- Constant_expression์๊ฐ --> ๋จ์ ๊ฐ vs ๋ณต์กํ ํจํด
- C: ์์
- Pascal: ๋ฒ์
- Python: ํจํด
- Selectable statements can be sequences, compound or block (๋จ์ผ statement or ์ฌ๋ฌ statement (block))
[default]is for unrepresented values- Execution flow through the structure
- No implicit branch out (Statement in
defaultalways is executed) - Explicit branch out should be used:
break(restrictedgoto) - [C-based]
switch (index) {
case 1:
case 3: odd += 1;
sumodd += index;
case 2:
case 4: even += 1;
sumeven += index;
default: printf("Error in switch, index = %d\n", index);
}
- ์คํ ํ๋ฆ:
case 1โ ์คํ โ ๊ทธ ๋ค์ ์๋์ผ๋กcase 2,default๊น์ง ๋ค ์คํ (implicit branch out ์์, fall-through ๋ฐ์) - Convenient to allow control to flow from one selectable code segment to another
case 1,2are empty, so control flows are changed tocase 3,4- Reliability problem when location of
breakis inadequate - ์ฝ๋ ๊ณต์ ๊ฐ๋ฅ --> case grouping ์ฝ๊ฒ ๊ฐ๋ฅ
switch (index) {
case 1:
case 3: odd += 1;
sumodd += index;
break;
case 2:
case 4: even += 1;
sumeven += index;
break;
default: printf("Error in switch, index = %d\n", index);
}
- No restrictions on the placement of the case expressions
switch (x)
default:
if (prime(x))
case 2: case 3: case 5: case 7:
process_prime(x);
else
case 4: case 6: case 8: case 9: case 10:
process_composite(x);
โ ์ฝ๋ ์ฝ๊ธฐ ์ด๋ ค์ โ ๊ตฌ์กฐ ๊นจ์ง โ powerful but dangerous
Language Specific Designs
[C#] Disallows the implicit execution of more than one segment (implicit fall-through ๊ธ์ง)
Explicit unconditional branch statement: either a
breakorgoto caseExpression and case can be string
switch (value) {
case -1:
Negatives++;
break;
case 0:
Zeros++;
goto case 1;
case 1:
Positives++;
break;
default:
Console.WriteLine("Error in switch \n");
break;
}
- ๋ณ๊ฒฝ ์ด์ = ์์ ์ฑ
- C ๋ฌธ์ :
break๋น ๋จ๋ฆฌ๋ฉด ๋ฒ๊ทธ, ์๋์น ์์ fall-through - C# ํด๊ฒฐ: ๋ฌด์กฐ๊ฑด ํ
case๋ง ์คํ, ์ฌ๋ฌ ๊ฐ ์คํํ๋ ค๋ฉด ํ๋ก๊ทธ๋๋จธ๊ฐ ๋ช ์์ ์ผ๋ก ์์ฑ - [PHP] Allow more type flexibility (including scalar, string, double precision)
- ํ์ ์ด ๋ฌ๋ผ๋ ๊ฐ์ด ๊ฐ์ผ๋ฉด true๋ก ๋ณด๋ ๋น๊ต (์ํ)
switch ("10") {
case 10:
echo "match";
}
- --> ์คํ๋จ
- [Ruby] Multiple-selection constructs
case
when Boolean_expression then expression
...
when Boolean_expression then expression
[else expression]
end
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
end
Ruby์
case๋ fall-through ์์ด ํ๋์ ์กฐ๊ฑด๋ง ์คํํ๋ฉฐ, expression์ผ๋ก ๊ฐ์ ๋ฐํImplementation
Translate: Selectable segments (n label) precedes branches
Put pairs of case value and label into table
Use linear search for execution
When n is large, hash table can be used
If case values are ranges of values, binary search table can be used
If range is narrow โ array (bin): very fast
Determining methods require costs too
switch๋ โ๋ฌธ๋ฒโ์ผ ๋ฟ ์ค์ ๋ก๋: ๋น๊ตํ๊ณ ์ ํํ๋ ๊ตฌ์กฐswitch๋ฌธ์ ๋ด๋ถ์ ์ผ๋ก ์กฐ๊ฑด ๋น๊ต์ ๋ถ๊ธฐ (goto)๋ก ๊ตฌํ๋ ์ ์์case๊ฐ๊ณผ label์ ๋งคํํ์ฌ ๋ถ๊ธฐMultiple-selection statements using
ifswitch/casestatement is inadequate when decision is based on Boolean expressionNested two-way selectors or use special word (
else-ifclause)
if count < 10 :w
bag1 = True
elif count < 100 :
bag2 = True
elif count < 1000 :
bag3 = True
if count < 10 :
bag1 = True
else :
if count < 100 :
bag2 = True
else :
if count < 1000 :
bag3 = True
else :
bag4 = True
- It is not easy to represent above using switch statement -> So,
else-ifstatement is not redundant - [Scheme] multiple selector is based on mathematical conditional expressions
CONDfunction- More than one predicate to be true (์ฒซ ๋ฒ์งธ true๋ง ์คํ)
- Value is returned as the value of
COND, so should haveELSE
Iterative Statements
- Loop, essence of programming
- Design issues
- How is the iteration controlled? (Logical, counting, or a combination of the two)
- Where should the control mechanism appear in the loop statement? (Top or bottom of the loop: Pretest (
while) - iteration statement (for) - posttest (do-while)) - Categories
- Counter-controlled loops
- Logically controlled loops
- User-located loop control mechanisms
- Iteration based on data structures
Counter-controlled loops
- Controlled by counter values (Loop parameters: initial and terminal values and stepsize)
- More complex than logically controlled but more demanding
- Design issues
- What are the type and scope of the loop variable? (Integer, enum, character, floating-point)
- Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control? (If changed, difficult to understand, complex)
- Should the loop parameters be evaluated only once, or once for every iteration? (If evaluated not once, complex but flexible)
- [Ada] Counter-controlled loops
discrete_rangecan be:1..10orMonday..Friday[reverse]: option for reverse order- Variable (
Count) is implicitly declared
for variable in [reverse] discrete_range loop
...
end loop;
- operational semantics
Count : Float := 1.35;
for Count in 1..10 loop
Sum := Sum + Count;
end loop;
ํน์ง: Loop ๋ณ์ ์๋ ์ ์ธ, ์ธ๋ถ ๋ณ์์ ๋ ๋ฆฝ, ๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐ, ๋ฒ์ ๊ณ ์ ,
reverse๊ฐ๋ฅ โ ๋งค์ฐ ์์ , ์ดํด ์ฌ์ โ loop control์ด ์ ๋ ๊นจ์ง์ง ์์Outside
Countis unaffected by loop variableCountEvaluated only once (in each iteration) โ not affect loop control
[C-based] Counter-controlled loops
loop body can be single, compound or null statement
expression_1: initialization and evaluated only onceexpression_2: loop control and evaluated before each execution (If absent, true โ infinite loop)expression_3: executed after each execution (loop counter)ํน์ง: ๋งค์ฐ ์ ์ฐ, ๋ณ์ ์ฌ๋ฌ ๊ฐ ๊ฐ๋ฅ, ์กฐ๊ฑด ๋ณต์กํ๊ฒ ๊ฐ๋ฅ โ ํ์ง๋ง: ๋ณต์ก + ์ค๋ฅ ๊ฐ๋ฅ
Multiple expressions and multiple loop variables increase flexibility
[C99 and C++]
Boolean expression is possible
Variable definitions is possible
Variables in loop parameters can be changed
Less reliable, more complex than Ada (loop control ๊นจ์ง)
[Python] Counter-controlled loops
value of
loop_variableis fromobject
for loop_variable in object:
...
- ํน์ง: ์นด์ดํฐ ์์, iterable ๊ธฐ๋ฐ, ๊ฐ์ ์์ฑํด์ ์ํ
range(a,b,c)is a function to make sequences starting fromatob-1with stepsizec- Functional Languages
- There is no counter variable in functional language -> Use recursion
- Counter can be a parameter for function
- [F#]
rec: denote recursive function
let rec forLoop loopBody reps =
if reps <= 0 then
()
else
loopBody()
forLoop loopBody, (reps - 1);;
- Counter-controlled loops ์์ฝ
- Counter-controlled loop๋ ์ด๊ธฐ๊ฐ, ์ข ๋ฃ์กฐ๊ฑด, ์ฆ๊ฐ๋์ผ๋ก ๋ฐ๋ณต์ ์ ์ดํ๋ค.
- ์ฃผ์ ์ค๊ณ ์ด์: loop ๋ณ์ ํ์ ๊ณผ scope, loop ๋ณ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ ์ฌ๋ถ, loop ์กฐ๊ฑด ํ๊ฐ ์์
- Ada๋ ์์ ์ฑ์ ์ํด loop ๋ณ์๋ฅผ ๊ณ ์ ํ๋ค.
- C๋ ์ ์ฐ์ฑ์ ์ ๊ณตํ์ง๋ง ๋ณต์ก์ฑ๊ณผ ์ค๋ฅ ๊ฐ๋ฅ์ฑ์ด ๋๋ค.
- Python์ iterable ๊ธฐ๋ฐ ๋ฐ๋ณต์ ์ฌ์ฉํ๋ค.
- ํจ์ํ ์ธ์ด๋ recursion์ผ๋ก ๋ฐ๋ณต์ ๊ตฌํํ๋ค.
Logically controlled loops
- Repetition control is based on Boolean expression
- Design issues
- Should the control be pretest (0๋ฒ ์คํ ๊ฐ๋ฅ) or posttest (์ต์ 1๋ฒ ๋ณด์ฅ)? (
whileordo while) - Should the logically controlled loop be a special form of a counting loop or a separate statement?
- operational semantics & code example
- [Java] Similar to C and C++, except that control expression must be boolean type
while (x) // X
while (x > 0) // O
- [F#]
rec: denote recursive function - Logically controlled loops ์์ฝ
- Logically controlled loop๋ Boolean ์กฐ๊ฑด์ ์ํด ๋ฐ๋ณต์ ์ ์ดํ๋ค.
- pretest loop (
while)๋ ์กฐ๊ฑด์ ๋จผ์ ๊ฒ์ฌํ๋ฉฐ, 0๋ฒ ์คํ๋ ์ ์๋ค. - posttest loop (
do-while)๋ ์กฐ๊ฑด์ ๋์ค์ ๊ฒ์ฌํ๋ฉฐ, ์ต์ 1๋ฒ ์คํ๋๋ค. - ์ผ๋ถ ์ธ์ด์์๋ logically controlled loop๋ฅผ counting loop์ ํน์ ํํ๋ก ๋ณด๊ธฐ๋ ํ๋ค.
- ํจ์ํ ์ธ์ด์์๋ recursion์ ํตํด ์ด๋ฅผ ๊ตฌํํ๋ค.
User-located loop control mechanism
- Programmer to choose a location for loop control other than the top or bottom of the loop body
- Comparison of control structure:
- Integral part of the exit (์กฐ๊ฑด ์์ฒด๊ฐ exit์ ๊ฒฐ์ , ๊ตฌ์กฐ์ ์ผ๋ก ๊น๋, exit ์์น = ํญ์ ๋ฃจํ ์กฐ๊ฑด)
while (x > 0) {
...
}
- Separate condition and exit (์กฐ๊ฑด๊ณผ exit์ด ๋ถ๋ฆฌ๋จ, exit ์์น๊ฐ ์ค๊ฐ์ผ๋ก ์ด๋ ๊ฐ๋ฅ)
while (true) {
if (x <= 0) break;
}
- Design issues: Should only one loop body be exited, or can enclosing loops also be exited?
- ํ๋๋ง ํ์ถ (๊ธฐ๋ณธ) โ ์์ , ์ดํด ์ฌ์
- ์ฌ๋ฌ loop ํ์ถ ํ์ฉ โ ๊ฐ๋ ฅ, ๋ถ
goto๋๋ ๋จ โ ๊ฐ๋ ์ฑ โ breakandcontinuebreak: terminationcontinue: skip
while (sum < 1000) {
getnext(value);
if (value < 0) continue;
sum += value;
}
- Multiple exits are possible โ decrease readability
while (sum < 1000) {
getnext(value);
if (value < 0) break;
sum += value;
}
- It is okay, location of break is the first statement after loop
while (...) {
if (a) break;
...
if (b) break;
}
- Fulfill a common need for
gotostatements through a highly restricted branch statement (goto๋ ์ํ, ๋์ ์ ํ๋ ํํ ์ ๊ณต)
Iteration based on data structures
- ์งํ ๊ณผ์ : ์นด์ดํฐ(
for) ์ค์ฌ โ ๋ฐ์ดํฐ ์ค์ฌ(iteration)์ผ๋ก ์งํ (๊ฐ์ ์ง์ ์ธ๋ ๊ฒ ์๋๋ผ, ๋ฐ์ดํฐ ์์ฒด๋ฅผ ์ํ) - [Fortran]
Do Count = 1, 9, 2(Initial value is 1, last value is 9, step size is 2, Iterator internal function should be called) - [Python]
for count in range [0, 9, 2]:(rangeis iterator) - [Ada] subrange can be used (MyRange is used both to declare the array and iterate through the array)
subtype MyRange is Integer range 0..99;
MyArray: array (MyRange) of Integer;
for Index in MyRange loop
...
end loop;
- [C-based] Flexibility (EX: binary tree traversal,
traverse()is iterator) - ํน์ง: iterator ์ง์ ๊ตฌํ, traversal ์์
- ์ฅ์ : ์ด๋ค ๊ตฌ์กฐ๋ ๊ฐ๋ฅ
- ๋จ์ : ๋ณต์ก, ์ค์ ๊ฐ๋ฅ
- User-defined iteration in OOP (๋ฐ์ดํฐ๊ฐ โ์ด๋ป๊ฒ ์ํ๋๋์งโ๋ฅผ ๋ด๋ถ์ ์จ๊น)
- There are many abstract types โ require iterator for each type
- [Python] Iterable object
__iter__()returns the iterable object itself.__next__()returns the value for the next iteration. If there are no more values, aStopIterationexception can be raised.
class MyCounter(object):
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def __next__(self):
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
c = MyCounter(1, 10)
for i in c:
print(i, end=' ')
# ์คํ ๊ฒฐ๊ณผ: 1 2 3 4 5 6 7 8 9 10
- [Java] enhanced version of for (generic collection)
for (String myElement : myList) { ... }(Referred to as โforeachโ)- [C#] also have generic collection with built-in iterators
List<String> names = new List<String>();
names.Add("Bob");
names.Add("Carol");
names.Add("Alice");
...
foreach (String name in names)
Console.WriteLine(name);
- ํน์ง: ๋ด๋ถ iterator ์ฌ์ฉ, ๊ฐ๋ฐ์๋ ๋ชฐ๋ผ๋ ๋จ โ iterator ํจํด ๋ด์ฅ
- [Ruby] There are several iterators
- [counter-controlled loop]
times,upto - [iteration for array and hash]
each - Block is related to
yield, return the last evaluated value
>> 4.times {puts "Hey!"}
Hey!
Hey!
Hey!
Hey! => 4
- Object 4,
{}: block,>>: interactive interpreter
>> list = [2, 4, 6, 8]
=> [2, 4, 6, 8]
>> list.each {|value| puts value}
2
4
6
8
=> [2, 4, 6, 8]
- Block can have parameter by
|
1.upto(5) {|x| print x, " "}
# ์คํ ๊ฒฐ๊ณผ: 1 2 3 4 5
block์ ๋ฉ์๋์ ์ ๋ฌ๋๋ ์ต๋ช ์ฝ๋ ๋ธ๋ก์ด๋ฉฐ, ๋ง์ง๋ง expression์ ๊ฐ์ ๋ฐํeach: go through array,=>: return value of expressions,puts: display parameter- Iteration based on data structures ์์ฝ
- Iteration based on data structures๋ ์นด์ดํฐ๊ฐ ์๋ ๋ฐ์ดํฐ ์์ฒด๋ฅผ ์ํ
- iterator๋ ๋ฐ๋ณต์ ์ํ ๊ฐ์ ์์ฑํ๋ ๊ฐ์ฒด
- Python์
__iter__,__next__๋ฅผ ํตํด iteration์ ๊ตฌํ - Java/C#์
foreach๋ฅผ ํตํด iterator๋ฅผ ์ถ์ํ - Ruby๋
block๊ธฐ๋ฐ iteration์ ์ ๊ณต
Unconditional Branching
- Transfers execution control to a specified location in the program
- Unconditional branch (
goto): very powerful, but makes dangerous, decrease readability and reliability - [Java, Python, Ruby] No
gotostatement - [C, C#] (ex) in switch statement (
goto case 1;) - Camouflaged
gotostatements are used (break,continue)
Guarded Commands
- To provide control statements supporting a program design during development
- Nondeterminism is sometimes needed in concurrent programs (Chap. 13)
- For increased clarity in reasoning
- [EX]
fi: reserved word for closing
if <Boolean expression> -> <statement>
[] <Boolean expression> -> <statement>
[] ...
[] <Boolean expression> -> <statement>
fi
- Basic Idea: if the order of evaluation is not important, the program should not specify one
- Looks like multiple selection but different
- All Boolean expressions are evaluated each time (๋งค๋ฒ ๋ชจ๋ ๊ฒ์ฌ)
- Nondeterministically choose among true statements
- If none of the Boolean expressions is true --> a run-time error + termination
- Guarded command๋ ์ฐธ์ธ ์กฐ๊ฑด๋ค ์ค ํ๋๋ฅผ ๋น๊ฒฐ์ ์ ์ผ๋ก ์ ํํ๋ ์ ํ๋ฌธ
- This forces the programmer to consider and list all possibilities.
- [EX] Choice of Statement
if i = 0 -> sum := sum + i
[] i > j -> sum := sum + j
[] j > i -> sum := sum + i
fi
- If
i = 0andj > i, nondeterministically chooses between the first and third - If
iis equal tojand is not zero, a run-time error occurs - Allow programmer to know that order of execution is irrelevant
- ํต์ฌ ์๋ฏธ: ์ด๊ฑด โ์คํ ๋ฐฉ์โ์ด ์๋๋ผ ํ๋ก๊ทธ๋จ ๋ช ์ธ(specification)์ ๊ฐ๊น์
- ์? โ์ด๋ค ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ฉด ๋๋คโ, โ์ด๋ค ๊ฒฝ๋ก๋ก ๊ฐ๋์ง๋ ์ค์ํ์ง ์๋คโ
- ์ฅ์ : reasoning ์ฌ์, concurrency ๋ชจ๋ธ์ ์ ํฉ, ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ช ์ํ๊ฒ ๋ง๋ฆ
- ๋จ์ : ์ค์ ๊ตฌํ ์ด๋ ค์, ๋๋ฒ๊น ์ด๋ ค์, ์์ธก ๋ถ๊ฐ
- Get desired result without overspecifying (ํ์ ์ด์์ผ๋ก ์คํ ์์๋ฅผ ์ ํ์ง ์์)
- if
x==y, no matter which we assign tomax(๋ ์ค ์๋ฌด๊ฑฐ๋ ์ ํ ๊ฐ๋ฅ)
if x >= y -> max := x
[] y >= x -> max := y
fi
- [C]
if (x >= y)
max = x;
else
max = y;
์์ ์์, ์กฐ๊ฑด๋ ํ ๋ฐฉํฅ์ผ๋ก ๊ฐ์ ๋จ
- [EX] Loop can be also guarded
do <Boolean expression> -> <statement>
[] <Boolean expression> -> <statement>
[] ...
[] <Boolean expression> -> <statement>
od
- All Boolean expressions are evaluated on each iteration
- If more than one is true --> nondeterministically choose
- After, expressions are again evaluated
- When all expressions are simultaneously false, the loop terminates
do q1 > q2 -> temp := q1; q1 := q2; q2 := temp;
[] q2 > q3 -> temp := q2; q2 := q3; q3 := temp;
[] q3 > q4 -> temp := q3; q3 := q4; q4 := temp;
od
- ๊ณ์:
q1 > q2๋ฉด swap,q2 > q3๋ฉด swap,q3 > q4๋ฉด swap --> ์กฐ๊ฑด ๋ง๋ ๊ฒ ์๋ฌด๊ฑฐ๋ ์คํ - ๊ฒฐ๊ตญ:
q1 <= q2 <= q3 <= q4์ฆ: ์ ๋ ฌ๋จ --> ์ด๋ค ์์๋ก swapํ๋ , ๊ฒฐ๊ณผ ๋์ผ - ์ฅ์ : concurrency ๋ชจ๋ธ์ ์ ํฉ, reasoning ์ฌ์, ๋ชจ๋ ๊ฒฝ์ฐ ๊ณ ๋ คํ๊ฒ ๋ง๋ฆ
- Connection between control statements and program verification is intimate
- ํ๋ก๊ทธ๋จ ๊ตฌ์กฐ๊ฐ โ ์ฆ๋ช ๊ฐ๋ฅ์ฑ์ ๊ฒฐ์ ํจ
- Verification is impossible with
gotostatements (ํ๋ฆ์ด ์ฌ๊ธฐ์ ๊ธฐ ์ ํ, entry/exit ๋ถ๋ช ํ, loop ๊ตฌ์กฐ ๊นจ์ง, ๋ ผ๋ฆฌ์ ์ผ๋ก ์ถ์ ๋ถ๊ฐ๋ฅ) - Verification is possible with only selection and logical pretest loops (๊ตฌ์กฐ์ ํ๋ฆ, entry/exit ๋ช ํ, loop invariant ์ ์ ๊ฐ๋ฅ)
- Verification is relatively simple with only guarded commands
- ๋ชจ๋ ๊ฒฝ์ฐ ๋ช ์: ๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ค ์จ์ผ ํจ
- ์์ ์์: ์กฐ๊ฑด ์งํฉ๋ง ๊ณ ๋ ค โ reasoning ๋จ์
- ๊ฑฐ์ ๋ ผ๋ฆฌ์ โ ๊ฒฐ๊ณผ
Summary
- Statement-Level Control Structures
- Selection Statements
- Iterative Statements
- Unconditional Branching
- Guarded Commands

