Found something weird in old code.

In some of my old code, I found a function for converting unsigned integers to roman numerals. It contains some rather ... unorthodox control flow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const char * utoroman(unsigned int i){
	static char s[16];
	if(i>=4000)return "Magnus";
	if(i==0)return "Nihil";
	char *t=s+15;
	int m=0;
	do{
		switch(i%10){
		case 1 ... 3:
		case 6 ... 8:
			*--t="IXCM"[m];
			i--;
			continue;
		case 5:	*--t="VLD"[m];
			break;
		case 4:	*--t="VLD"[m];
		if(0)
		case 9:	*--t="XCM"[m];
			*--t="IXCM"[m];
		case 0: break;
		}
		m++;
		i/=10;
	}while(i>0);
	return t;
}

The if statement on line 17 skips the following statement only when i%10 is 4, even though it is attached to that statement. Is that legal?
I think it is legal. Case 4 doesn't have a break statement, meaning it will also execute the code for case 9. The original coder (I suppose you), wants this but wants to skip the first line of case 9. The if accomplishes just that.

If i%10 == 9, code execution jumps to the case 9 line directly skipping the if(0), meaning both lines are executed. It is clever, if you ask me.
Topic archived. No new replies allowed.