180 likes | 307 Views
This guide outlines essential concepts related to control flow constructs in programming, crucial for IT441 students. It covers various types of control statements including if, while, and for loops. Students will learn about increment/decrement operations, logical string and arithmetic comparisons, and the significance of default variables. Practical exercises are provided to solidify understanding, inviting students to write programs that implement these concepts. For further details, reach out to Professor Alfred J. Bird or visit the course webpage.
E N D
IT441Network Services Administration Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu http://it441-s14-bird.wikispaces.umb.edu/ Door Code for IT441 Students – 536587* Office – McCormick 3rd floor 607 Office Hours – Tuesday and Thursday 4:00pm to 5:15pm
Ways to get out of a program • exit (0); • die $string;
Increment/Decrement • A very helpful construct is the increment/decrement statement • $i++ is equivalent to $i = $i+1 • $j = $i++ • $j = ++$i • $j = $i - - • $j = - - $i
Arithmetic Comparisons • $x > $y • $x < $y • $x >= $y • $x < =$y • $x == $y • $x != $y
String Comparisions • $x gt $y • $x lt $y • $x ge $y • $x le $y • $x eq $y • $x ne $y
Logical Operators • $x and &y • $x && $y • $x or $y • $x || $y • not $x • !$x
Control Flow Constructs • What is a control statement? • Types of control statements: • if • while • for
If Statements • if • if ( condition ) { action } • if else • if (condition ) { action } • else {another action } • if elsif else • if ( condition ) { action } • elsif (another condition ) { another action } • … • else { a further action }
If Statements Practice • Write a short program to: • Read in a number • Test if it is zero, even or odd • Print out the result
If Statements (cont) • unless statement unless (condition) {action}; • Reversed order print “Hello Al\n” if($inputName = “Al”); die “Can’t divide by zero\n:” if ($num4 == 0);
While Statement • while loops • while ( condition ) { action } • $i=1; • while ($i<=5) { • print $i, “\n”; • $i++; • } • until loops • Same form but opposite action of the while loop
While Statement Exercise • Write a short program using a while loop to: • Print out the integers from -5 to 5 • Modify the program to use an until loop
An interesting variable • $_ is the default variable for many functions • while ( $line = <STDIN>) { • chomp ($line); • … • } • while (<STDIN>) { • chomp; • … • }
Breaking Out of a Loop • There are three ways to modify the execution of the loop: • last; • next; • redo; • Statement labels • Labels a location in the program • Best to use all uppercase letters OUTER: while(…) { … }
Another Form of Loops • do { action } while ( condition ); • do { action } until ( condition );
For statement • for loop • for ( init_exp ; test_exp; step_exp) { action } • for ($i=1; $i<5; $i++) {print $i, “\n”;}
Foreach loop • foreach my $number (1..10) { • print “The number is: $number \n”; • }
For next time • Read Chapter 3 “Control Flow Constructs” pp 53-79 • Exercises 1, 2 & 3 on page 79