Presentation is loading. Please wait.

Presentation is loading. Please wait.

Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik.

Similar presentations


Presentation on theme: "Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik."— Presentation transcript:

1 Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik – 422222, India

2 OC-SBI083-CP2-01 Introduction Programmes and Courses  SEP – SBI083-CP2- 01

3 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.3 Credits  Academic Inputs by Sonali Alkari MSc (Botany), P.G. D.C. Bio-Informatics sonalisa_alkari@yahoo.com

4 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.4 How to Use This Resource  Counselor at each study center should use this presentation to deliver lecture of 40-60 minutes during Face-To-Face counseling.  Discussion about students difficulties or tutorial with assignments should follow the lecture for about 40-60 minutes.  Handouts (with 6 slides on each A4 size page) of this presentation should be provided to each student.  Each student should discuss on the discussion forum all the terms which could not be understood. This will improve his writing skills and enhance knowledge level about topics, which shall be immensely useful for end exam.  Appear several times, for all the Self-Tests, available for this course.  Student can use handouts for last minutes preparation just before end exam.

5 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.5 Learning Objectives  After studying this module, you should be able to: Execute Flow control, conditional testing What is statement block How to write and execute loops

6 School of Science and Technology, Online Counseling Resource… Flow Control  The order in which statements in the programs are executed by the computer is called flow control.  A program executes from the first statement at the top of the program to the last statement at the bottom, in order, unless told to do otherwise.  There are two ways to do it otherwise: condition statement and loops.  A conditional statement executes a group of statements only if the conditional test succeeds; otherwise, it just skips the group of statements.  A loop repeats a group of statement until an associated test fails. © 2007, YCMOU. All Rights Reserved.6

7 School of Science and Technology, Online Counseling Resource… Statement Blocks-1  A statement block is a sequence of statements, enclosed in matching curly braces. It looks like this: { first_statement; second_statement; third_statement;... last_statement; }  Perl executes each statement in sequence, from the first to the last. © 2007, YCMOU. All Rights Reserved.7

8 School of Science and Technology, Online Counseling Resource… Statement Blocks-2 if ($ready) { $hungry++ } if ($tired) { $sleepy = ($hungry + 1) * 2; }  The main feature of these kinds of construct is the testing for a conditional. © 2007, YCMOU. All Rights Reserved.8

9 School of Science and Technology, Online Counseling Resource… Conditional Tests & Matching Braces  There are several tests that can be used in the conditional part of the statement.  In addition to numeric equality == we can also test for inequality!=, greater than >, less than< and more.  Similarly we can test for string equality using the eq operator; if two strings are the same.  The statements that follow the conditional are enclosed within matching pair of curly braces.  These statements within curly braces are called a block and arise frequently in perl.  Matching braces are easy to lose track of, and this is a common syntax error. © 2007, YCMOU. All Rights Reserved.9

10 School of Science and Technology, Online Counseling Resource… The if statement -1  Like most other languages perl uses the keyword if to implement the decision control instruction.  The general form of if statement look like this : if (this condition is true) execute this statement;  The keyword if tells the compile that, this is a decision control instruction.  The condition following the keyword if is a always enclosed within a pair of parentheses.  If the condition, whatever it is, is true, then the statement is executed.  If the statement is not true then the statement is not executed. © 2007, YCMOU. All Rights Reserved.10

11 School of Science and Technology, Online Counseling Resource… The if statement -2  The if statement with a true conditional: if (1==1) { print “1 equals 1\n\n”; } produces the output 1 equals 1. © 2007, YCMOU. All Rights Reserved.11

12 School of Science and Technology, Online Counseling Resource… If-else Statement-1  The if statement by itself will execute a single statement, or a group of statements, when the condition following if is true.  It does nothing when the condition is false.  Can we execute one group of statements if the condition is true and another group of statement if the condition is false.  This is the purpose of else statement. if (some_expression) { true_statement_1; } else { false_statement_1; } © 2007, YCMOU. All Rights Reserved.12

13 School of Science and Technology, Online Counseling Resource… If-else Statement-2  The group of statement after the if up to and not including the else is called an ‘if block’.  Similarly, the statement after the else form the ‘else block’.  The else is written exactly below if.  The statement in the if block and those else block have been indented to the right print "how old are you? ";$a = ; chomp($a); if ($a < 18) { print "So, you're not old enough to vote, eh?\n"; } else { print "Old enough! Cool! So go vote!\n"; $voter++; # count the voters for later } © 2007, YCMOU. All Rights Reserved.13

14 School of Science and Technology, Online Counseling Resource… Unless Statement  Sometimes, you want to leave off the "then" part and have just an else part, because it is more natural to say "do that if this is false," rather than "do that if not this is true.“  Perl handles this with the unless variation: print "how old are you? "; $a = ; chomp($a); unless ($a < 18) { print "Old enough! Cool! So govote!\n"; $voter++; }  Replacing if with unless is in effect saying "If the control expression is false, do...." (An unless can also have an else, just like an if.) © 2007, YCMOU. All Rights Reserved.14

15 School of Science and Technology, Online Counseling Resource… If –elsif-else Statement-1  If you have more than two possible choices, add an elsif branch to the if statement, like so: if (some_expression_one) { one_true_statement_1; one_true_statement_2; one_true_statement_3; } elsif (some_expression_two) { two_true_statement_1; two_true_statement_2; two_true_statement_3; } elsif (some_expression_three) { three_true_statement_1; three_true_statement_2; three_true_statement_3;} else { all_false_statement_1; all_false_statement_2; all_false_statement_3; } © 2007, YCMOU. All Rights Reserved.15

16 School of Science and Technology, Online Counseling Resource… If –elsif-else Statement-2  Each expression (here, some_expression_one, some_expression_two, and some_expression_three) is computed in turn.  If an expression is true, the corresponding branch is executed, and all remaining control expressions and corresponding statement blocks are skipped.  If all expressions are false, the else branch is executed (if there is one).  You don't have to have an else block, but it is always a good idea.  You may have as many elsif branches as you wish. © 2007, YCMOU. All Rights Reserved.16

17 School of Science and Technology, Online Counseling Resource… Loops  The versatility of the computer lies in its ability to perform a set of instructions repeatedly.  This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.  This repetitive operation is done through a loop control structure.  A loop allow you to repeatedly execute a block of statements within curly braces.  There are several ways to loop in perl; while loops, For loop Foreach loop © 2007, YCMOU. All Rights Reserved.17

18 School of Science and Technology, Online Counseling Resource… The while Loop  To execute while statement, Perl evaluates the control expression (some_expression in the example).  If its value is true (using Perl's notion of truth), the body of the while statement is evaluated once.  This is repeated until the control expression becomes false, at which point Perl goes on to the next statement after the while loop. For example: print "how old are you? "; $a = ; chomp($a); while ($a > 0) { print "At one time, you were $a years old.\n"; $a--; } © 2007, YCMOU. All Rights Reserved.18

19 School of Science and Technology, Online Counseling Resource… Untill Statement  Sometimes it is easier to say "until something is true" rather than "while not this is true." Once again, Perl has the answer. Replacing the while with until yields the desired effect: until (some_expression) { statement_1; statement_2; statement_3; }  Note that in both the while and the until form, the body statements will be skipped entirely if the control expression is the termination value to begin with.  For example, if a user enters an age less than zero for the program fragment above, Perl skips over the body of the loop. © 2007, YCMOU. All Rights Reserved.19

20 School of Science and Technology, Online Counseling Resource… The do {} while Statement  Sometimes you don't want to test the condition at the top of the loop. Instead, you want to test it at the bottom.  To fill this need, Perl provides the do {} while statement, which is just like the regular while statement except that it doesn't test the expression until after executing the loop once. do { statement_1; statement_2; statement_3; } while some_expression; © 2007, YCMOU. All Rights Reserved.20

21 School of Science and Technology, Online Counseling Resource… The do {}until Statement  As with a normal while loop, you can invert the sense of the test by changing do {} while to do {} until.  The expression is still tested at the bottom, but its sense is reversed. For some cases, especially compound ones, this is the more natural way to write the test. $stops = 0;do { $stops++; print "Next stop? "; chomp($location = ); } until $stops > 5 || $location eq 'home'; © 2007, YCMOU. All Rights Reserved.21

22 School of Science and Technology, Online Counseling Resource… The for Statement  Another Perl iteration construct is the for statement, which looks suspiciously like C or Java's for statement and works roughly the same way. Here it is: for ( initial_exp; test_exp; re-init_exp ) { statement_1; statement_2; statement_3; }  In this case the initial_exp expression is evaluated first.  This expression typically assigns an initial value to an iterator variable, but there are no restrictions on what it can contain; in fact, it may even be empty (doing nothing). © 2007, YCMOU. All Rights Reserved.22

23 School of Science and Technology, Online Counseling Resource… The for Loop  Then the test_exp expression is evaluated for truth or falsehood.  If the value is true, the body is executed, followed by the re-init_exp (typically, but not solely, used to increment the iterator).  Perl then reevaluates the test_exp, repeating as necessary.  This example prints the numbers 1 through 10, each followed by a space: for ($i = 1; $i <= 10; $i++) { print "$i "; }  Initially, the variable $i is set to 1. Then, this variable is compared with 10, which it is indeed less than or equal to. The body of the loop (the single print statement) is executed, and then the re-init expression (the autoincrement expression $i++) is executed, changing the value in $i to 2 © 2007, YCMOU. All Rights Reserved.23

24 School of Science and Technology, Online Counseling Resource… The foreach Statement-1  Another iteration construct is the foreach statement. This statement takes a list of values and assigns them one at a time to a scalar variable, executing a block of code with each successive assignment.  It looks like this: foreach $i (@some_list) { statement_1; statement_2; statement_3; }  Unlike in the C-shell, the original value of the scalar variable is automatically restored when the loop exits; another way to say this is that the scalar variable is local to the loop. © 2007, YCMOU. All Rights Reserved.24

25 School of Science and Technology, Online Counseling Resource… The foreach Statement-2  Here's an example of a foreach: @a = (1,2,3,4,5); foreach $b (reverse @a) { print $b; }  This program snippet prints 54321.  Note that the list used by the foreach can be an arbitrary list expression, not just an array variable. (This is typical of all Perl constructs that require a list.) © 2007, YCMOU. All Rights Reserved.25

26 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.26 What You Learn-1…  The order in which statements in the programs are executed by the computer is called flow control.  A statement block is a sequence of statements, enclosed in matching curly braces.  A conditional statement executes a group of statements only if the conditional test succeeds; otherwise, it just skips the group of statements.

27 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.27 What You Learn-2…  There are several conditional statements like if statement, if-else, unless, If –elsif-else Statement  A loop repeats a group of statement until an associated test fails.  There are several ways to loop in perl, while loops, For loop, Foreach loop © 2007, YCMOU. All Rights Reserved.

28 School of Science and Technology, Online Counseling Resource… Critical Thinking Questions 1.Write a detailed account on different Loop statements. 1. Write a detailed account on different Conditional statements. © 2007, YCMOU. All Rights Reserved.28

29 School of Science and Technology, Online Counseling Resource… Hints For Critical Thinking Question 1.A conditional statement executes a group of statements only if the conditional test succeeds; otherwise, it just skips the group of statements.There are several conditional statements like if statement, if-else, unless, If –elsif-else Statement. 1.A loop repeats a group of statement until an associated test fails.There are several ways to loop in perl, while loops, For loop, Foreach loop © 2007, YCMOU. All Rights Reserved.29

30 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.30 Study Tips:Books  Beginning Perl Beginning Perl Simon Cozens, Peter Wainwright. 700 pages. Wrox Press Inc. (May 25, 2000).  Impatient Perl Greg London (Feb 7, 2004). Extreme Perl by Robert Nagler Impatient Perl Extreme Perl  MacPerl: Power & Ease Vicky Brown and Chris Nandor. 372 pages. (1998). MacPerl: Power & Ease  Picking Up Perl Bradley M. Kuhn and Neil Smyth. self published. second edition. (July 2005). Picking Up Perl

31 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.31 Study Tips:Web Resources www.en.wikipedia.org Microsoft Encarta Encyclopedia http://www.perl.org/ http://en.wikibooks.org/wiki/Programming:Perl_Websites http://cpan.perl.org/

32 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.32 Community Web Sites  Planet Perl - an aggregation of selected perl journals Planet Perl  use Perl; - read community news and personal journals, or start your own journal use Perl;  Perl Monks - the wisdom of the Monks can guide you on your Perl Quests Perl Monks  perl.org - your current location perl.org  the Perl Apprenticeship Site the Perl Apprenticeship Site

33 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.33 End of the Presentation Thank You


Download ppt "Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik."

Similar presentations


Ads by Google