Sams Teach Yourself C in 21 Days

(singke) #1
The Pieces of a C Program: Statements, Expressions, and Operators 91

4



  1. Rewrite the following nested ifstatements using a single ifstatement and logical
    operators.
    if (x < 1)
    if ( x > 10 )
    statement;

  2. To what value do each of the following expressions evaluate?
    a.(1 + 2 3)
    b.10 % 3
    3 - (1 + 2)
    c.((1 + 2) * 3)
    d.(5 == 5)
    e.(x = 5)

  3. If x = 4,y = 6, andz = 2, determine whether each of the following evaluates to
    true or false.
    a.if( x == 4)
    b.if(x != y - z)
    c.if(z = 1)
    d.if(y)

  4. Write an ifstatement that determines whether someone is legally an adult (age
    21), but not a senior citizen (age 65).
    10.BUG BUSTER:Fix the following program so that it runs correctly:
    / a program with problems... /
    #include <stdio.h>
    int x= 1:
    int main( void )
    {
    if( x = 1);
    printf(“ x equals 1” );
    otherwise
    printf(“ x does not equal 1”);
    return 0;
    }


07 448201x-CH04 8/13/02 11:15 AM Page 91

Free download pdf