468,504 Members | 2,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

switch and continue

39
Hi.Could any one explain me the following program:

int main()
{
char input[]="SWILTECH1\1\1";
int i,c;
for(i=0;(c=input[i])!='\0';i++)
{
switch(c)
{
case 'a': putchar('i');
case '1': break;
case 1: while((c=input[++i])!='\1' && c!='\0');
case 9: putchar('S');
case 'E':case 'L':continue;
default: putchar(c);continue;
}
putchar(' ');
}
return 0;
}
I could not understand how \1, '1' and 1 are treated here.I thought continue didnot work with switch.But here its working fine.Thanks for any help.

Regards,
Jerico
Sep 18 '06 #1
1 7613
Banfa
9,055 Expert Mod 8TB
well '\1' is the character constant 1, 1 is the value 1 and '1' is the character constant for the digit 1

'\1' == 1
'1' == 49 == ASCII code for 1

The continue is not working with the switch, you will notice that the switch is inside a for loop. The continue is working with that loop.

Expand|Select|Wrap|Line Numbers
  1. for(i=0;(c=input[i])!='\0';i++)
  2. {
  3.   switch(c)
  4.   {
  5.   case 'a': 
  6.     putchar('i');
  7.     // Fall through
  8.   case '1': 
  9.     break;
  10.  
  11.   case 1:
  12.     while((c=input[++i])!='\1' && c!='\0');
  13.     // Fall through
  14.   case 9: 
  15.     putchar('S');
  16.     // Fall through
  17.   case 'E':
  18.   case 'L':
  19.     continue;
  20.  
  21.   default: 
  22.     putchar(c);
  23.     continue;
  24.   }
  25.   putchar(' ');
  26. }
  27.  
The loop iterrates down the string, for each character in the string an action is taken. Taking the special cases first

For 'a' ASCII value 97
output an 'i', ASCII value 105, fall through to the '1' case and break out of the switch statement, output a ' ', space ASCI value 32

For 'a' ASCII value 49
break out of the switch statement, output a ' ', space ASCI value 32

For 1, ASCII value 1
iterate down the string until you find a '\1', ASCII value 1 or '\0', ASCII value 0, fall through to the 9, ASCII value 9 (TAB) case output an 'S', ASCII value 83 fall through to the 'E' and 'L' case, continue onto the next iteration of the loop.

For 9, ASCI value 9 (TAB)
output an 'S', ASCII value 83 fall through to the 'E' and 'L' case, continue onto the next iteration of the loop.

For 'E', ASCII value 69 and 'L' ASCII value 76
continue onto the next iteration of the loop.

For any other character (default)
Output the character and continue onto the next iteration of the loop.



In terms of the code this is very very poor form as it is so hard to read it and understand what it does. I would only pass it in a code review if the author could prove that it had some performance advantage in a critical section of code.
Sep 18 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Christopher Benson-Manica | last post: by
7 posts views Thread by Colin King | last post: by
14 posts views Thread by serrand | last post: by
25 posts views Thread by v4vijayakumar | last post: by
9 posts views Thread by wouter | last post: by
13 posts views Thread by xz | last post: by
28 posts views Thread by Ryan Liu | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.