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

T

Tomasz Chmielewski

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?
 
P

Peter Makholm

Tomasz Chmielewski said:
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
 
T

Tomasz Chmielewski

Peter said:
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".
 
T

Tomasz Chmielewski

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

//Makholm

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".
 
P

Peter Makholm

Tomasz Chmielewski said:
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
 
T

Tomasz Chmielewski

Peter said:
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.

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.
 
P

Peter Makholm

Tomasz Chmielewski said:
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
 
T

Tomasz Chmielewski

Peter said:
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?

I'm saying that I have two conditions ;)

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

Peter Makholm

Tomasz Chmielewski said:
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
 
R

RedGrittyBrick

Tomasz said:
I'm saying that I have two conditions ;)

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

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";

}
 
T

Tomasz Chmielewski

RedGrittyBrick said:
Tomasz Chmielewski wrote:
(...)


(...)

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
 
R

RedGrittyBrick

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!
 
T

Tomasz Chmielewski

RedGrittyBrick schrieb:

(...)
(...)

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.
 
T

Tomasz Chmielewski

RedGrittyBrick said:
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".
 
R

RedGrittyBrick

Tomasz said:
Ha ha, yeah, just don't use

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.
 
T

Tomasz Chmielewski

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.
 
L

Lars Eighner

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.
 
L

Lars Eighner

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

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.
 
C

Charlton Wilbur

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top