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

Adding More Expressions into for

Adding More Expressions into for

The C language allows you to put more expressions into the three expression fields in the for statement. Expressions in a single expression field are separated by commas.

The Null Statement

The Null Statement

Looping Under the for Statement

Looping Under the for Statement

The general form of the for statement is

for (expression1; expression2; expression3) {
   statement1;
   statement2;
   .
   .
   .
}

Using Nested Loops

Using Nested Loops

You can put a loop inside another one to make nested loops. The computer will run the inner loop first before it resumes the looping for the outer loop.

Listing 7.7 is an example of how nested loops work.

 

The do-while Loop

The do-while Loop

You may note that in the for and while statements, the expressions are set at the top of the loop. However, in this section, you're going to see another statement used for looping,