By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

switch and continue

P: 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
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
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.