Created
January 14, 2013 06:36
-
-
Save mysteriouspants/4528184 to your computer and use it in GitHub Desktop.
Just some rubbish to prove the duff's loops work for n>0, when you get the do{}while condition right. I didn't in a blog post, so I investigated. And that's what I found. About to delete the file from my hard drive, but I wanted to paste the code somewhere for safe keeping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cstdlib> | |
| int main(int args, const char* argv[]) { | |
| register size_t n, | |
| i=0, | |
| j=0; | |
| if (args == 2) { | |
| n = atoi(argv[1]); | |
| } else { | |
| n = 11; | |
| } | |
| switch(n %10) { | |
| case 0: do { | |
| ++j; | |
| case 9: ++j; | |
| case 8: ++j; | |
| case 7: ++j; | |
| case 6: ++j; | |
| case 5: ++j; | |
| case 4: ++j; | |
| case 3: ++j; | |
| case 2: ++j; | |
| case 1: ++j; | |
| } while (i++ < n/10); | |
| } | |
| std::cout << "n: " << n << std::endl | |
| << "j: " << j << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment