Presentation is loading. Please wait.

Presentation is loading. Please wait.

4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.

Similar presentations


Presentation on theme: "4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print."— Presentation transcript:

1 4.1 Controls: Ifs and Loops

2 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print "Here is your beer!\n"; } print "How old are you?\n"; my $age = ; # Read number if ($age < 18) { print "How about some orange juice?\n“; } Note the indentation: a single tab in each line of new block ‘ } ’ that ends the block should be in the same indentation as where it started

3 4.3 Comparison operators StringNumericComparison eq== Equal ne!= Not equal lt< Less than gt> Greater than le<= Less than or equal to ge>= Greater than or equal to if ($age == 18)... if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")... if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at...

4 4.4 if, else example Consider the following code: my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; }

5 4.5 if, elsif, else Sometimes it’s convenient to test several conditions in one if structure: my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100) { print "Number out of range\n"; } elsif ($num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; }

6 4.6 Boolean operators if (($age==18) and ($name eq "Yossi")){ print "Hi Yossi18"; } if (($age==18) or ($name eq "Yossi")){ print "Hi You!"; } if (!($name eq "Yossi")){ print "Hi man…" } and && - True if both conditions are true or || - True if at least one condition is true not ! - True if condition is False and && - True if both conditions are true or || - True if at least one condition is true not ! - True if condition is False

7 4.7 my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100) { print "Number out of range\n"; } elsif ($num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; } Boolean operators

8 4.8 my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100 or $num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; } Boolean operators

9 4.9 if (defined... ) There is an option to check whether a variable was defined: ($inFile) = @ARGV; open(IN, "<$inFile") or die "cannot open $inFile"; my @arr = ; if (defined $arr[9]) { print "$arr[9]\n"; }

10 4.10 You can ask questions about a file or a directory name (not filehandle): if (-e $name) { print "The file $name exists!\n"; } -e$name exists -r$name is readable -w$name is writable by you -z$name has zero size -s$name has non-zero size (returns size) -f$name is a file -d$name is a directory -l$name is a symbolic link -T$name is a text file -B$name is a binary file (opposite of -T). File Test Operators

11 4.11 Class exercise 4a Ask the user for his grades average and: 1.print "wow!" if it is above 90. 2.print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. 3.print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. Print an error message if the number is negative or higher than 100 (Use the or operator). 4*.Ask the user for a file name – and print a message stating whether or not the file exist.

12 4.12 Loops: while Commands inside a loop are executed repeatedly (iteratively). The while loop is "repetitive if": executed while the condition holds. my $luckyNum = 42; print "Guess a number\n"; my $num = ; while ($num != $luckyNum) { print "Wrong. Guess again.\n"; $num = ; } print "Correct!!\n"; my $luckyNum = 42; print "Guess a number\n"; my $num = <STDIN>; if ($num != $luckyNum) { print "wrong number..\n"; } else { print "Correct!!\n"; }

13 4.13 Loops: while (defined …) Let's observe the following code : open (IN, "<numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close (IN);

14 4.14 Loops: while Let's observe the following code to compute factorial. (example 5! = 1*2*3*4*5 = 120). my $num = ; my $factorial = 1; my $currentNumber = 1; while ($currentNumber <= $num) { $factorial = $factorial*currentNumber; $currentNumber++; } print "Factorial of $num is $currentNumber\n";

15 4.15 Loops: foreach The foreach loop passes through all the elements of an array my @nameArr = ("Yossi","Daiana","Jojo"); my $name; foreach $name (@nameArr) { print "Hello $name!\n" }

16 4.16 Loops: foreach The foreach loop passes through all the elements of an array open (IN, "<numbers.txt"); my @lines = ; chomp @lines; my $num; foreach $num (@lines) { if ($num > 10) { print $num; } close (IN); open (IN, "<numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close (IN);

17 4.17 Breaking out of loops next – skip to the next iteration last – skip out of the loop open (IN, "<numbers.txt"); my @lines = ; chomp @lines; foreach my $line (@lines) { if ($line eq "") { last; } if ($line <= 10) { next; } print $line; } close (IN);

18 4.18 Breaking out of loops die – end the program and print an error message (to the standard error ) if ($score < 0) { die "score must be positive"; } score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program

19 4.19 Fasta format Fasta format sequence begins with a single-line description, that start with ' > ', followed by lines of sequence data that contains new-lines after a fixed number of characters: >gi|16127995|ref|NP_414542.1| thr operon leader peptide… MKRISTTITTTITITTGNGAG >gi|16127996|ref|NP_414543.1| fused aspartokinase I and homoserine… MG1655]MRVLKFGGTSVANAERFLRVADILESNARQGQVATVLSAPAKITNHLVAMIEKTISGQDALPN AKFFAALARANINIVAIAQGSSERSISVVVNNDDATTGVRVTHQMLFNTDQVIEVFVIGVGGVGGALLEQ NAGDELMKFSGILSGSLSYIFGKLDEGMSFSEATTLAREMGYTEPDPRDDLSGMDVARKLLILARETGRE LELADIEIEPVLPAEFNAEGDVAAFMANLSQLDDLFAARVAKARDEGKVLRYVGNIDEDGVCRVKIAEVD GNDPLFKVKNGENALAFYSHYYQPLPLVLRGYGAGNDVTAAGVFADLLRTLSWKLGV >gi|16127997|ref|NP_414544.1| homoserine kinase [Escherichia coli… MG1655]MVKVYAPASSANMSVGFDVLGAAVTPVDGALLGDVVTVEAAETFSLNNLGRFADKLPSEPREN IVYQCWERFCQELGKQIPVAMTLEKNMPIGSGLGSSACSVVAALMAMNEHCGKPLNDTRLLALMGELEGR ISGSIHYDNVAPCFLGGMQLMIEENDIISQQVPGFDEWLWVLAYPGIKVSTAEARAILPAQYRRQDCIAH GRHLAGFIHACYSRQPELAAKLMKDVIAEPYRERLLPGFRQARQAVAEIGAVASGISGSGPTLFALCDKP ETAQRVADWLGKNYLQNQEGFVHICRLDTAGARVLEN

20 4.20 Class exercise 4b 1.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a while loop (see example FASTA file on the course webpage). 2.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a foreach loop (see example FASTA file on the course webpage). 3.(Ex 3.1b) Read a file containing numbers, one in each line and print the sum of these numbers. (use number.txt from the website as an example). 4*.Read the "fight club.txt" file and print the 1 st word of the 1 st line, the 2 nd word of the 2 nd line, and so on, until the last line. (If the i-th line does not have i words, print nothing).


Download ppt "4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print."

Similar presentations


Ads by Google