0

Possible Duplicate:
How does Duff’s device work?

int n=5;
int q=(n+3)/4;
switch(n%4)
{ 
  case 0:do{ n++;
  case 3:n++;
  case 2:n++;
  case 1:n++;}while(--q>0);
}
 cout<<n;

What will the value of n be? This is just the code snippet and the answer that is given is 10. Cannot see how?

Community
  • 1
  • 1
praxmon
  • 5,009
  • 22
  • 74
  • 121

1 Answers1

1

Final value of n is 10. Before the switch n is 5, and q is 2. Switch goes to case 1. n is incremented 1 time in first iteration, and 4 more times in second. Finally n has the value 5+1+4 = 10.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103