๐Ÿ’ก

๋•Œ๋ฐฐ์”จ 6.1 ~ ๋ณธ๋ฌธ

๋”ฐ๋ฐฐ์”จ

๋•Œ๋ฐฐ์”จ 6.1 ~

์•„์˜ณ์ด 2021. 1. 11. 19:00

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