473,385 Members | 1,780 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.

about " switch statement "

hi,all,
please see following:

int index = 0

switch(index){
case 0 - 10:
break;
default:
break;
}
this can compiler and run;
why?

Nov 16 '05 #1
2 1308
Because it's the same as

int index = 0

switch(index){
case -10:
break;
default:
break;
}

So, the default case is executed as long as index is not -10.
hi,all,
please see following:

int index = 0

switch(index){
case 0 - 10:
break;
default:
break;
}
this can compiler and run;
why?

Nov 16 '05 #2

"iincity" <sk****@hotmail.com> wrote in message
news:cp***********@mail.cn99.com...
hi,all,
please see following:

int index = 0

switch(index){
case 0 - 10:
break;
default:
break;
}
this can compiler and run;
why?


because 0 -10 is a compile time resolvable value(-10). The compiler allows
you to use expressions which can be resolved constantly at compiletime to
simplify code.

Most of the time this would be used for bit shifting (1<<5) for example or
multiplication (1024*1024*256). The compiler will then perform the
arithmetic and use the result as a constant.

Another thing this allows you to do is define constants like:

const int BytesPerK = 1024;
const int KPerMeg =1024;

and then use clearer switch cases like

switch (size)
{
case BytesPerK * KPerMeg * 256:
break;
}
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
9
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
1
by: xiaolim | last post by:
hi as shown in the source, i wanted to return to 'choice' in the 'switch' statement so that user can input their choices again after they chose the option. #include <iostream> #include...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.