What Is A Computer Program?

Looping in PHP

A computer program is a series of instructions that must contain sequence, selection, or repetition.

Sequence, Selection Or Repetition

Sequence is when the instructions are read in list fashion. Selection would be when some condition is specified such as if a given field is equal to a number or other given field. Do this if. Repetition is when a given instruction is repeated over and over again until a given condition is met such as do this 100 times. Do this until.

In this picture (taken from http://php.net/) you can see the most common construction. This one is called a for loop. The loop means it will be executed many times. The for means for a given number of times. In the picture the dollar sign indicates that the number after it is placeholder for a variable number much like in algebra. The expressions are separated by semicolons. The first expression just establishes an initial value for the variable. The second expression might say do this until i is less than or equal 10. The value of i would be checked here every time the loop is executed. The last one is incrementing or storing the value of i in i . echo just means to print a value such as i.

This web site is made with Drupal, a PHP program.

In example the loop will

In example the loop will execute 10 times because the middle expression says until i is less than or equal to 10. Also contained in loop but not shown would be the set of instructions you wanted executed 10 times. This loop would be a freestanding module that could be called from programs when needed. Modern computer programs are composed of external modules that are called by many different programs when needed. This is called the top down approach because there would be a few lines of code at the top that would call modules and return back to the top. This is in contrast to the old style that contained longer programs that jumped around in the program a lot using go to instructions producing what was called spaghetti code. These programs were difficult to debug and maintain just for this reason.

  • 1:21 PM 2 August 2015
  • www.dashaver.com

Home   |   Top   |   Bottom   |   Back one page