• Mindscape ๐Ÿ”ฅ
    • Playlist ๐ŸŽง
  • Algorithm

    • 1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ
    • 1966๋ฒˆ: ํ”„๋ฆฐํ„ฐ ํ
    • Python ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ํŒ
    • C++ std::vector ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ
    • Vim ์‚ฌ์šฉ ๋งค๋‰ด์–ผ
  • Ubuntu

    • ๋ฆฌ๋ˆ…์Šค ์šฐ๋ถ„ํˆฌ GRUB ํฐํŠธ ๋ณ€๊ฒฝ
    • ์šฐ๋ถ„ํˆฌ ์ด๋ฏธ์ง€ ๋น„๋””์˜ค ์ธ๋„ค์ผ(๋ฏธ๋ฆฌ๋ณด๊ธฐ) ์•ˆ ๋ณด์ž„ ๋ฌธ์ œ ํ•ด๊ฒฐ
    • Wine ํ™˜๊ฒฝ์—์„œ ์นด์นด์˜คํ†ก ์‹คํ–‰ ์‹œ explorer.exe ๋œจ์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๋ฒ•
    • ์šฐ๋ถ„ํˆฌ Wine ์นด์นด์˜คํ†ก ์‚ฌ์ง„ ์ด๋ฏธ์ง€ ์Šคํฌ๋ฆฐ์ƒท ๋ถ™์—ฌ๋„ฃ๊ธฐ
    • Wine ์นด์นด์˜คํ†ก ์ด๋ชจ์ง€ ๊นจ์ง ๋ฌธ์ œ ํ•ด๊ฒฐ
    • Ubuntu ์œˆ๋„์šฐ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋„๊ธฐ
  • Wellness

    • ์ฐจ์ „์žํ”ผ (Psyllium Husk)
    • ์—‘์ŠคํŠธ๋ผ ๋ฒ„์ง„ ์˜ฌ๋ฆฌ๋ธŒ์œ  (Extra Virgin Olive Oil)
    • ์ž๊ฐ€๋น„๊ฐ•์„ธ์ฒ™ (Nasal Irrigation)
    • QCY HT08 (MeloBuds Pro Plus)
    • ์ฝ˜์„œํƒ€ (Concerta)
    • ์ธ๋ฐ๋†€ (Inderal)
    • ์„คํŠธ๋ž„๋ฆฐ (Sertraline)
    • ๋ฉœ๋ผํ† ๋‹Œ (Melatonin)
    • ์น˜๊ฒฝ๋ถ€ ๋งˆ๋ชจ์ฆ
    • ๋ฐ”๋ฒจ ์Šค์ฟผํŠธ (Barbell Squat)
  • Humanities

    • Nordvik, Russia
    • North Sentinel Island
    • ๋กฑ๊ณ ๋กฑ๊ณ (Rongorongo)
    • ๋ฐ”๋กœํฌ ์Œ์•… (Baroque Music)
  • Design

    • ๊ตฌ๊ธ€์˜ ์•„์ด์ฝ˜ ๋Œ€๊ฐœํŽธ โ€” 6๋…„ ๋งŒ์˜ ์‹ค์ˆ˜ ์ธ์ •
    • ์ œ๋Ÿด๋“œ ์  ํƒ€ โ€” ๋Ÿญ์…”๋ฆฌ ์Šคํฌ์ธ  ์›Œ์น˜์˜ ์ฐฝ์‹œ์ž
    • ๋ฐ”์šฐํ•˜์šฐ์Šค โ€” ํ˜„๋Œ€ ๋””์ž์ธ์˜ ์›์ 
  • Brands

    • NOMOS Glashรผtte
    • Frรฉdรฉrique Constant
    • KZ (Knowledge Zenith)
    • ์—์ŠคํŠธ๋ผ (AESTURA)
    • JINHAO (้‡‘่ฑช)
    • Herman Miller
    • ๋ฐ์Šค์ปค (DESKER)
    • ๋ฌด์‹ ์‚ฌ ์Šคํƒ ๋‹ค๋“œ (Musinsa Standard)
  • Finance

    • ํ˜„๋Œ€์นด๋“œ ZERO โ€” Edition2 vs Edition3 ๋น„๊ต
    • ์‹ ํ•œ์นด๋“œ ์ฒ˜์Œ
    • S&P 500 ETF ํˆฌ์ž ๊ฐ€์ด๋“œ
    • ํŒŒํ‚นํ†ต์žฅ vs CMA ํ†ต์žฅ
    • ๋ฒ„ํฌ์…” ํ•ด์„œ์›จ์ด (Berkshire Hathaway)
    • ๋น„ํŠธ์ฝ”์ธ(Bitcoin)
  • Products

    • ์˜ค๋””์˜ค ์ธํ„ฐํŽ˜์ด์Šค (Audio Interface)
    • ์ฟ ๋ฃจํ† ๊ฐ€ (KURUTOGA)
    • CX31993 DAC ๋™๊ธ€
    • ํด๋ Œ์ง• ๋ฐ€ํฌ (Cleansing Milk)
    • ํ”ผ์ ฏ ํ† ์ด (Fidget Toy)
    • ThinkPad
  • Programming Languages

    • 8.0. Statement Level Control Structures
    • 8. Subprogram
    • 9. Implementing Subprogram
    • 10.1. Abstract Data Types and Encapsulation Constructs
    • 10.2. Support for Object Oriented Programming
    • 11. Concurrency
    • 12. FPL (1)
    • 13. FPL (2)
    • 14. Exception Handling and Event Handling
    • Final Exam

8.0. Statement Level Control Structures

์ž‘์„ฑ 2026. 6. 12.ยท์ˆ˜์ • 2026. 6. 12.

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 then and else clauses 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 then reserved word โ†’ parentheses is needed
  • Without Boolean data type โ†’ arithmetic expressions are used
  • if (์–ด๋–ค expression??) ๊ด„ํ˜ธ๊ฐ€ ํ•„์š”ํ•œ๊ฐ€? then keyword?
  • C / Java: โ†’ ๋ฐ˜๋“œ์‹œ boolean (๋˜๋Š” boolean์ฒ˜๋Ÿผ ํ•ด์„ ๊ฐ€๋Šฅํ•œ ๊ฐ’)
  • Design issue: How are the then and else clauses 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 then and else clauses 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 else because 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]
}

  • expression and constant_expression can 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 default always is executed)
  • Explicit branch out should be used: break (restricted goto)
  • [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, 2 are empty, so control flows are changed to case 3, 4
  • Reliability problem when location of break is 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 break or goto case

  • Expression 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 if

  • switch/case statement is inadequate when decision is based on Boolean expression

  • Nested two-way selectors or use special word (else-if clause)

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-if statement is not redundant
  • [Scheme] multiple selector is based on mathematical conditional expressions
  • COND function
  • More than one predicate to be true (์ฒซ ๋ฒˆ์งธ true๋งŒ ์‹คํ–‰)
  • Value is returned as the value of COND, so should have ELSE

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_range can be: 1..10 or Monday..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 Count is unaffected by loop variable Count

  • Evaluated 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 once

  • expression_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_variable is from object

for loop_variable in object:
    ...

  • ํŠน์ง•: ์นด์šดํ„ฐ ์—†์Œ, iterable ๊ธฐ๋ฐ˜, ๊ฐ’์„ ์ƒ์„ฑํ•ด์„œ ์ˆœํšŒ
  • range(a,b,c) is a function to make sequences starting from a to b-1 with stepsize c
  • 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๋ฒˆ ๋ณด์žฅ)? (while or do 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 ๋А๋‚Œ ๋‚จ โ†’ ๊ฐ€๋…์„ฑ โ†“
  • break and continue
  • break: termination
  • continue: 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 goto statements 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]: (range is 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, a StopIteration exception 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 goto statement
  • [C, C#] (ex) in switch statement (goto case 1;)
  • Camouflaged goto statements 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 = 0 and j > i, nondeterministically chooses between the first and third
  • If i is equal to j and 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 to max (๋‘˜ ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ์„ ํƒ ๊ฐ€๋Šฅ)
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 goto statements (ํ๋ฆ„์ด ์—ฌ๊ธฐ์ €๊ธฐ ์ ํ”„, 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
์ตœ๊ทผ ์ˆ˜์ •: 26. 6. 12. ์˜คํ›„ 3:28
Contributors: kmbzn, Claude Sonnet 4.6
Next
8. Subprogram

BUILT WITH

CloudflareNode.jsGitHubGitVue.jsJavaScriptVSCodenpm

All trademarks and logos are property of their respective owners.
ยฉ 2026 kmbzn ยท MIT License