IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

January Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgramming   Print This     
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 29 months 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);

}


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1185A1
Enter key:
Article Contributed By Sam.H:
Visit Profile

 KB Article #102724 Counter
3181
Since 8/19/2015
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2018 Prestwood IT Solutions.   [Security & Privacy]