www.webdeveloper.com
Results 1 to 2 of 2

Thread: simple perl script

  1. #1
    Join Date
    Feb 2003
    Posts
    20

    simple perl script

    Hey I m just starting to learn some perl, this may not be a good script, but i am wondering where my errors are, and how I can improve it.


    #!usr/bin/perl

    $c = 10;

    print "\nType something, and be amazed!\n";
    $a = <STDIN>;
    chomp $a;
    print "you typed, \"$a\" WOW!\n";
    #9
    print "Now let's try something else!\n enter a number between -10 and 10\n";
    $b = <STDIN>;
    chomp $b;
    #14
    do
    {
    for ($c == -10,$c <= 10,$c++);
    } while($b != $c);
    if ($b != $c)
    {
    print "You didn't enter a value between -10 and 10! DO SO1\n";
    $b = <STDIN>;
    }
    else
    {
    print "You entered $b\n";
    $b **= 2;
    print "When squared that equals $b\n";
    print "is that true?\n";
    $d = <STDIN>;
    if ($d == yes)
    {
    print "Darn right it is!\n";
    }
    else
    {
    print "Liar!\n";
    }
    }

  2. #2
    Join Date
    Feb 2003
    Location
    Mt Airy MD
    Posts
    379
    Here's one problem:
    Code:
    #14
    do {
      for ($c == -10,$c <= 10,$c++);
    } while($b != $c);
    
    if ($b != $c) {
      print "You didn't enter a value between -10 and 10! DO SO1\n";
      $b = <STDIN>;
    }
    If $b is not == $c your code never gets to the if($b != $c) section. Also, since you will forever do the do-while loop untill such time as $b == $c you don't need the if($b != $c) section at all.


    Last, since you've made no provision for the person entering something NOT between -10 and 10 if they do enter something outside that range you will enter an infinite loop at your do-while statement.
    Phil Karras
    Circle Software Consulting
    http://cs.yrex.com/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles