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

Is there any limit on the number of conditions to be added in an if stmt

Hi,

I have situation in C++ where in I'll have to add around 20-25 conditions <compare a variable against a number> to an if statement.
Is there any limit on the number of expressions/conditions to be added to an if statement.

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++
sample code.
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++

x = 10;

if ( (x == 20) || (x == 22) || ) //I have around 20 comparisions here
{
//do something
}

if ( (x == 21) || (x == 22) )//I have around 20 comparisions here
{
//do something else
}
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++

Though the value of the 'x' is not any of the constants i am comparing with the stmts in all the if blocks are getting executed. i checked and re-checked the values to make sure that it is different but code isn't behaving as expected.

Can somebody help me.

Note that I can't use the switch the conditions as the constants in the if conditions are not mutually exclusive.

Thanks in advance
Prashanth
Sep 15 '09 #1
4 2263
JosAH
11,448 Expert 8TB
@Prashanth Kumar B R
By definition they are mutually exclusive; have a look at Karnaugh maps; you can remodel your conditions such that you end up with a disjoint set of integer numbers that can be used in switch statements; but what I find more interesting: why are there so many conditions? Do you end up with less conditions if you consider the complement of the conditional expression?

kind regards,

Jos
Sep 15 '09 #2
donbock
2,426 Expert 2GB
@Prashanth Kumar B R
Your conditions are the logical-or of a bunch of subexpressions. The compiler will test each subexpression until it finds one that is true. It is normal for all subexpressions to be tested when they are all false.
(For the logical-and of a bunch of subexpressions, the compiler will test each subexpression until it finds one that is false.)

@JosAH
He has a sequence of if statements, not else if statements. Each value of x will execute a different combination of then-blocks.


Perhaps the code would be more concise if you moved each then-block into a separate subroutine and then used a switch statement. Each case would call one or more of those subroutines, corresponding to then-blocks you want activated for that value. This would make it easier to see at a glance what action is taken for each value of x.
Sep 15 '09 #3
newb16
687 512MB
@donbock
I understood it as his problem is that the body of 'if' is executed though condition seems to be false. ("Though the value of the 'x' is not any of the constants i am comparing with...")
Sep 15 '09 #4
donbock
2,426 Expert 2GB
@newb16
Then my guess is that somewhere among all those if statements he has one equals sign instead of two. Not only would that change the value of x, altering the path through subsequent if statements, but assignment to any nonzero value is considered true, causing execution of the then-block associated with that if.
Sep 15 '09 #5

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

Similar topics

4
by: MFA | last post by:
Hi All Is there any limit to include files (<!--#include file="xyz.asp"-->). I have developed an application where I am including different files on the base of diffirent conditions and values...
2
by: claus.hirth | last post by:
I wrote a stored procedure that uses a prepared INSERT INTO statement in order to play with the PREPARE and EXECUTE keywords. In transcript 1 below the call to that stored procedure does not...
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
11
by: VB Programmer | last post by:
Is there a limit as to how big an aspx page's html can be? (number of characters) Why do I ask? I have a webform with a lot of ActiveX controls (shows up as OBJECT in html). When I place like...
2
by: bruno | last post by:
I'm trying to dynamicaly change Where conditions in a Datagrid without success. The aspx form has some input fields, used to build the search, plus a final SEARCH button. In the click event I build...
1
by: Franc Walter | last post by:
Hello, i didn't find it in the help, i think it is not possible, but i try to question anyway: Is the MySQL command "LIMIT" possible with a subquery in MySQL 4.0.27? e.g.: SELECT * FROM tab1...
2
by: jeanbdenis | last post by:
Hi Folks, I have been struggling with this issues for the last couple of days. I have a java application which does an update to the database every 5 mins. The data written to the database...
5
by: Yash | last post by:
Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... WHERE <conditions>
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.