conditional help

B

buildmorelines

why does this code work?

#!/usr/bin/perl
print "true" if $ARGV[0];

and this doesnt and generates syntax error?

#!/usr/bin/perl
print "true" if $ARGV[0];
else print "false";
 
A

Andres Monroy-Hernandez

buildmorelines said:
why does this code work?

#!/usr/bin/perl
print "true" if $ARGV[0];

and this doesnt and generates syntax error?

#!/usr/bin/perl
print "true" if $ARGV[0];
else print "false";


I think that is simply the way perl syntax is. If you want to have an
if-else section, you have to do something like:

if (EXPR) { } else { }

Check: http://www.perldoc.com/perl5.8.0/pod/perlsyn.html
 
T

Tad McClellan

buildmorelines said:
and this doesnt and generates syntax error?

#!/usr/bin/perl
print "true" if $ARGV[0];
else print "false";


Because it is not a valid Perl program.
 
J

Joe Smith

buildmorelines said:
and this doesnt and generates syntax error?

#!/usr/bin/perl
print "true" if $ARGV[0];
else print "false";

Because perl is not C.

if() {...} elsif() {...} elsif() {...} else {...};

The else part in perl is surrounded by braces.

-Joe
 
C

Chris Mattern

buildmorelines said:
why does this code work?

#!/usr/bin/perl
print "true" if $ARGV[0];

Because this is valid Perl syntax.
and this doesnt and generates syntax error?

#!/usr/bin/perl
print "true" if $ARGV[0];
else print "false";

Because this isn't Perl syntax. You don't get to
make up stuff and tell the interpreter, "You
know what I mean." If you want to use else,
you have to use the compound if statement
with blocks. For one thing, it would be
impossible for the interpreter to correctly
interpret where the else needs to go in nested
if statements if this requirement wasn't
enforced.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
J

Joe Smith

Abigail said:
Joe Smith ([email protected]) wrote on MMMMLI September MCMXCIII in
<URL::: buildmorelines wrote:
::
:: > and this doesnt and generates syntax error?
:: >
:: > #!/usr/bin/perl
:: > print "true" if $ARGV[0];
:: > else print "false";
::
:: Because perl is not C.
::
:: if() {...} elsif() {...} elsif() {...} else {...};
::
:: The else part in perl is surrounded by braces.


But

print "true" if $ARGV [0];
else {print "false"}

is a syntax either. It has nothing at all to do with "an else part
needs to be surrounded by braces".

The point I was making is that when 'else' is not preceded by a '}',
it is a syntax error. Of course the '}' has to be preceded by a '{'
and conditional test to be a complete statement.

You're right about the statement-modifier part; I should have
phrased that differently.
-Joe
 

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

Similar Threads

I need help with some python code 1
Python battle game help 2
Help with code plsss 0
Problem Splitting Text String 2
Please help 7
Help in fixing this code 8
Need help with this script 4
Esp8266 Code problem 0

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top