CHAPTER 7
STATEMENTS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
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]


  1. EXPRESSION STATEMENTS
  2. COMPOUND STATEMENTS
  3. SIMPLE DECISIONS---THE IF
  4. MULTIWAY DECISIONS---THE ELSE-IF
  5. MULTIWAY DECISIONS---THE SWITCH
  6. LOOPS
  7. THE NULL STATEMENT
  8. JUMP STATEMENTS
  9. CASE STUDY: OCTAL DUMP

Expressions, Statements, Compound statements or Block

Expression (½Ä) :

Statement --> expression ;

total = 0 ---> = (assignment operator)¸¦ »ç¿ëÇÑ expression.

total = 0; ---> assignment statement


Embedded assignment : (x = 6) + (y = 7)

Multiple assignment : x1 = x2 = x3 = x4 = 0;

Statements

  1. Sequence - ÀÔÃâ·Â¹®, ÁöÁ¤¹®(Assignment statement)
  2. Selection (Choice) - Á¶°Ç¹®, ¼±Åù®
  3. Repetition (Iteration, Loop) - ¹Ýº¹¹®

Compound statement or Block : {¿Í }·Î µÑ·¯ ½ÎÀÎ 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++) ;


Á¶°Ç¹® (Conditional statements)

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;


  1. x°¡ 0ÀÏ °æ¿ì : if (x = 0) ...
  2. x°¡ 2³ª 3ÀÌ ¾Æ´Ò °æ¿ì : if (x != 2 || x != 3) ...
  3. x°¡ 0º¸´Ù Å©°í 10º¸´Ù ÀÛÀ» °æ¿ì : if (0 < x < 10) ...
  4. x°¡ TRUEÀÏ °æ¿ì : if (x == TRUE) ...
  5. if (y == 0) x = TRUE; else x = FALSE; // Redundant



¼±Åù®

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


  1. expr1À» ¼öÇà,
  2. expr2¼öÇà - expr2 °ªÀÌ TRUEÀ̸é loop¹® ¼öÇà, FALSEÀ̸é loop¸¦ ¹þ¾î³²
  3. expr3¼öÇà
  4. 2·Î goto
          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




Assignment #6 (±â°£: 1ÁÖÀÏ)

ÇÁ·Î±×·¥ 1) ÀÓÀÇÀÇ ¾çÀÇ Á¤¼ö¸¦ ÀÔ·Â¹Þ¾Æ ÀÚ¸®¼ö¸¦ °Å²Ù·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.

½ÇÇà ¿¹)

¾çÀÇ Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ. 1234
°á°ú : 4321



ÇÁ·Î±×·¥ 2) 0¿¡¼­ 255 »çÀÌÀÇ Á¤¼ö¸¦ ÀÔ·Â¹Þ¾Æ 1 byte memory¿¡ ÀúÀåÇÏ°í ±× bit patternÀ» forward¿Í reverse·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.

½ÇÇà ¿¹)

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');



[ Table Of Contents | Previous Chapter | Next Chapter]