473,394 Members | 1,889 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,394 software developers and data experts.

why does the switch construct does'nt allows negative values to be used inside a case

I just wanted to ask the reason that why is the below code not checking the case -1 while working for the other case values.
#include<stdio.h>
#include<conio.h>
int main()
{
int i=-1;
switch(i-2)
{
case -1:printf("feeling fish");
break;
case 0:printf("heloo");
case 1:printf("mend ur ways");
default:printf("u r crect");

}
getch();
return 0;
}

So why in the below code the case -1 doesnt run,when the value evaluated by the switch construct is actually a negative integer constant.
Feb 26 '15 #1
1 10120
weaknessforcats
9,208 Expert Mod 8TB
The switch does work on negative values:

Expand|Select|Wrap|Line Numbers
  1. switch (-1)
  2. {
  3.    case -1:
  4.      etc...
  5. }
However:

Expand|Select|Wrap|Line Numbers
  1. int i = -1;
  2. switch (i-2)
  3. {
  4.     case -1:
  5.      etc...
won't go to case -1 because i-2 is not -1. i-2 is an expression which can be only true or false. You need the negative value to be in an integer variable.
Feb 26 '15 #2

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

Similar topics

2
by: Dirk Hagemann | last post by:
Hi, Is there a datatype in python which allows no negative values? I subtract several times something from a value and I don't want to chek everytime if this value is still bigger or equal 0. ...
2
by: luke | last post by:
Could anyone, please, explain to me why I have negative values in RowModCtr column in sysobjects table? I have tested that after I update statistics the RowModCtr column is reset to 0. But why do I...
12
by: deko | last post by:
Is there an easy way to make all negative values positive in a particular table? I've been experimenting with this: Dim rst As DAO.Recordset Set rst = db.OpenRecordset("tblNegValues") Do...
0
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET...
11
by: Alan Silver | last post by:
Hello, I am using this validator on a textbox, and have discovered that if I set the InitialValue property, then the validator correctly fires if the user does not change the initial value of...
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
1
by: Badis | last post by:
Hi guys, I have a negative values in my Amount column in GridView could U tell me how to display them with no (-) sign in front, knowing that my gridview is bounded to a DataView. Thanks
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
5
by: JessSmith | last post by:
Hi I have a query which has fields in showing how close a client is to their renewal date, or how overdue they are. I wanted to have a totals column at the bottom of the query showing what...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.