CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Always IN Chaos
Novice

Nov 20, 2003, 7:54 PM

Post #1 of 8 (4244 views)
" IF " Statments .. Can't Post

What's the order ??

Code
While  
{


If
{
}

else
{
}

if
{
}

elsif
{
}


}



davorg
Thaumaturge / Moderator

Nov 21, 2003, 1:55 AM

Post #2 of 8 (4240 views)
Re: [Always IN Chaos] " IF " Statments .. [In reply to] Can't Post

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

Post #3 of 8 (4238 views)
Re: [davorg] " IF " Statments .. [In reply to] Can't Post

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

Post #4 of 8 (4237 views)
Re: [Always IN Chaos] " IF " Statments .. [In reply to] Can't Post

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

Post #5 of 8 (4224 views)
Re: [Always IN Chaos] " IF " Statments .. [In reply to] Can't Post

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

Post #6 of 8 (4218 views)
Re: [madhu_p] " IF " Statments .. [In reply to] Can't Post


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

Post #7 of 8 (4215 views)
Re: [davorg] " IF " Statments .. [In reply to] Can't Post

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

Post #8 of 8 (4212 views)
Re: [madhu_p] " IF " Statments .. [In reply to] Can't Post


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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives