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]