5

What does the syntax

if (1) {}

do?

I can't find documentation for this syntax and 1 is not being treated as a boolean. Am I right?.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
user2521358
  • 279
  • 1
  • 3
  • 11

9 Answers9

20

Technically, the if (1) does nothing interesting, since it's an always-executed if-statement.

One common use of an if (1) though is to wrap code you'd like to be able to quickly disable, say for debugging purposes. You can quickly change an if (1) to if (0) to disable the code.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
7

Yes, 1 is treated as true, so the block after if (1) is always executed.

From perldoc

The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context. All other values are true.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
4

1 is indeed being treated as a boolean value in this case, which always evaluates to true.

This is basically an if statement which always passes (making it unnecessary in the first place).

David
  • 208,112
  • 36
  • 198
  • 279
2

It runs the code block. It's equivalent to:

{
  ...
}

Added by ikegami:

No, it's not.

{ ... } is a "bare loop", a loop that executes only once. It is affected by next, last and redo.

if (1) { ... } is not a loop. It is not affected by next, last and redo.

>perl -E"for (1..3) { say $_; if (1) { next; } say $_; }"
1
2
3

>perl -E"for (1..3) { say $_; { next; } say $_; }"
1
1
2
2
3
3

However, they are similar in that both create a lexical scope.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Oesor
  • 6,632
  • 2
  • 29
  • 56
2

When in doubt, use Deparse:

$ perl -MO=Deparse -e'if (1) {}'
do {
    ()
};
-e syntax OK
shawnhcorey
  • 3,545
  • 1
  • 15
  • 17
1

Looks like 1 is being treated as a boolean true by virtue of the fact that the if doesn't evaluate to false. See How do I use boolean variables in Perl?

Community
  • 1
  • 1
David
  • 393
  • 4
  • 16
1

In Perl, boolean statements are false if they return a value of 0 and true otherwise. A numeric value returns its value.

In that statement, the user is forcing the value of the if statement to be true:

if (1) {
    print "This statement is always true\n";
}

And, of course:

if (0) {
    print "This statement is false, and you'll never see this printed\n";
}

The user could have written something like this too:

if ( 1 == 1 ) {
    print "This statement is always true\n"
}

You don't usually see this in an if statement. Why bother with an if if the condition is always true? However, it's quite common for while statements if you need an infinite loop:

while (1) {
    print "Keep doing this over, and over again\n";
}

Usually, there's an escape clause:

my $count = 0
while (1) {
    $count = $count + 1;
    print "The Count is now $count\n";
    last if $total > 100;
}

The only reason I can think someone would want to do this is some sort of debug checking. That is, most of the time the statement would be false, but if set to true, print out some sort of debug statement:

if (0) {    #Change from "0" to "1" to debug
    print "DEBUG: The value of foo = $foo\n";
}

However, there are better ways of handling something like this, such as through a constant:

use constant {
    DEBUG => 0,
};

if (DEBUG) {
    print "DEBUG: The value of foo = $foo\n";
}

Or through a debug subroutine.

David W.
  • 105,218
  • 39
  • 216
  • 337
0

In perl, the following values are considered false

0
Empty String

Everything else is considered true

So, if (1) is considered to always be true

Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

if(1) is indeed returning true every time, it does nothing except define a scope, similar to a code block

Stephan
  • 16,509
  • 7
  • 35
  • 61