본문 바로가기

코딩 책공부16

관계연산자 관계연산자는 ==, > , >=, = second); Console.WriteLine($"{first}=={second}:{result}"); result = (first second); Console.WriteLine($"{first}=={second}:{result}"); - first > second 를 계산하여 결과를 result에 할당하고 출력한다. result = (first = second); Console.WriteLine($"{first}=={second}:{result}"); - - first >= second 를 계산하여 결과를 result에 할당하고 출력한다. result = (first 2024. 4. 18.
OverflowException과 checked키워드 namespace _066_A019_Overflow { class Program { static void Main(string[] args) { Console.WriteLine("int.MaxValue = {0}", int.MaxValue); int x = int.MaxValue, y = 0; y = x + 10; Console.WriteLine("int.MaxValue + 10 = {0}", y); } } } static void Main(string[] args) { Console.WriteLine("int.MaxValue = {0}", int.MaxValue); - int.MaxValue를 출력한다. int.MaxValue는 부호를 갖는 32비트 정수가 표현할 수 있는 최대값으로 2,147,483,.. 2024. 4. 17.
DivideByZeroException과 try~catch문 C#에서는 실행 중에 나오는 에러를 예외(Exception) 라고 한다. 산술 연산에서 나올 수 있는 예외는 0으로 나눔 예외(DivideByZeroException)와 오버플로우 예외 (OverflowException)이다. namespace _064_A018_DivideByZero { class Program { static void Main(string[] args) { int x = 10, y = 0; Console.WriteLine(10.0 / y); Console.WriteLine(x / y); } } } 10.0/y 는 실수를 0으로 나누기 때문에 예외가 아니고 ∞를 출력한다. x/y를 할 때 y값이 0이기 때문에0으로 나눔 예외가 발생한다. 프로그램 중에 나누기 계산을 할 때는 위의 예제와.. 2024. 4. 16.
산술연산자 산술연산자는 4개의 사직연산자(+, -, *, /) 와 나머지(%) 연산자로 총 5가지가 있다. 연산의 결과는 숫자이다. 산술연산자에서 중요한것은 자료형이다. 즉, 피연산자의 자료형에 따라 계산 결과값의 자료형도 결정된다. 특히 주의해야 하는 것은 "정수/정수"의 결과는 정수라는 점이다. 예를 들어, 1/2의 결과는 0.5가 아니고 0이 된다. "정수/실수"의 결과는 실수이다. C나 c++과 달리 %연산자는 실수형에도 사용할 수 있다. namespace _062_A017_ArithmeticOperators { internal class Program { static void Main(string[] args) { Console.WriteLine("정수의 계산"); Console.WriteLine(123 .. 2024. 4. 15.