Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Evaluating the expression in if VS While

Expand Messages
  • Shlomi Fish
    ... A few notes about your code: 1. You re missing use strict; and use warnings; . Please add them. 2. Your first print is missing a n . 3. You are
    Message 1 of 10 , Jun 2, 2009
    • 0 Attachment
      On Tuesday 02 June 2009 15:59:50 edu_kumar wrote:
      > Hi all,
      >
      > I'm a beginner in Perl and I slowly started writing small programs in it.
      > While I was playing with the if and while loops in Perl, I got into a
      > really weird scenario. Please look at the snippet of code below..
      >
      > -----------------------Program starts here-------------------------
      > #!/usr/bin/perl
      >
      > $index = 0;
      > $s = 1;
      > if($s<1){
      > print("Hey, in perl its different man ! ");
      > }
      > else
      > {
      > print("The whole world stays as is..! \n");
      > }
      >
      > while($index <1) {
      > print ("$index < 1 is true! \n");
      > $index += 0.1;
      >
      > print ("$index\n");
      >
      > ---------------------------Program ends here -----------
      >

      A few notes about your code:

      1. You're missing "use strict;" and "use warnings;". Please add them.

      2. Your first print is missing a "\n".

      3. You are missing the closing right brace of the while loop.

      > the output I got is pretty weird. Here's the output...
      >
      > -- This is the output of the if statement and it looks very fine to me.
      >
      > The whole world stays as is..!
      >
      > -- However, this is the output of the While loop. Strangely the condition (
      > 1 < 1 ) evaluates to false in case of if loop and true in case of while
      > loop.

      That's because you're using the variable $index in the while loop which is
      initially set to 0. And 0 < 1. If you've used $s then the while loop would
      have terminated.

      Regards,

      Shlomi Fish

      >
      > 0 < 1 is true!
      > 0.1
      > 0.1 < 1 is true!
      > 0.2
      > 0.2 < 1 is true!
      > 0.3
      > 0.3 < 1 is true!
      > 0.4
      > 0.4 < 1 is true!
      > 0.5
      > 0.5 < 1 is true!
      > 0.6
      > 0.6 < 1 is true!
      > 0.7
      > 0.7 < 1 is true!
      > 0.8
      > 0.8 < 1 is true!
      > 0.9
      > 0.9 < 1 is true!
      > 1
      > 1 < 1 is true!
      > 1.1
      >
      >
      > Obviously I might be doing something stupid here and I wasn't able to
      > figure it out. can someone point me here in the right direction ? given
      > below is my perl version..
      >
      > ------------------------------------------------
      > bash-3.00$ /usr/bin/perl -version
      >
      > This is perl, v5.8.4 built for i86pc-solaris-64int
      > (with 29 registered patches, see perl -V for more detail)
      >
      > Copyright 1987-2004, Larry Wall
      >
      > Perl may be copied only under the terms of either the Artistic License or
      > the GNU General Public License, which may be found in the Perl 5 source
      > kit.
      >
      > Complete documentation for Perl, including FAQ lists, should be found on
      > this system using `man perl' or `perldoc perl'. If you have access to the
      > Internet, point your browser at http://www.perl.com/, the Perl Home Page.
      >
      > ------------------------------------------------

      --
      -----------------------------------------------------------------
      Shlomi Fish http://www.shlomifish.org/
      Best Introductory Programming Language - http://xrl.us/bjn84

      God gave us two eyes and ten fingers so we will type five times as much as we
      read.
    • murali iyer
        [Non-text portions of this message have been removed]
      Message 2 of 10 , Jun 7, 2009
      • 0 Attachment
         




        [Non-text portions of this message have been removed]
      • Jeff Pinyan
        In case you didn t see (and I assume you didn t): ... -- The Cross Reference - http://thecrossreference.blogspot.com/ Critical Mass (The Science of the
        Message 3 of 10 , Jun 7, 2009
        • 0 Attachment
          In case you didn't see (and I assume you didn't):

          To unsubscribe from this group, send an email to:
          > perl-beginner-unsubscribe@yahoogroups.com
          >

          --
          The Cross Reference - http://thecrossreference.blogspot.com/
          Critical Mass (The Science of the Liturgy) -
          http://romanliturgy.blogspot.com/

          [Mary said,] "Do whatever he tells you." ~ John 2:5


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.