Introduction to Algorithms and Programming COMP151 LAB 7 while loop do while loop Introduction to Algorithms and Programming COMP151
while loop Statement Condition list while (Condition) { Statement list } Statement list T F
Trace while Loop int count = 0; while (count < 2) { Initialize count int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
cout << "Welcome to C++!"; count++; } (count < 2) is true int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
cout << "Welcome to C++!"; count++; } Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
cout << "Welcome to C++!"; count++; } Increase count by 1 count is 1 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
(count < 2) is still true since count is 1 int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
cout << "Welcome to C++!"; count++; } Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
Increase count by 1 count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
(count < 2) is false since count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
The loop exits. Execute the next statement after the loop. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }
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++; }
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??
#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
starting numbers of the series 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
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 1 2 3 5 8 13 21 34 0+1=
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 1 2 3 5 8 13 21 34 1+1=
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 1 2 3 5 8 13 21 34 1+3=
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 1 2 3 5 8 13 21 34 2+3=
Step 1 0 1 1 2 3 5 8 13 21 34 N1 N2 sum Step 2 0 1 1 2 3 5 8 13 21 34 N1 N2 sum
Step 3 0 1 1 2 3 5 8 13 21 34 N1 N2 sum Step 4 0 1 1 2 3 5 8 13 21 34 N1 N2 sum
#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:
Exercise 3 Write C++ program to find the factorial of a given number using while loop
#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);
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
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??
#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);
starting numbers of the series 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
#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);
Exercise 6 Write C++ program to find the factorial of a given number using do while loop
#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);