๐ก
๋๋ฐฐ์จ 6.1 ~ ๋ณธ๋ฌธ
6.1 while ๋ฐ๋ณต ๋ฃจํ์์ sannf()์ ๋ฐํ๊ฐ ์ฌ์ฉํ๊ธฐ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int num, sum = 0;
int status;
printf("Enter an interger (q to quit) : ");
status = scanf("%d", &num); // return value of sanf()
while ( status == 1 ) // equality operator
{
sum = sum + num;
printf("Enter next integer (q to quit) : ");
status = scanf("%d", &num);
}
printf("Sum = %d\n", sum);
return 0;
}
scanf()์ ๋ฐํ๊ฐ์ status๋ก ๋ฐ์ ์ ์๊ฐ ์๋ ๋ฌธ์๋ฅผ ๋ฐ์ผ๋ฉด scanf("%d", &num);์ ๋ฐํ๊ฐ์ด 0์ด๊ธฐ ๋๋ฌธ์
while๋ฌธ์์ ๋๊ฐ๋ค.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int num, sum = 0;
printf("Enter an interger (q to quit) : ");
while ( scanf("%d", &num) == 1 ) // equality operator
{
sum = sum + num;
printf("Enter next integer (q to quit) : ");
}
printf("Sum = %d\n", sum);
return 0;
}
์ค๋ณต๋๋๊ฑธ ์ต๋ํ ์ค์ธ shortcut ์ฐ์ต ๋ง์ด ํด์ ์ต์ํด์ง์
6.2 ์์ฌ์ฝ๋ Pseudocode
int num, sum = 0; Initalize sum to 0
int status;
printf("Enter an interger (q to quit) : "); prompt user
status = scanf("%d", &num); Read input
while ( status == 1 ) // While the input is an integer
{
sum = sum + num; Add the input to sum
printf("Enter next integer (q to quit) : "); Prompt user
status = scanf("%d", &num); Then read next input
}
printf("Sum = %d\n", sum); After input complete, printf sum
์์ ์ฝ๋๋ฅผ Pseudocode๋ก ๋ง๋ ๊ฒ์ด๋ค.
์์ฌ์ฝ๋ Pseudocode ๋
์ปดํจํฐ ํ๋ก๊ทธ๋จ์ด ์ํํด์ผ ํ ๋ด์์ ์ธ๊ฐ์ ์ธ์ด๋ก ๊ฐ๋ตํ๊ฒ ์ค๋ช ํด ๋์ ๊ฒ์ด๋ค.
6.3 ์ง์ ์กฐ๊ฑด ๋ฃจํ while
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
/*
while (expression)
statement
*/
int i;
// 1
i = 1;
while (i < 5) // infinite loop
printf("Hi!\n"); // interation
// 2
i = 1;
while (--i < 5) // wrong direction
printf("Hi!\n");
// 3
i = 1;
while (i < 5)
{
printf("i before = %d\n", i);
i++;
printf("i after = %d\n", i);
}
// 4
i = 10;
while (i++ < 5) // cannot enter
{
(pnitf("Hi\n");
}
// Common mistakes
// 1
i = 0;
while (i < 3) // {} ๋น ์ง
printf("%i\n", i);
i++;
// 2
i = 0;
while (i++ < 3); // ; X
printf("%di\n, i");
// 3
while (scanf("%d", &i) == 1)
; // nul statement
// do something (?)
return 0;
}
6.4 ๊ด๊ณ ์ฐ์ฐ์
< is less than
<= is less than or equal to
== is equal to
>= is greater tha or equal to
> is greater than
!= is not equal to
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h> //fabs()
int main()
{
int n = 0;
while (n++ < 5) // n++ is relational expression
printf("%d", n);
printf("\n");
//char c = 'A';
//while (c != 'Z')
// printf("%c", c++);
const double PI = 3.1415926535897932384626433832795;
double guess = 0.0;
printf("Input PI : ");
scanf("%lf", &guess);
//while (guess != PI)
while (fabs(guess - PI) > 0.01)
{
printf("Fool! Try again.\n");
scanf("%lf", &guess);
}
printf("Good!");
return 0;
}
6.5 ์ฌ์ค๊ณผ ๊ฑฐ์ง
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int tv, fv;
tv = (1 < 2);
fv = (1 > 2);
printf("True is %d\n", tv); // 1
printf("False is %d\n", fv); // 0
printf("\n");
int i = -5;
while (i)
printf("%d is true\n", i++);
printf("%d is false\n", i);
return 0;
}
0์ false ๊ทธ ์ธ์ ๊ฐ๋ค์ ๋ชจ๋ true
6.6 _Bool ์๋ฃํ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
int main()
{
int i;
//i = 5;
//i == 5;
//5 = i; // ์์๋ L-value๊ฐ ๋์ง ๋ชปํ๋ค error
//5 == i;
//while (i = 5) {/*do something*/};
//while (i == 5) {/*do something*/};
_Bool boolean_true = (2 > 1);
_Bool boolean_false = (1 > 2);
printf("True is %d\n", boolean_true);
printf("False is %d\n", boolean_false);
printf(boolean_true ? "true" : "false");
prinf("\n");
printf(boolean_false ? "true" : "false");
bool bt = true;
bool bf = false;
printf("\n");
printf("True is %d\n", bt);
printf("False is %d\n", bf);
return 0;
}
#include <stdbool.h>๋ก ํค๋ ํ์ผ์ ํฌํจํ์ฌ ์๋ฃํ bool ๊ณผ ๊ฐ true, false๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
_Bool์ ๋ด๋ถ์ ์ผ๋ก ์ ์๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ํ์์ง์ ์๋ฅผ ์ ์ํ์ธ %d๋ก ์ฌ์ฉํ๋ค
6.7 ๊ด๊ณ ์ฐ์ฐ์์ ์ฐ์ ์์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int x = 1, y = 2, z;
x > y + 2; // x > (y + 2);
x = y > 2; // x = (y > 2);
z = x > y; // z = (x > y);
x != y == z; // (x != y) == z;
return 0;
}
1. <, >, <=, >=
2. ==, !=
6.8 for ๋ฃจํ ์๊ฐ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Indefinite loop vs. Counting loop
/*
Counting Loop
1.Counter initialization
2. Counter check
3. Counter change
*/
int i;
i = 1;
while (i <= 10) //Note: <=
{
printf("%d ", i);
i++;
}
for (int j = 1; j <= 10; j++)
printf("%d ", j);
return 0;
}
6.9 for๋ ์ ์ฐํด์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
/*
for(initialize; test; update)
statement;
*/
for (int i = 0; i > 0; i--)
prinf("%d ", i);
for (int i = 0; i < 100; i = i + 8)
prinf("%d ", i);
for (char c = 'A'; c <= 'Z'; c++)
prinf("%c ", c);
for (int i = 0; i * i < 10; i++)
prinf("%d ", i);
for (int x = 1, y = 5; y <= 20; y = (++x * 3) + 10)
prinf("%d ", x);
for (double d = 100.0; d < 300; d = d * 1.1)
prinf("%f\n", d);
int i, n;
n = 2;
for (i = 2; n < 10; /* left blank */)
{
n = n * i;
prinf("%d\n", n);
}
for (;;)
prinf("I love you! ");
int = 0;
for (printf("Let's go!\n"); i != 7; scanf("%d", i))
;
return 0;
}
'๋ฐ๋ฐฐ์จ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋ฐฐ์จ 7.2 ~ (0) | 2021.01.15 |
---|---|
๋ฐ๋ฐฐ์จ 6.10 ~ (0) | 2021.01.12 |
๋ฐ๋ฐฐ์จ 5.1 ~ (0) | 2021.01.10 |
๋ฐ๋ฐฐ์จ 4.5 ~ (0) | 2021.01.08 |
๋ฐ๋ฐฐ์จ 4.1 ~ (1) | 2021.01.07 |