Control Constructs
What is wrong with the following?
int x; if (x=1) x++;
What is wrong with the following?
int x, y, z; if (x > y && < z) x = y;
Write an extract of code to test if the variable
x
is a positive number?
Write an extract to test if the variable
x
is a positive and even number?
Answers
The test should be
x== 1
, with
x=1
this will assign the value of 1 to x rather than checking if
x
is a 1.
Assuming that it is intended to compare z to z, the test should be
x > y && x < z
if (x > 0)
if (x > 0 && x%2 == 0)
Back to Questions
Back to Main Page for Lecture 3