Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.

Similar presentations


Presentation on theme: "Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition."— Presentation transcript:

1 Iteration While / until/ for loop

2 While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition variable, 2.A correct condition expression, 3.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

3 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

4 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

5 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

6 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 foreach loop and will be covered in the dynamic array section

7 Recall : FASTA format The following is “fasta” file that is commonly used in bioinformatics: DNA_seq.fasta >Gene_ID, Gene name, date sequenced TTGTCAGAGCCCCAGCTGGTGTCCAGGGACTGACCGTGAGCCTGGGTGAAAGTGAGTTCC CCGTTGGAGGCACCAGACGAGGAGAGGATGGAAGGCCTGGCCCCCAAGAATGAGCCCTG AGGTTCAGGAGCGGCTGGAGTGAGCCGCCCCCAGATCTCCGTCCAGCTGCGGGTCCCAG AGGCCTGGGTTACACTCGGAGCTCCTGGGGGAGGCCCTTGACGTGCTCAGTTCCCAAACA GGAACCCTGGGAAGGACCAGAGAAGTGCCTATTGCGCAGTGAGTGCCCGACACAGCTGC ATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGC CGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATC ACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGG CCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACGGGGCCCTGGA TAAACAGAGGCAGGCGAGGCCACCCCCATCAAG…… The line in Bold is the “descriptor line” The rest are nucleotides bases

8 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 a fasta file called Dna_Seq.fasta

9 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


Download ppt "Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition."

Similar presentations


Ads by Google