LAB 7 while loop do while loop Introduction

LAB 7 while loop do while loop Introduction to Algorithms and Programming COMP 151

while loop Condition F T Statement list while (Condition) { Statement list }

Trace while Loop int count = 0; Initialize count while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

int count = 0; (count < 2) is true while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

int count = 0; Print Welcome to C++ while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

int count = 0; Increase count by 1 count is 1 now while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

int count = 0; (count < 2) is still true since count is 1 while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

int count = 0; Print Welcome to C++ while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Increase count by 1 count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

(count < 2) is false since count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

The loop exits. Execute the next statement after the loop. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Important Note When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. condition that will become false at some point count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 1 1 2 3 4 Write C++ program to sum the 5 6 numbers from 1 to 20 using 7 8 while loop 9 10 11 12 13 14 15 16 17 18 19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson 20 Education, Inc. All rights reserved. 0136097200 sum? ?

#include<iostream> using namespace std; int main() { int number=1, sum=0; while(number<=20) { sum=sum+number; number=number+1; } cout<<"The sum is"<<sum<<endl; return(0); } output: The sum is 210 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 starting numbers of the series Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 0+1= Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 1+1= Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 1+3= Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 2+3= Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Step 1 sum N 1 0 1 1 2 3 5 8 13 21 34 N 2 Step 2 sum N 1 0 1 1 2 3 5 8 13 21 34 N 2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Step 3 sum N 1 0 1 1 2 3 5 8 13 21 34 N 2 Step 4 sum N 1 0 1 1 2 3 5 8 13 21 34 N 2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

#include<iostream> using namespace std; int main() { int n 1=0, n 2=1, sum=0, n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<n 1<<"t"<<n 2; while(n>2) { sum=n 1+n 2; cout<<"t"<<sum; n 1=n 2; n 2=sum; n=n-1; } return(0); } output: Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 3 Write C++ program to find the factorial of a given number using while loop Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

#include<iostream> using namespace std; int main() { int number, fact=1; cout<<"Enter the number"<<endl; cin>>number; while(number>0) { fact=fact*number; number=number-1; } cout<<"The factorial of the number is"<<fact<<endl; return(0); } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

do while loop do { // Loop body; Statement(s); } while (loop-continuation-condition); Important note: do while loop guarantees at least one execution of the body Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 4 1 2 3 4 Write C++ program to sum the 5 6 numbers from 1 to 20 using do 7 8 while loop 9 10 11 12 13 14 15 16 17 18 19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson 20 Education, Inc. All rights reserved. 0136097200 sum? ?

#include<iostream> using namespace std; int main() { int number=1, sum=0; do { sum=sum+number; number=number+1; } while(number<=20); cout<<"The sum is"<<sum<<endl; return(0); Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 5 Write C++ program to print the Fibonacci series using do while loop 0 1 1 2 3 5 8 13 21 34 starting numbers of the series Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

#include<iostream> using namespace std; int main() { int num 1=0, num 2=1, sum=0, n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<num 1<<"t"<<num 2; do { sum=num 1+num 2; cout<<"t"<<sum; num 1=num 2; num 2=sum; n=n-1; } while(n>2); return(0); } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Exercise 6 Write C++ program to find the factorial of a given number using do while loop Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

#include<iostream> using namespace std; int main() { int number, fact=1; cout<<"Enter the number"<<endl; cin>>number; do { fact=fact*number; number=number-1; } while(number>0); cout<<"The factorial of the number is"<<fact<<endl; return(0); } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200
- Slides: 31