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

Slides:



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

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Lecture 8 perl pattern matching features
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
9-1 Iteration: Beyond the Basic PERFORM Chapter 9.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CISC105 – General Computer Science Class 4 – 06/14/2006.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
CSC111 Quick Revision.
ECE Application Programming
Chapter 5 - Control Structures: Part 2
CHAPTER 5A Loop Structure
Logical Operators and While Loops
Iteration: Beyond the Basic PERFORM
Lab5 PROGRAMMING 1 Loop chapter4.
Note the rights settings.
Logical Operators and While Loops
Presentation transcript:

Iteration While / until/ for loop

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

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 foreach loop 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 TTGTCAGAGCCCCAGCTGGTGTCCAGGGACTGACCGTGAGCCTGGGTGAAAGTGAGTTCC CCGTTGGAGGCACCAGACGAGGAGAGGATGGAAGGCCTGGCCCCCAAGAATGAGCCCTG AGGTTCAGGAGCGGCTGGAGTGAGCCGCCCCCAGATCTCCGTCCAGCTGCGGGTCCCAG AGGCCTGGGTTACACTCGGAGCTCCTGGGGGAGGCCCTTGACGTGCTCAGTTCCCAAACA GGAACCCTGGGAAGGACCAGAGAAGTGCCTATTGCGCAGTGAGTGCCCGACACAGCTGC ATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGC CGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATC ACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGG CCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACGGGGCCCTGGA TAAACAGAGGCAGGCGAGGCCACCCCCATCAAG…… 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 a fasta 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