Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

empty "if" statement - it doesn't look nice, does it?

0 views
Skip to first unread message

Tomasz Chmielewski

unread,
Aug 11, 2008, 5:19:06 AM8/11/08
to
Although an empty "if" statement is technically OK in Perl, it doesn't
look very nice (and in some languages is even forbidden).

Let's suppose we have a code like:

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


What would be a good way to get rid of this empty "if" statement?
Or is it OK to have it?


--
Tomasz Chmielewski
http://wpkg.org

Peter Makholm

unread,
Aug 11, 2008, 5:24:01 AM8/11/08
to
Tomasz Chmielewski <t...@nospam.wpkg.org> writes:

> What would be a good way to get rid of this empty "if" statement?

Eiter negate the condition or use an "unless" statement instead.

//Makholm

Tomasz Chmielewski

unread,
Aug 11, 2008, 5:36:43 AM8/11/08
to
Peter Makholm schrieb:

> Tomasz Chmielewski <t...@nospam.wpkg.org> writes:
>
>> What would be a good way to get rid of this empty "if" statement?
>
> Eiter negate the condition or use an "unless" statement instead.

Negating it or using "unless" would yield an unwanted result (but I
didn't explain it very precisely, sorry for that).
We want to print the text only if neither $a nor $b were equal a given
number.


Example 1 - does "nothing" (uses empty "if"):

my $a = 1;
my $b = 2;

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


Example 2 - negated, but the result is wrong:

my $a = 0;
my $b = 2;

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


Text was printed, although $b was equal 2. Not what we wanted.

Using "next" or "last" in the empty statement would break the things if
this "if" was inside of yet another "if".
"goto" function? Doesn't look much better than an empty "if".

Tomasz Chmielewski

unread,
Aug 11, 2008, 5:43:08 AM8/11/08
to
Peter Makholm schrieb:

Negating it or using "unless" would yield an unwanted result (but I

didn't explain it very precisely, sorry for that).
We want to print the text only if neither $a nor $b were equal a given
number.


Example 1 - does "nothing" (uses empty "if"):

my $a = 1;
my $b = 2;

if ($a == 1 && $b == 2) {


# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}

Example 2 - negated, but the result is wrong:

my $a = 0;
my $b = 2;

if ($a != 1 && $b != 2) {


# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}

Text was printed, although $b was equal 2. Not what we wanted.

Using "next" or "last" in the empty statement would break the things if
this "if" was inside of yet another "if".
"goto" function? Doesn't look much better than an empty "if".

Peter Makholm

unread,
Aug 11, 2008, 5:46:09 AM8/11/08
to
Tomasz Chmielewski <t...@nospam.wpkg.org> writes:

> Example 2 - negated, but the result is wrong:

Isn't this using the same condition as you original post?

The negated condition would be 2!( $a == 1 && $b == 2)" or by applying
De Morgans Law: "$a != 1 || $b != 2"

> my $a = 0;
> my $b = 2;
>
> if ($a == 1 && $b == 2) {
> # Essentially an empty "if" statement
> } else {
> print "We're here only if a is not 1 and b is not 2!\n";
> }
>
>
> Text was printed, although $b was equal 2. Not what we wanted.

I think you understanding of boolean logic is flawed. The and operator
means that both påarts should be true, which they clearly are not. So
you code does whats expected.

> Using "next" or "last" in the empty statement would break the things
> if this "if" was inside of yet another "if".
> "goto" function? Doesn't look much better than an empty "if".

Using next or last only makes sens if you're dealing with loop
constructs.

//Makholm

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:03:19 AM8/11/08
to
Peter Makholm schrieb:

> Tomasz Chmielewski <t...@nospam.wpkg.org> writes:
>
>> Example 2 - negated, but the result is wrong:
>
> Isn't this using the same condition as you original post?

Yes, I cancelled the post, and sent a proper condition, too late it seems.


> The negated condition would be 2!( $a == 1 && $b == 2)"

if !( $a == 1 && $b == 2)

?
Doesn't look correct to me?


> or by applying
> De Morgans Law: "$a != 1 || $b != 2"

No, it wouldn't work in a desired way.


>> my $a = 0;
>> my $b = 2;
>>
>> if ($a == 1 && $b == 2) {
>> # Essentially an empty "if" statement
>> } else {
>> print "We're here only if a is not 1 and b is not 2!\n";
>> }
>>
>>
>> Text was printed, although $b was equal 2. Not what we wanted.
>
> I think you understanding of boolean logic is flawed. The and operator
> means that both påarts should be true, which they clearly are not. So
> you code does whats expected.

I know such does logically what it's supposed to do.
What I meant: negating the "if ($a == 1 && $b == 2)" wouldn't make what
I want.

Peter Makholm

unread,
Aug 11, 2008, 6:08:56 AM8/11/08
to
Tomasz Chmielewski <t...@nospam.wpkg.org> writes:

> I know such does logically what it's supposed to do.
> What I meant: negating the "if ($a == 1 && $b == 2)" wouldn't make
> what I want.

so you say that

if (condition) {
# no-op
} else {
do_something();
}

does what you intend to do, but neither

if (!condition) {
do_something();
}

nor

unless (condition) {
do_something();
}

works for you?

//Makholm

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:11:34 AM8/11/08
to
Peter Makholm schrieb:

I'm saying that I have two conditions ;)

I want to do_something() only if none of both conditions is true.

Peter Makholm

unread,
Aug 11, 2008, 6:17:47 AM8/11/08
to
Tomasz Chmielewski <t...@nospam.wpkg.org> writes:

> I'm saying that I have two conditions ;)
>
> I want to do_something() only if none of both conditions is true.

So you initial code didn't work? That should print something if just
on of the equalities was false.

So what you want is:

if (!cond1 && !cond2) { doit(); }

or

if ($a != 1 && $b != 2) { doit(); }

or

unless (cond1 && cond2) { doit(); }

//Makholm

RedGrittyBrick

unread,
Aug 11, 2008, 6:22:17 AM8/11/08
to

Sigh!

$ perl t5.pl
Ugly method
a=1, b=2 :
a=1, b=9 :condition met
a=9, b=2 :condition met
a=9, b=9 :condition met

Less Ugly method
a=1, b=2 :
a=1, b=9 :condition met
a=9, b=2 :condition met
a=9, b=9 :condition met


$ cat t5.pl
#!/usr/bin/perl
use strict;
use warnings;

print "Ugly method\n";
foo(1,2);
foo(1,9);
foo(9,2);
foo(9,9);

print "\nLess Ugly method\n";
bar(1,2);
bar(1,9);
bar(9,2);
bar(9,9);

sub foo {
my($a,$b) = @_;
print "a=$a, b=$b :";

if ($a == 1 && $b == 2) {
# ugly
} else {
print "condition met";
}

print "\n";
}

sub bar {
my($a,$b) = @_;
print "a=$a, b=$b :";

if ($a != 1 || $b != 2) {
print "condition met";
}

print "\n";

}


--
RGB

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:36:22 AM8/11/08
to
RedGrittyBrick schrieb:
> Tomasz Chmielewski wrote:

(...)

>> I'm saying that I have two conditions ;)
>>
>> I want to do_something() only if none of both conditions is true.
>>
>>
>
> Sigh!

(...)

> sub bar {
> my($a,$b) = @_;
> print "a=$a, b=$b :";
>
> if ($a != 1 || $b != 2) {
> print "condition met";
> }
>
> print "\n";
>
> }

Ha ha, yeah, just don't use

RedGrittyBrick

unread,
Aug 11, 2008, 6:38:08 AM8/11/08
to
Tomasz Chmielewski wrote:

TC> if ($a == 1 && $b == 2) {
TC> # Essentially an empty "if" statement
TC> } else {
TC> print "We're here only if a is not 1 and b is not 2!\n";
TC> }

... [negation suggested]

TC2> Text was printed, although $b was equal 2. Not what we wanted.

Try your original expression with a=9 and b=2!


Peter Makholm schrieb:

PM> The negated condition would be 2!( $a == 1 && $b == 2)"
PM> or by applying
PM> De Morgans Law: "$a != 1 || $b != 2"

TC> No, it wouldn't work in a desired way.

Probably because your original expression doesn't do what you want
either!

--
RGB

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:39:03 AM8/11/08
to
RedGrittyBrick schrieb:

(...)

>> I'm saying that I have two conditions ;)
>>
>> I want to do_something() only if none of both conditions is true.
>>
>>
>
> Sigh!

(...)

> sub bar {
> my($a,$b) = @_;
> print "a=$a, b=$b :";
>
> if ($a != 1 || $b != 2) {
> print "condition met";
> }
>
> print "\n";
>
> }

Ha ha, yeah, am I dumb or blind today? I suppose so.
Thanks, Peter and RGB.

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:43:35 AM8/11/08
to
RedGrittyBrick schrieb:

> Tomasz Chmielewski wrote:
>
> TC> if ($a == 1 && $b == 2) {
> TC> # Essentially an empty "if" statement
> TC> } else {
> TC> print "We're here only if a is not 1 and b is not 2!\n";
> TC> }
>
> .... [negation suggested]

>
> TC2> Text was printed, although $b was equal 2. Not what we wanted.
>
> Try your original expression with a=9 and b=2!

It will print the message - the result I want.


> Peter Makholm schrieb:
>
> PM> The negated condition would be 2!( $a == 1 && $b == 2)"
> PM> or by applying
> PM> De Morgans Law: "$a != 1 || $b != 2"
>
> TC> No, it wouldn't work in a desired way.
>
> Probably because your original expression doesn't do what you want
> either!

It does.
It only uses the "empty if" when both "a is 1" and "b is 2".

RedGrittyBrick

unread,
Aug 11, 2008, 6:44:13 AM8/11/08
to

Your code doesn't match your comments. Whether your code is wrong or
your comments are wrong - only you can say. If your code is wrong then
correct it before applying negation.

Perhaps when you wrote

if (a == 1 && b == 2)

you actually meant

if (a != 1 && b != 2)

in which case the negation would be

if !(a != 1 && b != 2)

or

if (a == 1 || b == 2)


Either way, if you still don't understand, write a small program (as I
did) that illustrates your question.

--
RGB

Tomasz Chmielewski

unread,
Aug 11, 2008, 6:49:09 AM8/11/08
to
RedGrittyBrick schrieb:
> Tomasz Chmielewski wrote:
>> RedGrittyBrick schrieb:
>>> Tomasz Chmielewski wrote:
>>
>> (...)
>>
>>>> I'm saying that I have two conditions ;)
>>>>
>>>> I want to do_something() only if none of both conditions is true.
>>>>
>>>>
>>>
>>> Sigh!
>>
>> (...)
>>
>>> sub bar {
>>> my($a,$b) = @_;
>>> print "a=$a, b=$b :";
>>>
>>> if ($a != 1 || $b != 2) {
>>> print "condition met";
>>> }
>>>
>>> print "\n";
>>>
>>> }
>>
>> Ha ha, yeah, just don't use

My grumpy fingers again.
Was to be 'ha ha, yeah, just don't use "else" at all'.

Sorry for not clear comments and thanks for help.

Lars Eighner

unread,
Aug 11, 2008, 7:14:36 AM8/11/08
to
In our last episode, <g7p17c$3vk$1...@online.de>, the lovely and talented
Tomasz Chmielewski broadcast on comp.lang.perl.misc:

> Peter Makholm schrieb:
>> Tomasz Chmielewski <t...@nospam.wpkg.org> writes:
>>
>>> What would be a good way to get rid of this empty "if" statement?
>>
>> Eiter negate the condition or use an "unless" statement instead.

> Negating it or using "unless" would yield an unwanted result (but I
> didn't explain it very precisely, sorry for that).
> We want to print the text only if neither $a nor $b were equal a given
> number.

Then why didn't you write that:

if ( $a != 1 && $b != 2 ){print your stuff here, no need for an else}

Or if you must, express this as !( $a == 1 || $b == 2).

That isn't what your orginal amounted to. Else was reached if
either $a or $b were not equal to particular values.

> Example 1 - does "nothing" (uses empty "if"):

> my $a = 1;
> my $b = 2;

> if ($a == 1 && $b == 2) {
> # Essentially an empty "if" statement
> } else {
> print "We're here only if a is not 1 and b is not 2!\n";
> }

No. you get there if either a is not 1 or b is not 2.

Even if you know no logic, running this script should have
told you your conclusion was wrong. This is why people general
ask for actual code from a script that you are trying to run,
not a theoretical example.

> Example 2 - negated, but the result is wrong:

> my $a = 0;
> my $b = 2;

> if ($a == 1 && $b == 2) {
> # Essentially an empty "if" statement
> } else {
> print "We're here only if a is not 1 and b is not 2!\n";
> }

No. You get there any way because $a is not 1.

> Text was printed, although $b was equal 2. Not what we wanted.

> Using "next" or "last" in the empty statement would break the things if
> this "if" was inside of yet another "if".
> "goto" function? Doesn't look much better than an empty "if".


--
Lars Eighner <http://larseighner.com/> use...@larseighner.com
Cole's Law: Thinly sliced cabbage.

Lars Eighner

unread,
Aug 11, 2008, 7:30:18 AM8/11/08
to
In our last episode, <g7p38m$rbo$3...@online.de>, the lovely and talented

Tomasz Chmielewski broadcast on comp.lang.perl.misc:

> Peter Makholm schrieb:

So if p is one condition
and q is the other

neither true is: ~p and ~q
this is exactly the same as: ~(p or q)

So

if ( $a != 1 && $b != 2){ this gets done}

and

if ( !( $a == 1 || $b == 2 )){ this gets done}

are exactly the same. And even if you cannot understand the simplest
logic, you can write a short test script to show you that it is so.

"We have no opinion on your Arab - Arab conflicts, such as your dispute with
Kuwait." -- Bush's Ambassador April Glaspie, giving Saddam Hussein
the greenlight to invade Kuwait.

Charlton Wilbur

unread,
Aug 11, 2008, 12:07:15 PM8/11/08
to
>>>>> "TC" == Tomasz Chmielewski <t...@nospam.wpkg.org> writes:

TC> I'm saying that I have two conditions ;)

The negation of A && B is !A || !B, or, !(A && B).

The negation of A || B is !A && !B, or, !(A || B).

Charlton


--
Charlton Wilbur
cwi...@chromatico.net

RedGrittyBrick

unread,
Aug 11, 2008, 12:25:02 PM8/11/08
to
Charlton Wilbur wrote:
>>>>>> "TC" == Tomasz Chmielewski <t...@nospam.wpkg.org> writes:
>
> TC> I'm saying that I have two conditions ;)
>
> The negation of A && B is !A || !B, or, !(A && B).
>
> The negation of A || B is !A && !B, or, !(A || B).

As an addendum:
Peter Makholm mentioned De Morgan's laws.
http://en.wikipedia.org/wiki/DeMorgan%27s_Laws

--
RGB

xho...@gmail.com

unread,
Aug 11, 2008, 12:40:59 PM8/11/08
to

It's OK with me, especially if it only temporarily empty and something
is likely to go there in a future modification of the code. Or even if
it is permanently empty but might contain a useful comment.

if ($aldjflajdsf>98 && $lkjadf<10987) {
## This means the frobnitz isn't compatible with the dohickey
} else {
print "blah blah blah"
}

Although it would usually be better to arrange your variable names and
value such that they are more self-explanatory.

Xho
Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

s...@netherlands.com

unread,
Aug 11, 2008, 4:19:13 PM8/11/08
to

I didn't read the rest of the reply's but I'm sure somebody said this:

> print "We're here only if a is not 1 and b is not 2!\n";

Your here if "a is not 1" OR "b is not 2". Not AND.

Essentially:

if (!($a == 1 && $b == 2))
{
print "We're here only if a is not 1 OR b is not 2!\n";
}

Which, if you "boolean negate" all things in parenthesis (multiply by -1), can be reduced to:

if ($a != 1 || $b != 2)
{

print "We're here only if a is not 1 OR b is not 2!\n";
}


Conclusion, there is no LOGICAL reason to have an empty block just to negate an if statement.
ALL statements can be resolved using BOOLEAN logic, all..

However, as a long time C++ programmer, I can tell you that I do this all the time.
Why?

As a placeholder if the condition were to someday be true.

However the reality is that

this notation: if (!($a == 1 && $b == 2))
is preffered
over this: if ($a != 1 || $b != 2)

because it separates the classes into distinct "what is" and "what isn't",
which is what you wan't.


sln

s...@netherlands.com

unread,
Aug 11, 2008, 5:03:35 PM8/11/08
to

Following up on my own post..
Ok, I just read the other posts.

I am not saying you don't have the 'logic' portion of your brain
when I make this recomendation. Instead, I'm offering a suggestion on how
to develop it.

Its a natural ability to resolve boolean expressions.
Programmers, over time develop this naturally.
Realistaclly, probably %40 of initial codeing errors, and
%20 of remaining errors after fixes, are attributed to errors
in logic, boolean errors.

I call them "conceptual" errors. They are what testers are for.

The people who are very good in boolean logic, can read code
as fast as people read books. They are well paid.

Reading the boolean logic in code is the first line of error detection
when debugging large code. It scrapes off the first layer.
Its re-read for deeper "conceptual" errors, design errors.
By that time, all the flaws are known. The fix is harder.
Well paid people.

If you were good in math since childhood, say intermediate algebra,
could almost do it in your head, you will probably be a good programmer
if you study.

So my suggestion is that if you have access to college, you take a course
called "Formal Symbolic Logic 101". It's all about Boolean symbolism,
sort of Electrical Engineering'ish. In essence, it takes the words, sentences,
paragraphs of .. debate speaches, for instance... or news articles, or whatever..
And formulates BOOLEAN equations to represent them. The equations are then resolved,
ie: reduced, to test the validity of the representation. Its TRUE or FALSE, and why.
Although there are many more constructs of logic in language (human language), that
comprise the conditions, in that context there are many more gradations of fallicy
than can be incorporated into computer language. As such, there are logic laws,
rules, falicy's, that can be easily identified.

The big thing in "Formal Symbolic Logic" is that logic is incorporated to interpret
language. And guess what? The very same formal logic symbolism laws are used in CS and EE and ME
and all the other sciences.

Its an elective in Junior College, take it!


sln


s...@netherlands.com

unread,
Aug 11, 2008, 5:14:53 PM8/11/08
to
On Mon, 11 Aug 2008 21:03:35 GMT, s...@netherlands.com wrote:

>On Mon, 11 Aug 2008 20:19:13 GMT, s...@netherlands.com wrote:
>

<snip>

Also might help if there are unary expressions like this:

$var &= ~($badval << $bits);

Jürgen Exner

unread,
Aug 14, 2008, 10:23:43 AM8/14/08
to
Tomasz Chmielewski <t...@nospam.wpkg.org> wrote:
>Peter Makholm schrieb:

>> The negated condition would be 2!( $a == 1 && $b == 2)"


>
>if !( $a == 1 && $b == 2)
>

>Doesn't look correct to me?

Right, it's a syntax error. You need to write

if (!($a == 1 && $b == 2))

>> or by applying
>> De Morgans Law: "$a != 1 || $b != 2"
>
>No, it wouldn't work in a desired way.

Nonsense, of couse it does.. Have you ever had even basic teaching in
Boolean logic?

>What I meant: negating the "if ($a == 1 && $b == 2)" wouldn't make what
>I want.

Obviously you have no clue whatsoever. Negating the condition of an
if-statement will swap the 'then' and 'else' parts without causing any
semantic difference.
Maybe that is not what you want, but then maybe you should restate your
goal because by all sensible interpretations of you question that is
exactly what you were asking for.

jue

0 new messages