Hi predrag,

Welcome to Perl and PerlMonks :-)

Just a note on terminology: Those things in braces are typically called "blocks" (Update 2: not everything in braces is a block, though, for example there are anonymous hashes). "Loops" are normally for/foreach, while, and until, each of which have their own "blocks". So what you've got there are three if/elsif/else structures, with a total of four blocks. (Update: See Compound Statements)

There's nothing obviously wrong with the code you showed. Whether or not it's "correct" depends on whether your program works or not :-)

The best way to know whether your program works or not is tests. That is, a set of test input data and an expected output to which you can compare the program's actual output (there are ways to automate this, but that's a different topic). In your tests, it's useful to test as many branches of your code as possible, by setting up your test input data so that as many of the if/elsif/else blocks as possible are executed.

Perhaps we could give you some more comments if you could show a slightly longer, more representative sample - for example, something that includes the outer foreach loop and some short example data that it operates on (see also SSCCE). This is just a wild guess since I don't know anything at all about the structure of your input data and what you're doing with it, but perhaps one of these two variants of the logic might be more appropriate:

if ($char eq "a") { ... } elsif ($char eq "b") { ... } if ($k==2) { ... } else { ... } # OR if ($char eq "a") { ... if ($k==2) { ... } else { ... } } elsif ($char eq "b") { ... }

Regards,
-- Hauke D


In reply to Re: Begginer's question: If loops one after the other. Is that code correct? (updated) by haukex
in thread Begginer's question: If loops one after the other. Is that code correct? by predrag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.