Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Tue, 02/26/2013 - 00:00

Workshop

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."

Quiz

    What is the difference between the = operator and the == operator?
    In the x + - y - - z expression, which operator is the subtraction operator, and which one is the unary minus operator?
    Given x = 15 and y = 4, what do the x / y and (float)x / y expressions return, respectively?
    Is the y *= x + 5 expression equivalent to the y = y * x + 5 expression?

Exercises

   1. Given x = 1 and y = 3, write a program to print out the results of these expressions: x += y, x += -y, x -= y, x -= -y, x *= y, and x *= -y.
    Given x = 3 and y = 6, what is the value of z after the expression  z = x * y == 18 is executed?

   2. Write a program that initializes the integer variable x with 1 and outputs results with the following two statements:

    printf("x++ produces:   %d\n", x++);
    printf("Now x contains: %d\n", x);

    Rewrite the program you wrote in exercise

 

3. This time, include the following two statements:

    printf("x = x++ produces: %d\n", x = x++);
    printf("Now x contains:   %d\n", x);

    What do you get after running the executable of the program? Can you explain why you get such a result?
    The following program is supposed to compare the two variables, x and y, for equality. What's wrong with the program? (Hint: Run the program to see what it prints out.)

    #include <stdio.h>

    main()
    {
       int x, y;

       x = y = 0;
       printf("The comparison result is: %d\n",  x = y);
       return 0;
    }

 

Related Items

The goto Statement

The goto Statement

The continue Statement

The continue Statement

The break Statement

The break Statement

You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.

The switch Statement

The switch Statement

Nested if Statements

Nested if Statements

As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.