Tuesday, August 9, 2011

Chapter 4, exercise 4

4.  Write an if statement that assigns the value of x to the variable y only if x is between 1 and 20.  Leave y unchanged if x is not in that range.

(If 'between' means 'inclusive of 1 and 20')

if (x>=1 && x<=20)
  y = x;

(If 'between' means 'exclusive of 1 and 20')

if (x>1 && x<20)
  y = x;

No comments:

Post a Comment