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

Introduction To Programming Information Technology , 1’ st Semester

  • 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

Page 1: Introduction To Programming Information Technology , 1’ st  Semester

Using Java

MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGYKHANYOUNIS- PALESTINE

Lecture 18Recursion & Pointers in Java

Page 2: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 3: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 4: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 5: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 6: Introduction To Programming Information Technology , 1’ st  Semester

6Presented & Prepared by: Mahmoud R. Alfarra

Example: Using Recursion: Factorials

Solve this Problem using Iteration HW 18.1

Page 7: Introduction To Programming Information Technology , 1’ st  Semester

7Presented & Prepared by: Mahmoud R. Alfarra

Example: Using Recursion: Factorials

Calling of factorial method to calculate the factorial of numbers from 0 to 10

Page 8: Introduction To Programming Information Technology , 1’ st  Semester

8Presented & Prepared by: Mahmoud R. Alfarra

Example: Using Recursion: Factorials

Page 9: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 10: Introduction To Programming Information Technology , 1’ st  Semester

10Presented & Prepared by: Mahmoud R. Alfarra

Example Using Recursion: Fibonacci Series

Page 11: Introduction To Programming Information Technology , 1’ st  Semester

11Presented & Prepared by: Mahmoud R. Alfarra

Example Using Recursion: Fibonacci Series

Page 12: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 13: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 14: Introduction To Programming Information Technology , 1’ st  Semester

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

Page 15: Introduction To Programming Information Technology , 1’ st  Semester

What is Pointers? A pointer is nothing but a variable that

holds the memory address of another type.

15Presented & Prepared by: Mahmoud R. Alfarra

Page 16: Introduction To Programming Information Technology , 1’ st  Semester

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 ;

Page 17: Introduction To Programming Information Technology , 1’ st  Semester

كان النبي صلى الله عليه وسلم إذا سمع الرعد أو رأى كان النبي صلى الله عليه وسلم إذا سمع الرعد أو رأى البرق يقول:البرق يقول:

(سبحان الذي يسبح (سبحان الذي يسبح الرعد بحمده و المالئكة الرعد بحمده و المالئكة

من خيفته)من خيفته)17

Presented & Prepared by: Mahmoud R. Alfarra

Page 18: Introduction To Programming Information Technology , 1’ st  Semester

PracticesPractices18

Presented & Prepared by: Mahmoud R. Alfarra