Control Constructs


  1. What is wrong with the following?
    
    int x;
    if (x=1)
    	x++;
    

  2. What is wrong with the following?
    
    int x, y, z;
    
    if (x > y && < z)
    	x = y;
    

  3. Write an extract of code to test if the variable x is a positive number?

  4. Write an extract to test if the variable x is a positive and even number?


Answers





































































  1. The test should be x== 1, with x=1 this will assign the value of 1 to x rather than cecking if x is a 1.

  2. Assuming that it is intended to compare z to z, the test should be x > y && x < z

  3.  if (x > 0)
    

  4.  if (x > 0 && x%2 == 0)
    


Back to Questions Back to Main Page for Lecture 3