Presentation is loading. Please wait.

Presentation is loading. Please wait.

While and Do While Loops

Similar presentations


Presentation on theme: "While and Do While Loops"— Presentation transcript:

1 While and Do While Loops
By Ms. Bellacera

2 While Loop Form while (condition) { statement; } //executes an //unknown //number of times int z=0; while (z<10) { z=z+1; } cout<<“z is “<<z<<endl; What gets printed?

3 Do While Loop Form int num, sum=0; do { cout<<“enter the number”<<endl; cin<<num; sum=sum+num; } while (num!=0); cout<<“sum is “<<sum<<endl; do { statement; } while (condition); //executes //at least once What’s this? What gets printed?

4 While versus For Loops initialization while (condition) { statement incrementation } for (initialization; condition; incremenatation) { statement . }

5 While/For Loops Questions
1. Write the while loop equivalent to the following loop. double total = 0.0; double val; for (val=1.0; val<1000; val*=1.5) { total +=val; } 2. Write a for loop equivalent to the following while loop. int k=1; int sum=0; while (k<=num) sum+=k; k+=2;

6 While versus For Loops initialization
int z=0; while (z<10) { z++; } cout<<“z is“<<z<<endl; condition for (int z=0; z<10; z++) { } cout<<“z is “<<z<<endl; incrementation

7 Create the Factorial Program
Produce a listing of factorials starting at 0! Continue until the value of the factorial goes over 4,000,000. Show the factorial and its value on each iteration. (ie. 0! = 1 1! = 1 2! = 2 . )


Download ppt "While and Do While Loops"

Similar presentations


Ads by Google