1 / 22

Introduction to Programming Basics

Introduction to Programming Basics. This review introduces the basic concepts to programming. Note that most (if not all) modern programming languages support these concepts This review is intended for students who have little or no programming experience

silvio
Download Presentation

Introduction to Programming Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Basics • This review introduces the basic concepts to programming. • Note that most (if not all) modern programming languages support these concepts • This review is intended for students who have little or no programming experience • "Learning Perl" is probably your best option for a reference book (in the syllabus).

  2. Assignment operator (=)Variables and Memory In most programming languages, we want to "store" values (integers, characters, strings, etc.) for later use, or for using in a calculation. To store values, we use "variables" with the assignment operator ( = ) In perl, all (or almost all) variables are preceded by a dollar-sign ($). Examples: $test = 32; $value = 4 * 2; $test = $value + 1; $test = $test + 5;

  3. Rules for Evaluation $x = 3 + 5; $test = $value + (1 * $x); $test = $test + 5; $sequence = "ATGCCT"; Generally, the expressions on the right-hand-side (RHS) of the assignment operator (=) are evaluated FIRST, from inner-most parentheses, to outer-most parentheses, left-to-right. The resulting value is then assigned to the variable on the left-hand-side (LHS).

  4. Rules of Evaluation (cont.) $test = 5; #line 1 $test = $test + 2; #line 2 Because of this rule, it is possible to first read and use the value held within a variable, before it is assigned a new value. For example, in "line 2" above, on the RHS, $test has a value of 5. $test + 2 evaluates to a value of 7 on the RHS. Then 7 is assigned to $test on the LHS. After the completion of "line 2", $test now contains the value of 7 Note that it would be incorrect to put something other than a variable on the LHS (this is NOT algebra): 5 = $x + 2; # INCORRECT

  5. Logical Expression • What does it mean if an expression is "true" or "false"? • For example: 2 < 5 • 6 != 4 • Is "45" "true" or "false"?

  6. Conditional Operators == "equal" for numerical expressions eq > < != "not equal" ne >= <= zero is "false" (0, 8<2, 5 == 4, 5 != 5, etc) Everything else is "true" (1, 5, 8>2, 5 == 5, etc)

  7. Conditional statement • if <condition true> • if <condition> … else … • if <condition> … elsif … else … if($a == 5) { print "$a \n"; $a=6; } $a=0;

  8. Conditional statement if($a == 5) { print "$a \n"; $a=6; } else{ print "$a \n"; } $a=0;

  9. Conditional statement Note -- code blocks Any number of "if" statements may be strung together in this way Conditional == > < >= <= != true/false (0 == false) if($a == 5) { print "$a \n"; $a=6; } elsif($b > 10) { print "$a \n"; } else { $b=5; } $a=0;

  10. repeating statements/loops -- while while(condition true) { statement 1 statement 2 : } Statement (outside of while loop)

  11. Formal Statements • Assignment $a = 5; $b = $a + 1; • "for" iterative statement (for-loop) informally: for i = 1 to 5

  12. For-loop for(initialize statement;condition;post statement) { # loop body statement 1 statement 2 : } Out of loop statement

  13. For-example (Perl) for($i=0;$i<5;$i=$i+1) { $a=5; $j=$i+5; print "$j\n"; } $a=6;

  14. Logical Operators Not (!) operator 1 = 1 0 = 0 !1 = 0 !0 = 1 Example: if( !(!0)) evaluates to false if(!(5>4)) evaluates to false

  15. Logical operator: && (and) || (or) 1 && 1 == 1 1 && 0 == 0 0 && 1 == 0 0 && 0 == 0 1 || 1 == 1 1 || 0 == 1 0 || 1 == 1 0 || 0 == 0 Example: if(( 5>4) && (0)) || (4 != 5) evaluates to TRUE

  16. "call" • Call (function, subroutine, procedure) • Change where the algorithm (program) is running

  17. Subroutine Example statement 1 statement 2 subroutine() statement 3 #end program subroutine() statement a statement b return()

  18. Subroutine Example -- pass by value statement 1 statement 2 subroutine(x) statement 3 #end program subroutine() newValue = x statement a statement b return()

  19. Subroutine Example -- return statement 1 statement 2 y=subroutine(x) statement 3 #end program subroutine() newValue=x statement a statement b return(z)

  20. Subroutine #!/usr/bin/perl $a = 5; $b = 6; print "a = $a\n"; if($a==10) { $a=$b; } print_hello(); if($b == 6) { print "b = $b\n"; } # END of program sub print_hello { $i=11; print "Now in print_hello subroutine\n"; if($i==11) { $i=0; } return(); }

  21. Passing a value "in" #!/usr/bin/perl $a = 5; $b = 6; print "a = $a\n"; if($a==10) { $a=$b; } print_hello($a); if($b == 6) { print "b = $b\n"; } # END of program sub print_hello { $i=$_[0] print "Now in print_hello subroutine\n"; if($i==5) { print "$i = $i\n"; } return(); }

  22. Return Value too #!/usr/bin/perl $a = 5; $b = 6; print "a = $a\n"; if($a==10) { $a=$b; } $a = print_hello($a); print "a = $a\n"; if($b == 6) { print "b = $b\n"; } # END of program sub print_hello { $i=$_[0]; print "Now in print_hello subroutine\n"; if($i==5) { print "i = $i\n"; } $i = $i+1; return($i); }

More Related