Chapter4_3-Repetition

count count 1 and 3 exhibit off by one

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: 10 ){ 4 4 count = 0; while ( count < 10 ){ ... ... count++; count++; } } 1 and 3 exhibit off-by-one error. 1 3 Page 8 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP The do-while Statement int sum = 0, number = 1; do { sum += number; number++; These statements are These statements are executed as long as sum executed as long as sum is less than or equal to is less than or equal to 1,000,000. 1,000,000. } while ( sum <= 1000000 ); Page 9 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Syntax for the do-while do Statement <loop body> while ( <boolean expression> ) ; do { sum += number; loop body loop body number++; } while ( sum <= 1000000 ); Boolean Expression Boolean Expression Page 10 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Control Flow of do-while int sum = 0, number = 11 int sum = 0, number = sum += number; sum += number; number++; number++; ssum<= um <= 1000000 ? 1000000 ? true false Page 11 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = scanner.nextInt( ); sum += number; } These statements are These statements are executed for 20 times executed for 20 times ((i i= 0, 1, 2, … , ,19). = 0, 1, 2, … 19). Page 12 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Syntax for the for Statement for ( <initialization>; <boolean expression>; <increment> <loop body> Boolean Boolean Expression Expression Initialization Initialization f...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online