배움 저장소
홍정모의 따라하며 배우는 C언어 6 반복문 본문
6.1 scanf( )의 반환값 사용하기
int num, sum = 0;
int status;
printf("Enter Next Integer (q to quit) : ");
status = scanf("%d", &num); // (int) num에 문자를 넣으면
// status = 0이된다.
while(status == 1)
{
sum = sum + num;
printf("Enter Next Integer (q to quit) : ");
status = scanf("%d", &num);
}
printf("Sum = %d \n", sum);
scanf()함수를 while 조건문에 바로 넣을 수 있다.
int num, sum = 0;
printf("Enter Next Integer (q to quit) : ");
while(scanf("%d", &num) == 1)
{
sum = sum + num;
printf("Enter Next Integer (q to quit) : ");
}
printf("Sum = %d \n", sum);
6.3 진입조건 루프 While
While = Entry condition loop
while (expression)
statement
6.4 관계연산자
// #include <math.h>
const double PI = 3.1415926535897932;
double guess = 0.0;
// 실수끼리 비교는 정밀한 숫자를 요구한다.
while ( guess != PI );
// 간단하게 할 수 있다. // fabs return
while ( fabs(guess - PI) > 0.01 ) // The absolute value of input
{
printf("Fool! Try again.\n");
scanf("%lf", &guess);
}
printf("Good!");
return 0;
while 조건문의 true/false 판단 기준은 0이다. 0일 때만 false, 나머지 숫자에서는 true로 간주한다.
6.6 _Bool의 자료형
_Bool 자료형은 많은 사람이 만들어 사용하고 있는 bool 자료형과 구분하기 위하여 underscore를 붙였다.
while(i = 5) { /* Do something */ }; // 가능하다. 표현식(expression)
while(i == 5){ /* Do something */ };// 하지만 이 표현의 실수일 가능성이 높다.
_Bool boolean_true = (2 > 1);
_Bool boolean_false = (2 < 1);
printf("True is %d\n", boolean_true);
printf("False is %d\n", boolean_false);
// 삼항 연산자
printf(boolean_true ? "true" : "false");
printf("\n");
printf(boolean_false ? "true" : "false");
_Bool 자료형은 정수형(int)로 저장된다.
bool 자료형을 사용하려면 다음 헤더파일을 포함해야 한다. <stdbool.h>
// #include <stdbool.h>
bool bt = true;
bool bf = false;
printf("True is %d \n", bt);
printf("False is %d \n", bf);
// stdbool.h 내부
#define bool _Bool
#define false 0
#define true 1
6.7 관계연산자의 우선순위
https://www.tutorialspoint.com/cprogramming/c_operators_precedence.htm
Operators Precedence in C
Operators Precedence in C Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher preceden
www.tutorialspoint.com
6.8 For Loop
// 동작하지만 권장하는 사용방법은 아니다
for(printf("Let's Go\n"); i!=7; scanf("%d", &i));
6.11 콤마 연산자
int i, j;
i = 1;
i++, j=1; // comma is sequence point
printf("%d %d\n", i, j);
int x,y,z;
z = x = 1, y = 2; // 1. z = (x = 1)
// 2. y = 2
z = ((x=1), (y=2)); // z = 2
// z = (1,2)일 때 뒤에 있는 값을 할당한다
이때 sequence point를 확인해보자.
int a, saved;
a = 0;
a++, (saved = printf("a=%i,", a)), (a = printf("a=%i,saved=%i", a, saved));
Output
>> a = 1, a = 1, saved = 4
1. comma(,)를 만나고 a++이 실행된다. a=0에서 a=1이 된다.
2. printf(...) 함수에서 a=1가 출력된다.
3. printf( )의 반환값은 출력한 문자 개수이다. '\0'와 같이 \ + character 형식은 하나의 문자로 취급한다.
6.12 제논의 역설 시뮬레이션 예제
1. 값 2는 int이다. double로 쓰려면 값 2.0를 사용해야 한다.
2. 곱하기가 나누기보다 빠르다.
6.13 탈출조건 루프 Do while
do while 조건문은 조건을 확인하기 전 body에 있는 statement를 한 번 실행시킨다. 따라서 body가 반드시 한 번이상 실행된다. while문 끝에 semicolon( ; )을 붙여주자.
int secret_code = 10;
int guess;
do
{
printf("Enter secret Code: ");
scanf("%d", &guess);
}
while (guess != secret_code);
6.16 배열
test[i]가 주소인 줄 알았는데 값이다. 값을 할당하기 위하여 &(ampersand)를 붙여주어야 함
int test[4];
for(int i=0; i<4; ++i)
{
scanf("%d", &test[i]);
}
for(int i=0; i<4; ++i)
{
printf("%d ", test[i]);
}
#define count 4
int test2[count] // O
int const coun = 4;
int test3[coun] // X : Error
6.18 pow 구현하기
int base, exp, result, i;
while( scanf("%d %d", &base, &exp) == 2 )
{
result = 1;
for(i=0; i<exp; ++i)
{
result *= base;
}
printf("result=%d\n", result);
}
return 0;
'Programming Language > C' 카테고리의 다른 글
홍정모의 따라하며 배우는 C언어 8 문자 입출력과 유효성 검증 (0) | 2021.11.15 |
---|---|
홍정모의 따라하며 배우는 C언어 7 분기 (0) | 2021.11.15 |
홍정모의 따라하며 배우는 C언어 5 연산자, 표현식, 문장 (0) | 2021.11.11 |
홍정모의 따라하며 배우는 C언어 4 문자열과 형식지정자 (0) | 2021.11.10 |
홍정모의 따라하며 배우는 C언어 3 (0) | 2021.11.09 |