Upload
erling
View
27
Download
0
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Using Java
MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGYKHANYOUNIS- PALESTINE
Lecture 18Recursion & Pointers in Java
Recursion Concepts Example Using Recursion: Factorials Example Using Recursion: Fibonacci
Series Recursion vs. IterationWhat is Pointers ? Emank X Mezank
2Presented & Prepared by: Mahmoud R. Alfarra
A recursive method is a method that calls itself. A recursive method can be called either directly, or
indirectly through another method.
3Presented & Prepared by: Mahmoud R. Alfarra
Recursion Concepts
MethodCalls
Direct recursion
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.
4Presented & Prepared by: Mahmoud R. Alfarra
Recursion Concepts
Recursive Method
Calls A Method
Calls
Indirect recursion
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)*….. *1n * (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.
5Presented & Prepared by: Mahmoud R. Alfarra
Example: Using Recursion: Factorials
6Presented & Prepared by: Mahmoud R. Alfarra
Example: Using Recursion: Factorials
Solve this Problem using Iteration HW 18.1
7Presented & Prepared by: Mahmoud R. Alfarra
Example: Using Recursion: Factorials
Calling of factorial method to calculate the factorial of numbers from 0 to 10
8Presented & Prepared by: Mahmoud R. Alfarra
Example: Using Recursion: Factorials
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.
9Presented & 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
10Presented & Prepared by: Mahmoud R. Alfarra
Example Using Recursion: Fibonacci Series
11Presented & Prepared by: Mahmoud R. Alfarra
Example Using Recursion: Fibonacci Series
12Presented & Prepared by: Mahmoud R. Alfarra
Be care
Not providing, in the body of a while statement, an action Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become that eventually causes the condition in the while to become false normally results in a false normally results in a logic error logic error called an called an infinite loopinfinite loop, , in which the loop never terminates.in which the loop never terminates.
Set a semi colon after the condition results a logic error Set a semi colon after the condition results a logic error
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).
13Presented & Prepared by: Mahmoud R. Alfarra
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.
14Presented & Prepared by: Mahmoud R. Alfarra
What is Pointers? A pointer is nothing but a variable that
holds the memory address of another type.
15Presented & Prepared by: Mahmoud R. Alfarra
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
Presented & Prepared by: Mahmoud R. Alfarra
PracticesPractices18
Presented & Prepared by: Mahmoud R. Alfarra