Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Iteration While / until/ for loop

2 Iteration: while/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initalise condition variable, 2.write 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 Run the above program to see the output This type of condition is referred to as counter control

3 Loops with defined #!/usr/bin/perl 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 sentinal control

4 Default variable $_ $_ is a variable but 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 knows to use the $_ – #!/usr/bin/perl – print Type something. quit to finish\n ; – while (<>) { chomp; – if ($_ eq quit) – { last; # breaks out of while loop if quit – } # $_ generic variable name – print You typed $_ \n\n; – print Type something> ; – } – print goodbye!\n; Class question: – Run the following program WhileLoopEg2.pl – Explain the output ?

5 Iteration: The For loop when you know the number of iterations required use For-loop !!!! Run the following programs to see the output – 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"; }

6 While loop to finding length of file #!/usr/bin/perl # file size.pl $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

7 Sample Exercise Problem definition: – this time 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


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

Similar presentations


Ads by Google