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

Slides:



Advertisements
Similar presentations
Recursion Recursive Thinking Recursive Programming
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 6 Introduction to classes and objects.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
M180: Data Structures & Algorithms in Java
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Introduction To Programming Information Technology , 1’st Semester
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Chapter Topics Chapter 16 discusses the following main topics:
Presented By: Mahmoud Rafeek Alfarra
RECURSION.
Chapter 3: Decisions and Loops
Presented By: Mahmoud Rafeek Alfarra
10.3 Details of Recursion.
JavaScript: Control Statements I
Recursion, Tail Recursion
MSIS 655 Advanced Business Applications Programming
Introduction To Programming Information Technology , 1’st Semester
Outline Altering flow of control Boolean expressions
Presented By: Mahmoud Rafeek Alfarra
Introduction To Programming Information Technology , 1’st Semester
3 Control Statements:.
Introduction To Programming Information Technology , 1’st Semester
Computer Science Core Concepts
Presented By: Mahmoud Rafeek Alfarra
Introduction To Programming Information Technology , 1’st Semester
Dr. Sampath Jayarathna Cal Poly Pomona
Programming Fundamentals Lecture #7 Functions
Presented By: Mahmoud Rafeek Alfarra
Presentation transcript:

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

 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

 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

 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

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

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

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

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

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

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

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

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

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

Practices 18 Presented & Prepared by: Mahmoud R. Alfarra