Chapter4_3-Repetition

Nextint sum number i loop body loop body page

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: or ( i=0 ; i < 20 IIncrement// ncrement Decrement Decrement ; number = scanner.nextInt(); sum += number; i++ ){ loop body loop body } Page 13 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP ) Control Flow of for i i= 0; = 0; false i i< 20 ? < 20 ? true number = . .. .. .; ; number = sum sum += number; += number; i i++; ++; Page 14 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP More for Loop Examples 1 1 for (int i = 0; i < 100; i += 5) i i= 0, 5, 10, … ,,95 = 0, 5, 10, … 95 2 2 for (int j = 2; j < 40; j *= 2) j j= 2, 4, 8, 16, 32 = 2, 4, 8, 16, 32 3 3 for (int k = 100; k > 0; k--) ) k = 100, 99, 98, 97, ..., 1 k = 100, 99, 98, 97, ..., 1 Page 15 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP The Nested-for Statement • Nesting a for statement inside another for statement is commonly used technique in programming. • Let’s generate the following table using nested-for statement. Page 16 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Generating the Table int price; for (int width = 11; width <=20, width++){ INNER OUTER for (int length = 5, length <=25, length+=5){ price = width * length * 19; //$19 per sq. ft. System.out.print (“ “ + price); } //finished one row; move on to next row System.out.println(“”); } Page 17 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online