Control Structures

The do {} while / until loop

The while / until statement we saw before tests its condition at the top of every loop, before the loop is entered.

The do {} while / until construct, on the other hand, allows us to test the condition at the bottom of the loop, after it is executed once.

Using while:

do{
  __________________;
  __________________;
  __________________;
   
}while (expression);   #if condition is true, execute block again

Using until:

do{
  __________________;
  __________________;
  __________________;
   
}until (expression);   #if condition is false, execute block again

Example:

Ask user to enter no. of translated nucleotides in an mRNA sequence, and calculate the number of amino acids in the resulting protein only if the nucleotide number is positive and is divisible by 3.

#!/usr/bin/perl

my ($nuc, $aa);

do{

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

}until ($nuc > 0 and $nuc % 3 == 0);

$aa=$nuc/3;
print "No. of amino acids: $aa\n";

Other solutions to the same problem: click here.


Table of Contents.
Previous | Next.