IT SOLUTIONS
 Coder
 Coder Group Home eMag: Coder Edition WEEKLYLESSON MYGROUPSETTINGS
 Message Board
 Coder KB
 Coder Study Test PRESTWOODCERTIFIED
 Coder Store PRESTWOODSTORE Store Home Buy Coder Items
 Members Only

Prestwood eMagazine

March Edition
Subscribe now! It's Free!

 ► KB ► Programming
 Next Random Article Next Random Tidbit || Change Topic Random From All
From the March 2016 Issue of Prestwood eMag
 Coder Programming:Control Structures, and Recursion
Posted 9 years ago on 8/19/2015
Take Away:

Control Flow in a Computer Program: Sequential, Conditional, Iteration.  Also has an introduction to Recursion.

KB102724

In computer programming, the control flow of the program is generally one of 3 ways:

Sequential, Conditional, and IterationSequential is straight forward: Process a command then the next command then the next command...      Conditional is when we reach a point that we have to make a decision about which way to proceed. For example: if there isn't any inventory at the store, then place a large order, else if there is a small amount of inventory at the store then place a small order, else if there is plenty of inventory at the store then do not place an order.  Iteration is a repetitive form of Conditional (loop); we repeat a sequence of commands as long as a condition is met. Example: Read the name and enrollment date for each student while there are more students in the list; the condition is: Are there any more students in the list? If YES then read one more student's information and ask again: Are there any more students in the list? ...  Keep doing that until the answer to the question is NO (there are no more students in the list) then proceed to the next step in the program.

There is a fourth approach in which a problem is considered solved when a smaller set of the problem is solved, this smaller set is considered solved when a smaller set of it is solved... This approach is called Recursion.  In Recursion, a function calls itself until a "Base Case" is met. Let's take the popular "factorial" Math problem as an example. What is the factorial of 3? It is 3 multiplied by the factorial of 2.  When we figure out the factorial of 2 then the original problem of the factorial of 3 is solved.  What is the factorial of 2? It is 2 multiplied by the factorial of 1.  The same argument applies here: When we figure out the factorial of 1, then we figured out the factorial of 2, then we figured out the original problem: the factorial of 3. Let's proceed to figure out the factorial of 1: It is 1 multiplied by the factorial of 0. What is the factorial of 0? It is 1. The problem of the factorial of 0 is solved. This is called the "Base Case": we reached a subset of the problem for which the solution is found.  Now that we know the factorial of 0, let's figure out the factorial of 1, it is 1 multiplied by 1 which equals 1.  What about the factorial of 2? It is 2 multiplied by 1 which equals 2. What about the factorial of 3? It is 3 multiplied by 2 which equals 6.  In C#, we write the recursive factorial function as follows:

int factorial(int x){

if (x==0) return 1;  //Base Case

else return x*factorial(x-1);

}