Monday, December 7, 2009

Learnings Dec. 1- Jan.5 (lorebeth betinol)

WE have no class!! Becausse its teachers day. There is a Teachers dance contest at the Tagum City Pavilion.

But some of our classmates is task to report about iterative statements..

Here....

Iterative StatementsIterative statements, commonly referred to as looping statements, involve executing a series of statements zero or more times. We will discuss two specific shell looping statements, the while loop and the for loop.
The while loop executes the statement block enclosed within zero or more times as long as the boolean condition (or command) evaluates to TRUE (which again means returns a zero exit status). The general syntax of the while loop is: while boolean_condition
do
statement
optional statement(s)
done
As with the if-then statement, the boolean_condition is usually (but not always) evaluated using the test command. Note also that the body of the while is enclosed with the do-done statements. Thus if we wanted to find a sum of the numbers between 1 and 5, we could do this with the following while loop: COUNT=1
SUM=0
while [ "$COUNT" -le 5 ]
do
SUM=`expr $SUM + $COUNT` # or SUM=$(( SUM + COUNT ))
COUNT=`expr $COUNT + 1`
done
Note the double quotes surrounding the COUNT variable; these are to make sure this variable is seen by test, even if it has no value, to avoid any errors (as described above). Always keep in mind when coding looping statements that a condition must occur to terminate the loop, otherwise an infinite looping condition occurs. Incrementing the variable COUNT by 1 in this cases causes the value of COUNT to eventually reach 6 causing the loop to terminate. As with the if-then scenario described above, the while-do structure can be written as follows if desired: while [ "$COUNT" -le 5 ] ; do Another looping statement worth of discussion here is the for statement. While many modern languages have for statements, the for statement in the shell is a little different. The general syntax is as follows: for VARIABLE in list
do
statement
optional statement(s)
done
Unlike the while statement, a for statement doesn't terminate based upon a condition, rather it terminates when all items in the list have been "processed." The concept of a list was first introduced with the discussion regarding special environment variables. A list is a grouping of data items, sometimes referred to as words, since they are separated by spaces. A list may have zero items, a few items, or a large number of items. A list (with more than one item) will have a beginning item (the leftmost) and an ending item (the rightmost). The items in the list can be strings, variables, files, etc.
When a for loop executes for the first time, the VARIABLE will be assigned the first (leftmost) item in the list. During each subsequent iteration of the loop, the variable will be assigned the next item in the list. Thus when all items in the list have been assigned to the variable, the loop terminates. Refer to the following example: The variable name in this example is VALUE, and the list is composed of the 3 items (or words) "fred", "barney", and "dino." Thus when this loop executes, the following is the output: VALUE: fred VALUE: barney VALUE: dinoNotice there are 3 items in the list and the body of the loop executes 3 times. Thus for a list of size n, the body of a for loop will execute n times. Lists in for loops can be specified a number of ways, for example:
list
represents
item1 item2 ... itemn
a list of n items
$1 $2 $3
a list of 3 command line parameters
$*
a list of all command line parameters
*
a list of all files in current directory
a*
a list of all files starting with the letter aFor loops are typically used to process a quantity of things, for example, renaming all of the files in a directory by appending an extension of .backup (to indicate a backup copy) to their filename. As with the other scenarios described above, the for can be written as follows if one prefers: for VARIABLE in list ; do

No comments: