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

If else problem

553 512MB
Basic if statement question:

If ( .... )
{
If (....) // If 1 ***
If (....) // If 2 ***
If (....) // If 3 ***
}
else
if ( .... )
{
if (something = something)
{
if (....) // *** This and the next 2 If statements are identical to those
if (....) // in code above... how would you avoid duplicating the same
if (....) // if statements.. here are only 3 but what if there are 10s?
}
}

One solution to avoid duplicating the the same if statements is to use goto statement, but is there any othe rbetter and preferred way of doing it?


Thanks
Aug 4 '07 #1
10 3326
JonLT
41
Basic if statement question:

If ( .... )
{
If (....) // If 1 ***
If (....) // If 2 ***
If (....) // If 3 ***
}
else
if ( .... )
{
if (something = something)
{
if (....) // *** This and the next 2 If statements are identical to those
if (....) // in code above... how would you avoid duplicating the same
if (....) // if statements.. here are only 3 but what if there are 10s?
}
}

One solution to avoid duplicating the the same if statements is to use goto statement, but is there any othe rbetter and preferred way of doing it?


Thanks
you could use a function:
Expand|Select|Wrap|Line Numbers
  1. bool ifFunction(bool state)
  2. {
  3.    if(...)
  4.       if(...)
  5.          if(...)
  6.             return true;
  7.  
  8.    return false;
  9. }
  10.  
and then use it like this
Expand|Select|Wrap|Line Numbers
  1. If  ( .... ) 
  2. {
  3.    if(ifFunction(...))
  4. }
  5. else 
  6.    if ( .... )
  7.    {
  8.       if (something = something)
  9.       {
  10.          if(ifFunction(...))
  11.       } 
  12.    }
  13.  
Aug 4 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
And you could make that function inline to avoid the overhead of a call.
Aug 4 '07 #3
JosAH
11,448 Expert 8TB
you could use a function:
Expand|Select|Wrap|Line Numbers
  1. bool ifFunction(bool state)
  2. {
  3.    if(...)
  4.       if(...)
  5.          if(...)
  6.             return true;
  7.  
  8.    return false;
  9. }
  10.  
and then use it like this
Expand|Select|Wrap|Line Numbers
  1. If  ( .... ) 
  2. {
  3.    if(ifFunction(...))
  4. }
  5. else 
  6.    if ( .... )
  7.    {
  8.       if (something = something)
  9.       {
  10.          if(ifFunction(...))
  11.       } 
  12.    }
  13.  
How about a little Boolean algebra?

T => (A , B , C)
!T => (D , B , C)

rewriting the terms yields

T => A
!T => D
B
C

this reduces easily to code like this:

Expand|Select|Wrap|Line Numbers
  1. if (T) A;
  2. else D;
  3. B;
  4. C;
  5.  
kind regards,

Jos
Aug 4 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
How about a little Boolean algebra?
Ah yes. DeMorgan's Theorem and Karnaugh Maps.

Check out Introduction to Boolean Algebra and Logic Design by Gerhard Hoernes (McGraw-Hill 1964).
Aug 4 '07 #5
JosAH
11,448 Expert 8TB
Ah yes. DeMorgan's Theorem and Karnaugh Maps.

Check out Introduction to Boolean Algebra and Logic Design by Gerhard Hoernes (McGraw-Hill 1964).
Yes, it's fun isn't it? Have a look at the Quine McCluskey (sp?) algorithm for a
better, less human oriented, less visual approach at boolean logic formula
minimization; those Karnaugh maps are a disaster in that respect.

kind regards,

Jos
Aug 4 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
less visual approach at boolean logic formula
minimization; those Karnaugh maps are a disaster in that respect.
Not if you use Gray Code.
Aug 4 '07 #7
JosAH
11,448 Expert 8TB
Not if you use Gray Code.
Care to elaborate? because I don't understand how, e.g. a 3 variables Karaugh
map (8 cells) would be organized such that a Gray code can come in handy.
It might be just me because I've been lazy all day in my back garden ;-)

kind regards,

Jos (<-- no noticable brain activity to be found)
Aug 4 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
In a Gray code only one bit can change state to advance to the next code in the sequence. There are various forms but here is one:

000
001
011
010
110
111
101
100

By using a Gray code on the toroidal Karanugh map and plotting your true states of your Boolean equation, should any two true states be adjacant then one of the variables is irrelevant. That's because it changed state and did not affect the outcome.

Four adjacent true points mean two irrelevant variables.

And, of course, the open sequares are the inverse.

It's much easier for reduction than using a Vietsch diagram and measuring
Aug 4 '07 #9
JosAH
11,448 Expert 8TB
In a Gray code only one bit can change state to advance to the next code in the sequence. There are various forms but here is one:

000
001
011
010
110
111
101
100

By using a Gray code on the toroidal Karanugh map and plotting your true states of your Boolean equation, should any two true states be adjacant then one of the variables is irrelevant. That's because it changed state and did not affect the outcome.

Four adjacent true points mean two irrelevant variables.

And, of course, the open sequares are the inverse.

It's much easier for reduction than using a Vietsch diagram and measuring
Yes I know what Gray codes are; but still, finding 'adjacent' areas is still a very
visual process; the Q-McK algorithm simply produces tables of the terrms of
disjunctive normal forms and produces a reduced table (if possible) etc. etc.
until no further reduction is possible (it tries to find 'resolutions' just like those
Gray code numbers). The Karnaugh maps make me dizzy above three (four?)
variables; the Q-McK algorithm doesn't care about many variables are used.

Doing that by hand is a boring and error prone process.
I agree Karnaugh maps are more fun to work (play?) with for humans using just
a few variables.

kind regards,

Jos
Aug 4 '07 #10
JamC
8
Getting back to the question
You can use compound expression in if

Expand|Select|Wrap|Line Numbers
  1. Ex..
  2.  
  3. if (age >= 0) 
  4.    if (age < 120)
  5.       print age is valid
  6.  
  7. to
  8.  
  9. if (age >= 0) && (age < 120)
  10.   print age is valid
  11. =========================
  12.  
  13. if (age < 0) || (age >= 120)
  14.   print age is valid
  15.  
  16. to
  17.  
  18. if (age < 0)
  19.    print age is valid
  20. else if (age >= 120)
  21.    peint age is valid
Aug 4 '07 #11

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

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
6
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if...
3
by: Jeroen Ceuppens | last post by:
Hi, I have a problem accesing h, when i want to do h.Run() after the if else structure, i get the following error: (see below for code) C:\Documents and Settings\Eindwerk\Mijn...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
3
by: divya | last post by:
Hiii, Please read the following piece of code:- function SendToWebServer1(sendto) //sendto string contains a URL { if (document.form1.txtbookedby.value == "") { alert('your name field...
1
by: jesmi | last post by:
hi all i have a problem with my code.following is my code: Connection con = null; PreparedStatement stmt = null;
9
by: RP | last post by:
I have following code lines: =============================== if (txtMethod.Text != "D") || (txtMethod.Text != "F")) { txtMethod.Clear(); txtMethod.Focus(); } else {
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:
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
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.