Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms and Programming COMP151

Similar presentations


Presentation on theme: "Introduction to Algorithms and Programming COMP151"— Presentation transcript:

1 Introduction to Algorithms and Programming COMP151
LAB 7 while loop do while loop Introduction to Algorithms and Programming COMP151

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

3 Trace while Loop int count = 0; while (count < 2) {
Initialize count int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

4 cout << "Welcome to C++!"; count++; }
(count < 2) is true int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

5 cout << "Welcome to C++!"; count++; }
Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

6 cout << "Welcome to C++!"; count++; }
Increase count by 1 count is 1 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

7 (count < 2) is still true since count is 1
int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

8 cout << "Welcome to C++!"; count++; }
Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

9 Increase count by 1 count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

10 (count < 2) is false since count is 2 now
int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

11 The loop exits. Execute the next statement after the loop.
int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

12 condition that will become false at some point
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 int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

13 Write C++ program to sum the numbers from 1 to 20 using while loop
Exercise 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Write C++ program to sum the numbers from 1 to 20 using while loop sum??

14 #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 is210

15 starting numbers of the series
Exercise 2 Write C++ program to print the Fibonacci series using while loop starting numbers of the series

16 Write C++ program to print the Fibonacci series using while loop
Exercise 2 Write C++ program to print the Fibonacci series using while loop 0+1=

17 Write C++ program to print the Fibonacci series using while loop
Exercise 2 Write C++ program to print the Fibonacci series using while loop 1+1=

18 Write C++ program to print the Fibonacci series using while loop
Exercise 2 Write C++ program to print the Fibonacci series using while loop 1+3=

19 Write C++ program to print the Fibonacci series using while loop
Exercise 2 Write C++ program to print the Fibonacci series using while loop 2+3=

20 Step 1 N1 N2 sum Step 2 N1 N2 sum

21 Step 3 N1 N2 sum Step 4 N1 N2 sum

22 #include<iostream>
using namespace std; int main() { int n1=0,n2=1,sum=0,n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<n1<<"\t"<<n2; while(n>2) sum=n1+n2; cout<<"\t"<<sum; n1=n2; n2=sum; n=n-1; } return(0); output:

23 Exercise 3 Write C++ program to find the factorial of a given number using while loop

24 #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);

25 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

26 Write C++ program to sum the numbers from 1 to 20 using do while loop
Exercise 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Write C++ program to sum the numbers from 1 to 20 using do while loop sum??

27 #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);

28 starting numbers of the series
Exercise 5 Write C++ program to print the Fibonacci series using do while loop starting numbers of the series

29 #include<iostream>
using namespace std; int main() { int num1=0,num2=1,sum=0,n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<num1<<"\t"<<num2; do sum=num1+num2; cout<<"\t"<<sum; num1=num2; num2=sum; n=n-1; } while(n>2); return(0);

30 Exercise 6 Write C++ program to find the factorial of a given number using do while loop

31 #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);


Download ppt "Introduction to Algorithms and Programming COMP151"

Similar presentations


Ads by Google