전공/프로그래밍언어론

Statement - Level Control Structures

nongdamgom 2023. 12. 28. 12:46

Chapter 8. Statement - Level Control Structures

(Selection Statements , Iterative Statements 까지 일 듯)

Levels of control Flow

  • Within expressions(ch.7)

--> operator associativity and precedence level

  • Among program units(ch.9)

--> subprograms

  • Among program statements (this chapter)

Contol Statements

Imperative language

  • 명령형 언어에서의 computation : expression을 평가하고 resulting value를 variable에 assigning
  • 이 computation을 더 flexible하고 powerful 하게 만들기 위한 추가적인 machanisms 존재

--> Selecting among alternative control flow path. ex) if, switch

--> Causing the repeated execution. ex) while, for

  • 이같은 기능을 제공하는 statement를 control statements라고 한다.

Functional language

  • 함수형 언어에서의 computation : expression을 평가하고 주어진 parameter를 function에 applying
  • expression과 function의 flow는 다른 expression과 function에 의해 제어됨

Control Structure

  • control statement와 이것이 실행을 제어하는 statement들의 collection
  • Design question

--> Should a control structure have multiple entries?

/* C code with multiple entries */ int main(){ int i = 0; start_loop: // 1 if(i < 5){ printf("%d\n", i); i++; goto start_loop; // 2 } return 0; }

Select Statements

  • choosing between two or more paths of execution

--> Two - way selectors

--> Multiple - way selectors

Two-Way Selection Statements

/* General form */ if control_expression then clause else clause
  • Design Issues:

--> What is the form and type of the control expression?

--> How are the then and else clauses specified?

--> How should the meaning of nested selectors be specified?

The Control Expression

  • then 이 사용되지 않으면, () 를 씀
  • C89, C99, Python, C++

--> control expression 에 arithmetic 사용 가능. ex> if((x+y) > 15)

Clause Form

  • single statements or compound statements
  • In Perl

--> 모든 clause를 다 { } 로 구분 (statement가 하나만 있어도 compound로 취급)

  • 대부분 언어들이 복합문을 { }로 구분
  • Python, Ruby : 복합문이라기보다 그냥 statement sequences
/* python */ if x > y : x = y; print "case 1"

--> then 대신 : (colon) 사용

--> indentation으로 compound 표현

Nesting Selectors

  • In Java
if(sum == 0) if(count == 0) result = 0; else result = 1; // if(count == 0)과 match 되는 else

--> Java's static semantics rule : else는 nearest previous 한 if랑 match 됨

if(sum == 0){ if(count == 0) result = 0; } else result = 1; // if(count == 0)과 match 되는 else

--> 이렇게 해야 if(sum == 0)과 else가 match됨

  • In Ruby (statemetn sequences as clasuses)
/*case 1*/ if sum == 0 then if count == 0 then result = 0 else // count == 0 과 match result = 1 end // 안쪽 if의 끝 end // 바깥쪽 if의 끝 /*case 2*/ if sum == 0 then if count == 0 then result = 0 end // 안쪽 if의 끝 else // sum == 0 과 match result = 1 end // 바깥쪽 if의 끝
  • python 은 indentation으로 구분 // 쓰기 귀찮음 니가 아는 그거 맞음

Selector Expressions

  • ML, F#, Lisp 에서 selector는 statement가 아닌, 값을 반환하는 expression임
/* F# */ let x = 10 let result = if x > 0 then printfn "positive" else () // unit type --> 값 넣기 싫을 때

--> if 식이 값을 반환하려면, else절을 반드시 가져야함

--> else절에 반환할 값이 없으면, unit type 이용(no value 라는 뜻)

--> then과 else절의 return value type 은 반드시 같아야함

to be continued...