Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java.

Similar presentations


Presentation on theme: "Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java."— Presentation transcript:

1 Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java

2  Recursion Concepts  Example Using Recursion: Factorials  Example Using Recursion: Fibonacci Series  Recursion vs. Iteration  What is Pointers ?  Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra

3  A recursive method is a method that calls itself.  A recursive method can be called either directly, or indirectly through another method. 3 Presented & Prepared by: Mahmoud R. Alfarra Recursion Concepts Method Calls Direct recursion

4  A recursive method may call another method, which may in turn make a call back to the recursive method.  Such a process is known as an indirect recursive call or indirect recursion. 4 Presented & Prepared by: Mahmoud R. Alfarra Recursion Concepts Recursive Method Calls A Method Calls Indirect recursion

5  write a recursive program to perform a popular mathematical calculation.  Consider the factorial of a positive integer n, written n!, which is the product of n * (n-1)*(n-2)*….. *1  With 1! equal to 1 and 0! defined to be 1.  For example, 5! is the product 5 · 4 · 3 · 2 · 1, which is equal to 120. 5 Presented & Prepared by: Mahmoud R. Alfarra Example: Using Recursion: Factorials

6 6 Presented & Prepared by: Mahmoud R. Alfarra Example: Using Recursion: Factorials Solve this Problem using Iteration HW 18.1

7 7 Presented & Prepared by: Mahmoud R. Alfarra Example: Using Recursion: Factorials Calling of factorial method to calculate the factorial of numbers from 0 to 10

8 8 Presented & Prepared by: Mahmoud R. Alfarra Example: Using Recursion: Factorials

9  The Fibonacci series begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers. 9 Presented & Prepared by: Mahmoud R. Alfarra Example Using Recursion: Fibonacci Series fibonacci (n) = fibonacci (n-1)+ fibonacci (n-2) Solve this Problem using Iteration HW 18.2

10 10 Presented & Prepared by: Mahmoud R. Alfarra Example Using Recursion: Fibonacci Series

11 11 Presented & Prepared by: Mahmoud R. Alfarra Example Using Recursion: Fibonacci Series

12 12 Presented & Prepared by: Mahmoud R. Alfarra Be care Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the loop never terminates. Set a semi colon after the condition results a logic error

13 Recursion vs. Iteration  Both iteration and recursion are based on a control structure.  Iteration uses a repetition structure (such as for, while or do/while)  Recursion uses a selection structure (such as if, if/else or switch). 13 Presented & Prepared by: Mahmoud R. Alfarra

14 Recursion vs. Iteration  Both iteration and recursion involve repetition.  Iteration explicitly uses a repetition structure.  Recursion achieves repetition through repeated method calls.  Iteration and recursion each involve a termination test. 14 Presented & Prepared by: Mahmoud R. Alfarra

15 What is Pointers ?  A pointer is nothing but a variable that holds the memory address of another type. 15 Presented & Prepared by: Mahmoud R. Alfarra

16 Declaring a Pointer type  The general form of declaring a pointer type is as shown below 16 Presented & Prepared by: Mahmoud R. Alfarra type * variable_name; Where * is known as the de-reference operator int *x ;  This Declares a pointer variable x, which can hold the address of an int type. The reference operator (&) can be used to get the memory address of a variable. int x = 100; The &x gives the memory address of the variable x, which we can assign to a pointer variable int *ptr = & x ;

17 كان النبي صلى الله عليه وسلم إذا سمع الرعد أو رأى البرق يقول: (‏سبحان الذي يسبح الرعد بحمده و الملائكة من خيفته) 17 Presented & Prepared by: Mahmoud R. Alfarra

18 Practices 18 Presented & Prepared by: Mahmoud R. Alfarra


Download ppt "Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java."

Similar presentations


Ads by Google