90 likes | 259 Views
Iteration . While / until/ for loop. While/ Do-while loops . Iteration continues until condition is false: 3 important points to remember: Initialise condition variable, A correct condition expression , change condition variable while-loops : #!/ usr/bin/perl $count = 1; #1
E N D
Iteration While / until/ for loop
While/ Do-while loops • Iteration continues until condition is false: • 3 important points to remember: • Initialise condition variable, • A correct condition expression , • change condition variable • while-loops : • #!/usr/bin/perl • $count = 1; #1 • while ($count <= 5) { #2 • print “$count potato\n”; • $count = $count + 1; #3) • } • do – while loops: • #!/usr/bin/perl • $count = 1; #1 • do { • print "$count potato\n”; • $count = $count + 1; # 3 • } • while ($count <= 5); #2 This type of condition is referred to as counter control
Sentinel controlled Loops • #!/usr/bin/perl • use strict; use warnings; • print ‘Type something. “quit” to finish\n ‘; • while ( $line = <> ) • { • chomp $line; • if ($line eq ‘quit’) • { • last; # breaks out of while loop if quit • } • print “You typed ‘$line’\n\n”; • print ‘Type something again ”quit” to finish ‘; • } • print “goodbye!\n”; • Class question: • Run the following program WhileLoopEg1.pl • Explain the output? This type of condition is referred to as sentinel control
The Perl Default variable: $_ • $_ is a default variable ; it does not have to be declared and is a way of reducing the amount of code. If no variable is specified in an expression then perl “assumes ” its $_ • #!/usr/bin/perl • print “Type something. ‘quit’ to finish\n ”; • while (<>) { • chomp; • if ($_ eq ‘quit’) # $_ default variable name • { • last; # breaks out of while loop if quit • } • print “You typed ‘$_ ’\n\n”; • print “Type something> ”; • } • print “goodbye!\n”; • Run the following program : WhileLoopEg2.pl
While loop: other example • #!/usr/bin/perl • use strict; use warnings; • $length = 0; # set length counter to zero • $lines = 0; # set number of lines to zero • print “enter text one line at a time and press (ctrl z) to quit”; • while (<>) # read input one line at a time • { • chomp; # remove terminal newline • $length = $length + length $_ ; • $lines = $lines + 1; • } • print “you inputted or entered the following:\n” • print “LENGTH = $length\n”; • print “LINES = $lines\n”; • Run and explain the output of the above program WhileLoopEg3.pl
Iteration: The For loop • For-loop !!!! A basic for loop is: • Incremental for loop • for ($count = 1; $count < 10; $count++) • { • print "$count potato\n"; • } • De-incremental for loop • for ($count = 10; $count >=1; $count--) • { • print "$count potato\n"; • } • A variation of the for loop is the foreachloop and will be covered in the dynamic array section
Recall : FASTA format • The following is “fasta” file that is commonly used in bioinformatics: DNA_seq.fasta • >Gene_ID , Gene name , date sequenced • TTGTCAGAGCCCCAGCTGGTGTCCAGGGACTGACCGTGAGCCTGGGTGAAAGTGAGTTCCCCGTTGGAGGCACCAGACGAGGAGAGGATGGAAGGCCTGGCCCCCAAGAATGAGCCCTGAGGTTCAGGAGCGGCTGGAGTGAGCCGCCCCCAGATCTCCGTCCAGCTGCGGGTCCCAGAGGCCTGGGTTACACTCGGAGCTCCTGGGGGAGGCCCTTGACGTGCTCAGTTCCCAAACAGGAACCCTGGGAAGGACCAGAGAAGTGCCTATTGCGCAGTGAGTGCCCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACGGGGCCCTGGATAAACAGAGGCAGGCGAGGCCACCCCCATCAAG…… • The line in Bold is the “descriptor line” • The rest are nucleotides bases
Sample Exercise 1 • Problem definition: • ask the user for the name of a FASTA file [ the name must not be hardcoded into the program]: • Open and read the file • Display the number of lines in the file [ assuming it exists] • Close the file • Do not forget to check the precision of your program • The sample code contains afasta file called Dna_Seq.fasta
Sample Exercise 2 • Problem definition: • Open the fasta file Dna_Seq.fasta • Display The descriptor line only • Then ask the user if they want to display the DNA sequence and if so display the sequences [it can be multiple lines] otherwise display an appropriate message. • Close the file • Do not forget to check the precision of your program • The sample code contains the fasta file called Dna_Seq.fasta