This chapter covers C's rather small set of statements. We present the fine points of the statements we have already used and describe in detail those we have so far overlooked. While doing so, we improve and extend many of the programs we wrote in earlier chapters, and we write new programs to simulate a simple calculator and to print various breakdowns of input characters. The chapter concludes by tying several topics together in a program that prints an octal dump of its input.
Jump to: [Previous Chapter | Next Chapter]
total = 0 ---> = (assignment operator)¸¦ »ç¿ëÇÑ expression.
total = 0; ---> assignment statement
Embedded assignment : (x = 6) + (y = 7)
Multiple assignment : x1 = x2 = x3 = x4 = 0;
Statements
if (a > b) b++; a--; |
if (a > b) { b++; a--; } |
for (i = 1, sum = 0; i <= 10; sum += i, i++) ; i = -10; while (i++) ;
if ¹® : expressionÀÌ TRUE(!= 0)¸é statement ¼öÇà
if (expression) statement;
if-else ¹® : expressionÀÌ ÂüÀ̸é statement_1 ¾Æ´Ï¸é statement_2 ¼öÇà
if (expression) statement_1; else statement_2;
if (expr) statement_1; staement_2; |
if (expr) statement_1; else statement_2; statement_3; |
if (expr_1) if (expr_2) statement_1; else statement_2; |
if-else if-[else] ¹®
if (expression_1) statement_1; else if (expression_2) statement_2; else if (expression_n) statement_n; else statement;
switch ¹®
switch (expression) { case v1: statements case v2: statements default: statements }
¿¹) cardrank.c
1) while ¹Ýº¹¹® - expressionÀÌ ÂüÀÎ µ¿¾È statement ¹Ýº¹ ¼öÇà
while (expression) statement
2) for ¹Ýº¹¹®
for (expr1; expr2; expr3) statement
expr1; while (expr2) { statement; expr3; }
3) do-while ¹Ýº¹¹® - statement¸¦ ¼öÇàÇÏ°í expressionÀÌ TRUEÀÎ µ¿¾È statement ¹Ýº¹ ¼öÇà
do statement while (expresseion);
Goto LABEL; : LABEL: statement;
½Ç½À) ±¸±¸´Ü Ç¥ Ãâ·Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.
2 3 4 5 6 7 8 9 ----------------------------------- 2 | 4 6 8 3 | 4 | 5 | 6 | 7 | 8 | 9 | 18
½ÇÇà ¿¹)
¾çÀÇ Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ. 1234 °á°ú : 4321
½ÇÇà ¿¹)
0¿¡¼ 255 »çÀÌÀÇ Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ. 10 Forward : 00001010 Reverse : 01010000 0¿¡¼ 255 »çÀÌÀÇ Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ. 200 Forward : 11001000 Reverse : 00010011
Hint> ByteÀÇ bit patternÀÌ bit 7, 6, 5, 4, 3, 2, 1, 0 ¼øÀ¸·Î ±¸¼ºµÇ¾úÀ» °æ¿ì 1 byte memory¸¦ »ç¿ëÇÏ´Â º¯¼ö chÀÇ bit patternÀ» Ãâ·ÂÇÏ·Á¸é
for (i = 0; i < 8; i++) if ((ch & (1 << (7 - i))) != 0) putchar('1'); else putchar('0');