Home: Perl Programming Help: Beginner:
" IF " Statments ..



Always IN Chaos
Novice

Nov 20, 2003, 7:54 PM


Views: 6793
" IF " Statments ..

What's the order ??

Code
While  
{


If
{
}

else
{
}

if
{
}

elsif
{
}


}



davorg
Thaumaturge / Moderator

Nov 21, 2003, 1:55 AM


Views: 6789
Re: [Always IN Chaos] " IF " Statments ..

Not sure what you're asking. Please rephrase your question.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Always IN Chaos
Novice

Nov 21, 2003, 2:36 AM


Views: 6787
Re: [davorg] " IF " Statments ..

is the "code" above the correct format for doing While "blocks" or is there a better way?


davorg
Thaumaturge / Moderator

Nov 21, 2003, 5:23 AM


Views: 6786
Re: [Always IN Chaos] " IF " Statments ..

The syntax for a while loop is

Code
while (COND) BLOCK

Where "COND" is any logical condition and "BLOCK" is a block of code that is executed as long as COND is true. Note that in Perl a block of code is always delimited with { and } so an example while loop might look like this

Code
$x = 10; 
while ($x) {
print "$x\n";
--$x;
}


Does that help? I'm still not sure I understand your question.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


madhu_p
Novice

Nov 29, 2003, 10:34 AM


Views: 6773
Re: [Always IN Chaos] " IF " Statments ..

try this# I think u should use 'else' after 'elsif'While (cond)
{


If (cond)
{
}

else
{
}

if (cond)
{
}

elsif (cond)
{
}
else(cond) # I think u should use 'else' after 'elsif'.{}
}hope this could help uSmilemadhu
Kaiserslautern,Germany


davorg
Thaumaturge / Moderator

Nov 30, 2003, 1:17 PM


Views: 6767
Re: [madhu_p] " IF " Statments ..


In Reply To
try this# I think u should use 'else' after 'elsif'While (cond)
{

If (cond)
{
}

else
{
}

if (cond)
{
}

elsif (cond)
{
}
else(cond) # I think u should use 'else' after 'elsif'.{}
}


You don't need to have an 'else' clause after an 'elsif' clause unless your logic requires it.

An 'else' clause _never_ has a condition block.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


madhu_p
Novice

Dec 1, 2003, 4:01 AM


Views: 6764
Re: [davorg] " IF " Statments ..

Iam sorry ,iam wrong 'else 'will not have condition but I think 'else' will follow 'elsif'(ref:programming perl O'reilly : page no:94)

so it will be

'elsif'While (cond)
{

If (cond)
{
}

else
{
}

if (cond)
{
}

elsif (cond)
{
}
else # I think u should use 'else' after 'elsif'.

{

}


}
Kaiserslautern,Germany


davorg
Thaumaturge / Moderator

Dec 1, 2003, 7:39 AM


Views: 6761
Re: [madhu_p] " IF " Statments ..


In Reply To
Iam sorry ,iam wrong 'else 'will not have condition but I think 'else' will follow 'elsif'(ref:programming perl O'reilly : page no:94)


Please include the edition number when referencing pages in books as they can change between editions. Page 94 of the most recent (3rd) edition has nothing to say about if, elsif or else. But on page 113 it has the following examples of compound logical statements:


Code
if (EXPR) BLOCK 
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK


Which does, I confess, imply that an "elsif" block must have a following "else" block, but it's not true. As simple experimentation would have shown. The "else" block is _always_ optional.

I'll submit a patch to the documentation.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks