1 / 18

Introduction To Programming Information Technology , 1’ st Semester

MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE. Using Java. Introduction To Programming Information Technology , 1’ st Semester . Lecture 18 Recursion & Pointers in Java . Teacher: Mahmoud Rafeek Alfarra. Outline. Recursion Concepts

erling
Download Presentation

Introduction To Programming Information Technology , 1’ st Semester

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Using Java Introduction To ProgrammingInformation Technology , 1’st Semester Lecture 18 Recursion & Pointers in Java Teacher: Mahmoud Rafeek Alfarra

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

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

  4. Recursion Concepts Calls Recursive Method A Method Indirect recursion Calls • 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. Presented & Prepared by: Mahmoud R. Alfarra 4

  5. Example: Using Recursion: Factorials 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. Presented & Prepared by: Mahmoud R. Alfarra 5

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

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

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

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

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

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

  12. 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 Presented & Prepared by: Mahmoud R. Alfarra 12

  13. Recursion vs. Iteration 13 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). 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. Presented & Prepared by: Mahmoud R. Alfarra 14

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

  16. Declaring a Pointer type 16 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 ; • The general form of declaring a pointer type is as shown below Presented & Prepared by: Mahmoud R. Alfarra

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

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

More Related