1 / 7

Java Methods

Java Methods. Chapter 8. Topics covered. For loops While loops Do-while loops “return” and “break”. loops. 3 conditions needed for a while loops 1. initialization of the test variable int i = 0 while( i <1) 2. testing of the condition before each loop

carlyn
Télécharger la présentation

Java Methods

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. Java Methods Chapter 8

  2. Topics covered • For loops • While loops • Do-while loops • “return” and “break”

  3. loops • 3 conditions needed for a while loops • 1. initialization of the test variable • inti = 0 • while(i<1) • 2. testing of the condition before each loop • 3. a potential change for the variable in the tested condition • i++;

  4. loops • Mechanics of a for loop • for(inti = 0; i<10; i++) • {…} • Initialization (inti =0) – only executed once • Condition (i<10) – executed before each pass • Change (i++) – executed at the end of each pass

  5. Do-while loop • Example: • do • { i++} • while(i<10); • The do-while loop differs from a while loop as the condition is tested after the body of the loop

  6. Lab – page 204 • Create a program that finds “perfect” numbers • Perfect number – equal to the sum of all of its divisors • Ex) 6 = 1+ 2 + 3 • Next 3: 28, 496, 8128 • 1st – write a program to find the 1st 4 perfect numbers • Research Mersenne primes and the info on page 204-205 to find the 5th perfect number

  7. HW • #2, 5, 9,12, 13, 14

More Related