Chapter4_3-Repetition

Equal to 100 page 4 dr s gannouni dr a touir

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: er is less than or equal to 100. equal to 100. } Page 4 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Syntax for the while Statement while ( <boolean expression> ) <loop body> Boolean Expression Boolean Expression while ( sum number <= 100 = ){ sum + number; loop body loop body number = number + 1; } Page 5 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Control Flow of while int sum = 0, number = 11 int sum = 0, number = number <= number <= 100 ? 100 ? false true sum = sum + number; sum = sum + number; number = number + 1; number = number + 1; Page 6 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP More Examples 1 1 int sum = 0, number = 1; while ( sum <= 1000000 ) { sum = sum + number; Keeps adding the Keeps adding the numbers 1, 2, 3, … until numbers 1, 2, 3, … until the sum becomes larger the sum becomes larger than 1,000,000. than 1,000,000. number = number + 1; } 2 2 int product = 1, number = 1, count = 20, lastNumber; Computes the product of Computes the product of the first 20 odd integers. the first 20 odd integers. lastNumber = 2 * count - 1; while (number <= lastNumber) { product = product * number; number = number + 2; } Page 7 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Loop Logical Errors • Goal: Execute the loop body 10 times. 1 count = 1; 2 count = 1; 1 2 while ( count < 10 ){ while ( count <= 10 ){ ... ... count++; count++; } 3 3 } count = 0; while ( count <=...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online