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
10 3326
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: -
bool ifFunction(bool state)
-
{
-
if(...)
-
if(...)
-
if(...)
-
return true;
-
-
return false;
-
}
-
and then use it like this -
If ( .... )
-
{
-
if(ifFunction(...))
-
}
-
else
-
if ( .... )
-
{
-
if (something = something)
-
{
-
if(ifFunction(...))
-
}
-
}
-
And you could make that function inline to avoid the overhead of a call.
you could use a function: -
bool ifFunction(bool state)
-
{
-
if(...)
-
if(...)
-
if(...)
-
return true;
-
-
return false;
-
}
-
and then use it like this -
If ( .... )
-
{
-
if(ifFunction(...))
-
}
-
else
-
if ( .... )
-
{
-
if (something = something)
-
{
-
if(ifFunction(...))
-
}
-
}
-
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:
kind regards,
Jos
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).
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
less visual approach at boolean logic formula
minimization; those Karnaugh maps are a disaster in that respect.
Not if you use Gray Code.
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)
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
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
Getting back to the question
You can use compound expression in if - Ex..
-
-
if (age >= 0)
-
if (age < 120)
-
print age is valid
-
-
to
-
-
if (age >= 0) && (age < 120)
-
print age is valid
-
=========================
-
-
if (age < 0) || (age >= 120)
-
print age is valid
-
-
to
-
-
if (age < 0)
-
print age is valid
-
else if (age >= 120)
-
peint age is valid
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 -...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: jesmi |
last post by:
hi all
i have a problem with my code.following is my code:
Connection con = null;
PreparedStatement stmt = null;
|
by: RP |
last post by:
I have following code lines:
===============================
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear();
txtMethod.Focus();
}
else
{
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: 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,...
|
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...
|
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...
| |