CCBet
CCBet

Reputation: 426

Variables assignment

I have a question for an exam and by the way that it sounds, it could be a trap question:

How many assignments will there be executed in the following algorithm if n and p are considered to be integers?

p=1;
n=279;

while (n>=100) { 
  p=p*10;
  n=n-100;
} 

I would say that there are 4 assignments in the while loop and two before the while ... so 6 ? I'm not sure why the question highlights the integer feature of the variables.

Upvotes: 0

Views: 85

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18813

I'd say 6 because the declaration seems to be somewhere else -- it's not

int p=1, n=279; 

Ok, even in that case I'd lean towards 6 because the declarations imply assignments (and this thread seems to agree: Difference between declaration statement and assignment statement in C? ), but this is fortunately not the question here.

Upvotes: 1

Related Questions