473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 7820
Banfa
9,065 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

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

Similar topics

4
by: Christopher Benson-Manica | last post by:
Given the following snippet, int i; for( i=0 ; i < 10 ; i++ ) { switch( i ) { case 0: case 1: continue; /* NB */ default: /* do something */
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
13
by: xz | last post by:
What if I want the following: vector<intv; // v is loaded by push_back() switch( v.size() ) { case 2: //do something
28
by: Ryan Liu | last post by:
Hi, I have a client/server application, using one thread/client approach. I see very high context switch/sec. What are the ways to reduce it? Each thread sleep longer in its endless loop if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.