100 likes | 392 Views
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
E N D
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 • 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++;
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
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
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
HW • #2, 5, 9,12, 13, 14