Вы находитесь на странице: 1из 30

프로그램 구조 및 데이터 관련 개념

Namespace

Class 구조체 인터페이스


생성자 생성자 메소드
소멸자 상수 속성 ,
이벤트
상수 필드
인덱서
필드 메소드
메소드 속성 열거형
속성 인덱서
인덱서 연산자
연산자 대리자
이벤트
다른 클래스 다른 Namespace

2021년 11월 2일 C# 프로그래밍 1


Statements Blocks

 Use Braces As
Block Delimiters  A Block and Its
Parent Block Sibling Blocks Can
{ Cannot Have a Have Variables with
// codes Variable with the the Same Name
Same Name {
{ int i ;
}
int i ; }
{ …
int i ; {
} int i ;
} }

2021년 11월 2일 C# 프로그래밍 2


Types of Statements

Selection Statements : if , switch

Iteration Statements : while, do, for, foreach

Jump Statements : goto, break, continue

2021년 11월 2일 C# 프로그래밍 3


• Using Selection Statements

 The if Statement

 Cascading if Statements

 The switch Statement

 Quiz : Spot the Bugs

2021년 11월 2일 C# 프로그래밍 4


The if Statement

 Syntax :

if { Boolean - expression }
first-embedded-statement
else
second-embedded-statement

 No Implicit Conversion from int to bool


int x ;

if (x) … // Must be if (x != 0) in C#
if (x = 0) … // Must be if (x == 0) in C#

2021년 11월 2일 C# 프로그래밍 5


Cascading if Statements

enum Suit { Clubs, Hearts, Diamonds, Spades }


Suit trumps = Suit.Hearts ;
if ( trumps == Suit.Clubs )
color = "Black" ;
else if ( trumps == Suit.Hearts )
color = "Red" ;
else
color = "Black" ;

2021년 11월 2일 C# 프로그래밍 6


The switch Statement

 Use switch Statements for Multiple Case Blocks


 Use break Statements to Ensure That No Fall
Through Occurs
switch (trumps) {
case Suit.Clubs :
case.Suit.Spades :
color = "Black" ; break ;
case Suit.Hearts :
case.Suit.Diamonds :
color = "Red" ; break ;
default : color = "ERROR" ; break ;
}
2021년 11월 2일 C# 프로그래밍 7
The switch Statement

 You cannot group a collection of constants


together in a single case label.
 Each constant must have its own case label.
 Can use case & default label as the destination of
a goto statement.
 Evaluate the following types of expressions
• integer
• char
• enum
• string

2021년 11월 2일 C# 프로그래밍 8


• Using Iteration Statements

 The while Statement


 The do Statement
 The for Statement
 The foreach Statement
 Quiz : Spot the Bugs

2021년 11월 2일 C# 프로그래밍 9


The while Statement

 Execute Embedded Statements Based on Boolean


Value
 Evaluate Boolean Expression at Beginning of Loop
 Execute Embedded Statements While Boolean
Value is True

int i = 0 ;
while ( i < 10 ) {
Console.WriteLine( i ) ;
i ++ ;
}

2021년 11월 2일 C# 프로그래밍 10


The do Statement

 Execute Embedded Statements Based on Boolean


Value
 Evaluate Boolean Expression at End of Loop
 Execute Embedded Statements While Boolean
Value is True

int i = 0 ;
do {
Console.WriteLine( i ) ;
i ++ ;
} while ( i < 10 );

2021년 11월 2일 C# 프로그래밍 11


The for Statement

 Place Update Information at the Start of the Loop


for ( int j = 0 ; j < 10 ; j ++ ) {
Console.WriteLine ( j ) ; }
 Variables in a for Block Are Scoped Only Within
the Block
for ( int j = 0 ; j < 10 ; j ++ ) {
Console.WriteLine ( j ) ;
Console.WriteLine ( j ) ; // Error

 A for Loop Can Iterate Over Several Values

for ( int j = 0, k = 0 ; ….. j ++, k ++ )

2021년 11월 2일 C# 프로그래밍 12


The foreach Statement

 Choose the Type and Name of the Iteration


Variable
 Execute Embedded Statements for Each Element
of the Collection Class

ArrayList numbers = new ArrayList( ) ;


for ( int j = 0; j < 10; j ++ ) {
numbers.Add ( j ) ;
}

foreach ( int number in numbers ) {


Console.WriteLine (number) ; }

2021년 11월 2일 C# 프로그래밍 13


• Using Jump Statements

 The goto Statement


 The break and continue Statement

2021년 11월 2일 C# 프로그래밍 14


The goto Statement

 Flow of Control Transferred to a Labeled


Statement
 Can Easily Result in Obscure "Spaghetti" Code

if ( number % 2 == 0) goto even ;


Console.WriteLine ("odd") ;
goto End ;
Even :
Console.WriteLine ("even") ;
End :

2021년 11월 2일 C# 프로그래밍 15


The break and continue Statements

 The break Statement Jumps out of an Iteration


 The continue Statement Jumps to the Next
Iteration

int j = 0 ;
while ( true ) {
Console.WriteLine ( j ) ;
j ++ ;
if ( j < 10 )
continue ;
else
break ;
}

2021년 11월 2일 C# 프로그래밍 16


• Handling Basic Exceptions

 Why Use Exceptions ?


 Exception Objects
 Using try and catch Blocks
 Multiple catch Blocks

2021년 11월 2일 C# 프로그래밍 17


Why Use Exceptions ?

 Traditional Procedural Error Handling is


Cumbersome

int errorCode ;
File source = new File ("code.cs") ;
if (errorCode == -1) goto Failed ;
int length = (int) source.Length ;
if (errorCode == -2) goto Failed ;
char [ ] contents = new char [ length ] ;
if (errorCode == -3) goto Failed ;
// Succeeded …
Failed: …

2021년 11월 2일 C# 프로그래밍 18


Exception Objects

Exception

SystemException

OutOfMemoryException

IOException

NullReferenceException

ApplicationException

2021년 11월 2일 C# 프로그래밍 19


Using try and catch Blocks

 Object-Oriented Solution to Error Handling


• Put the normal code in a try block
• Handle the exceptions in a separate catch
block
try {
Console.WriteLine ("Enter a number") ;
int j = int.Parse(Console.ReadLine( )) ;
}
catch (OverflowException caught)
{
Console.WriteLine (caught) ;
}

2021년 11월 2일 C# 프로그래밍 20


Multiple catch Blocks

 Each catch Block Catches One Class of Exception


 A try Block Can Have One General Catch Block
 A try Block is Not Allowed to Catch a Class That is
Derived from a Class Caught in an Earlier catch
Block
try { Console.WriteLine ("Enter a number") ;
int j = int.Parse(Console.ReadLine( )) ;
Console.WriteLine ("Enter a number") ;
int k = int.Parse(Console.ReadLine( )) ;
int m = j / k ;
}
catch (OverflowException caught) { … }
catch (DevideByZeroException caught) { … }
2021년 11월 2일 C# 프로그래밍 21
try…catch…finally

try { int [] grades = new int [10];


grades[10] = 100; }
catch (IndexOutOfRangeException e)
{ Console.WriteLine(“…”); }
catch (DevideByZeroException e)
{ Console.WriteLine(“…”); }
catch (Exception e)
{ Console.WriteLine(“e.Message”); }
finally
{ Console.WriteLine(“Always”); }

2021년 11월 2일 C# 프로그래밍 22


각 블록의 용도

 try : 예외발생 코드를 감싼다 .


 catch : 예외 발생시 , 실행될 코드
 finally : 무조건 실행할 코드

 try… catch… catch… finally…


 try… catch… catch…
 try… finally…

 throw e;

2021년 11월 2일 C# 프로그래밍 23


Raising Exceptions - throw

 Throw an Approate Exception


 Give the Exception a Meaningful Message
throw expression ;

 System-Defined Exception
필요시에 , 런타임은 throw 문을 수행하여
시스템에서 정의한 예외를 발생시키고 프로그램은
즉시로 catch 문을 조사하게 된다 .

2021년 11월 2일 C# 프로그래밍 24


Raising Your Own Exception

 Can use the throw statement to raise your own


exception
if (min < 1 || min >= 60) {
string fault = min + "is not a valid minute";
throw new InvalidTimeException(fault); }

 User-defined Exception
class InvalidTimeException : ApplicationException
{ … }

2021년 11월 2일 C# 프로그래밍 25


• Raising Exceptions

 The throw Statement


 The finally Clause
 Checking for Arithmetic Overflow
 Guidelines for Handling Exceptions

2021년 11월 2일 C# 프로그래밍 26


The throw Statement

 Throw an Appropriate Exception


 Give the Exception a Meaningful Message

throw expression ;

if (minute < 1 || minute >= 60) {


throw new InvalidTimeException (minute +
"is not a valid minute");
// !! Not reached !!
}

2021년 11월 2일 C# 프로그래밍 27


The finally Clause

 All of the Statements in a finally Block Are Always


Executed

CriticalSelection.Enter ( x ) ;
try {
...
}
finally {
CriticalSelection.Exit ( x ) ;
}

2021년 11월 2일 C# 프로그래밍 28


Checking for Arithmetic Overflow

 By Default, Arithmetic Overflow is Not Checked


 A checked statement turns overflow checking
on
checked {
int number = int.MaxValue ;
Console.WriteLine (++number) ;
}

unchecked {
int number = int.MaxValue ;
Console.WriteLine (++number) ;
}

2021년 11월 2일 C# 프로그래밍 29


Guidelines for Handling Exceptions

 Throwing
• Avoid exceptions for normal or expected cases
• Never create and throw objects of class
Exception
• Include a description string in an Exception
object
• Throw objects of the most specific class
possible
 Catching
• Arrange catch blocks from specific to general
• Do not let exceptions drop off Main

2021년 11월 2일 C# 프로그래밍 30

Вам также может понравиться

  • Computer System Architecture
    Computer System Architecture
    Документ25 страниц
    Computer System Architecture
    kanggun0225
    Оценок пока нет
  • Chap 04
    Chap 04
    Документ59 страниц
    Chap 04
    kanggun0225
    Оценок пока нет
  • Chap 01
    Chap 01
    Документ26 страниц
    Chap 01
    kanggun0225
    Оценок пока нет
  • Chap 02
    Chap 02
    Документ12 страниц
    Chap 02
    kanggun0225
    Оценок пока нет
  • 파일 입출력
    파일 입출력
    Документ6 страниц
    파일 입출력
    kanggun0225
    Оценок пока нет
  • Chap 03
    Chap 03
    Документ25 страниц
    Chap 03
    kanggun0225
    Оценок пока нет
  • 나눗셈 하드웨어 구성도
    나눗셈 하드웨어 구성도
    Документ25 страниц
    나눗셈 하드웨어 구성도
    kanggun0225
    Оценок пока нет
  • 중앙처리장치의 명령어
    중앙처리장치의 명령어
    Документ64 страницы
    중앙처리장치의 명령어
    kanggun0225
    Оценок пока нет
  • Computer System Architecture
    Computer System Architecture
    Документ22 страницы
    Computer System Architecture
    kanggun0225
    Оценок пока нет
  • 조합 논리회로
    조합 논리회로
    Документ32 страницы
    조합 논리회로
    kanggun0225
    Оценок пока нет
  • 주기억장치
    주기억장치
    Документ45 страниц
    주기억장치
    kanggun0225
    Оценок пока нет
  • Review 1 (1) 2 (2) 3
    Review 1 (1) 2 (2) 3
    Документ2 страницы
    Review 1 (1) 2 (2) 3
    kanggun0225
    Оценок пока нет
  • 캐시 기억장치
    캐시 기억장치
    Документ37 страниц
    캐시 기억장치
    kanggun0225
    Оценок пока нет
  • 연습문제풀이 / 도움말
    연습문제풀이 / 도움말
    Документ63 страницы
    연습문제풀이 / 도움말
    kanggun0225
    Оценок пока нет
  • Transport Layer  Application Layer에서 받은 Data를
    Transport Layer  Application Layer에서 받은 Data를
    Документ9 страниц
    Transport Layer  Application Layer에서 받은 Data를
    kanggun0225
    Оценок пока нет
  • 카르노 맵
    카르노 맵
    Документ17 страниц
    카르노 맵
    kanggun0225
    Оценок пока нет
  • Data 의 표현
    Data 의 표현
    Документ25 страниц
    Data 의 표현
    kanggun0225
    Оценок пока нет
  • Chapter 6
    Chapter 6
    Документ7 страниц
    Chapter 6
    kanggun0225
    Оценок пока нет
  • 입력 / 출력
    입력 / 출력
    Документ31 страница
    입력 / 출력
    kanggun0225
    Оценок пока нет
  • 제어장치
    제어장치
    Документ48 страниц
    제어장치
    kanggun0225
    Оценок пока нет
  • Chapter 2. Application Layer
    Chapter 2. Application Layer
    Документ12 страниц
    Chapter 2. Application Layer
    kanggun0225
    Оценок пока нет
  • Chapter 1. Introduction
    Chapter 1. Introduction
    Документ8 страниц
    Chapter 1. Introduction
    kanggun0225
    Оценок пока нет
  • 07주 01강 실습 (실습1) 배열의 초기화 실습 (실행
    07주 01강 실습 (실습1) 배열의 초기화 실습 (실행
    Документ2 страницы
    07주 01강 실습 (실습1) 배열의 초기화 실습 (실행
    kanggun0225
    Оценок пока нет
  • 디지털 논리 회로
    디지털 논리 회로
    Документ37 страниц
    디지털 논리 회로
    kanggun0225
    Оценок пока нет
  • Chapter 5
    Chapter 5
    Документ11 страниц
    Chapter 5
    kanggun0225
    Оценок пока нет
  • Review 1 2 3 4 5 6
    Review 1 2 3 4 5 6
    Документ2 страницы
    Review 1 2 3 4 5 6
    kanggun0225
    Оценок пока нет
  • 06주 01강 실습
    06주 01강 실습
    Документ2 страницы
    06주 01강 실습
    kanggun0225
    Оценок пока нет
  • Review 1 2 3 4 5 6
    Review 1 2 3 4 5 6
    Документ2 страницы
    Review 1 2 3 4 5 6
    kanggun0225
    Оценок пока нет
  • Review 1 2 3 4 5 6
    Review 1 2 3 4 5 6
    Документ3 страницы
    Review 1 2 3 4 5 6
    kanggun0225
    Оценок пока нет
  • 03주 01강 실습
    03주 01강 실습
    Документ2 страницы
    03주 01강 실습
    kanggun0225
    Оценок пока нет