Loop Control

An example for using the redo command

redo restarts a loop block
#!/usr/bin/perl

# Ask user to enter no. of translated nucleotides in a mRNA
# sequence, and calculate no. of amino acid in the resulting protein.
# Repeat asking user for the number until the entered value is
# positive and divisible by 3.



{
   print "please enter no. of translated nucleotides: ";
   $nuc = <STDIN>;
   chomp ($nuc);

   if ($nuc <= 0) {
      print "wrong input - negative or zero number\n";
      redo;
   }

   if ($nuc % 3 != 0) {
      print "wrong input - number not divisible by 3\n";
      redo;
   }
}

print "no. of amino acids: ", $nuc/3, "\n";

Table of Contents.
Previous