230 likes | 502 Views
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
E N D
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).
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;
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).
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
Logical Expression • What does it mean if an expression is "true" or "false"? • For example: 2 < 5 • 6 != 4 • Is "45" "true" or "false"?
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)
Conditional statement • if <condition true> • if <condition> … else … • if <condition> … elsif … else … if($a == 5) { print "$a \n"; $a=6; } $a=0;
Conditional statement if($a == 5) { print "$a \n"; $a=6; } else{ print "$a \n"; } $a=0;
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;
repeating statements/loops -- while while(condition true) { statement 1 statement 2 : } Statement (outside of while loop)
Formal Statements • Assignment $a = 5; $b = $a + 1; • "for" iterative statement (for-loop) informally: for i = 1 to 5
For-loop for(initialize statement;condition;post statement) { # loop body statement 1 statement 2 : } Out of loop statement
For-example (Perl) for($i=0;$i<5;$i=$i+1) { $a=5; $j=$i+5; print "$j\n"; } $a=6;
Logical Operators Not (!) operator 1 = 1 0 = 0 !1 = 0 !0 = 1 Example: if( !(!0)) evaluates to false if(!(5>4)) evaluates to false
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
"call" • Call (function, subroutine, procedure) • Change where the algorithm (program) is running
Subroutine Example statement 1 statement 2 subroutine() statement 3 #end program subroutine() statement a statement b return()
Subroutine Example -- pass by value statement 1 statement 2 subroutine(x) statement 3 #end program subroutine() newValue = x statement a statement b return()
Subroutine Example -- return statement 1 statement 2 y=subroutine(x) statement 3 #end program subroutine() newValue=x statement a statement b return(z)
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(); }
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(); }
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); }