FP 201 - Unit 3 Part 2
-
Upload
rohassanie -
Category
Documents
-
view
332 -
download
2
Transcript of FP 201 - Unit 3 Part 2
![Page 1: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/1.jpg)
Unit 3.2 Understand Loop Control Structures
![Page 2: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/2.jpg)
At the end of this presentation, students will be able to:◦ Understand looping control structures◦ Describe the structure and working of for,
while and do-while loops◦ Explain the need for break, continue, return
and goto statements
![Page 3: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/3.jpg)
Looping statements are used to :◦ execute a set of instructions repeatedly, as long
as the specific condition is satisfied.
The loop in C++ comes in 3 forms:◦ for ◦ while◦ do-while
![Page 4: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/4.jpg)
Syntax: for(initialization;condition; incrementation/decrementation)
{ //loop statements
}
Example:for (i=0; i<5; i++)
{cout << “\n” << i ;}
![Page 5: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/5.jpg)
initialization◦ refer to initial value of loop counter◦ carried out just once at the beginning of loop
condition(expression)◦ determined whether loop should continue◦ if expression is false the loop will be terminate
Incrementation/decrementation◦ The initial value of the loop control variable is either
incremented or decremented each time the loop gets executed.
![Page 6: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/6.jpg)
The program LoopDemo.cpp illustrates the working of a for loop.
![Page 7: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/7.jpg)
#include<iostream>using namespace std;
int main(){
int num;for (num = 1; num <= 10; num++){
cout<<num; }
return 0;}
12345678910
![Page 8: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/8.jpg)
There are a few patterns that you often need: ◦ To go from zero to some maximum – 1:
for (i = 0; i < max; ++i) ◦ Or, in the opposite direction:
for (i = max - 1; i >= 0; --i) ◦ Or To go from 1 to some maximum:
for (i = 1; i <= max; ++i) ◦ Or, in the opposite direction:
for (i = max; i > 0; --i) You might sometimes need a combination of elements from
more than one of these patterns, such as for (i = 0; i <= max; i++), but if you write something unusual like this, make sure that it really is what you want.
![Page 9: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/9.jpg)
Is a looping statement that enables you to repeat a set of instructions based on a condition.
If the condition is TRUE executes loop statement(s) in the while block and executing its block until the condition is FALSE.
![Page 10: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/10.jpg)
Syntax:
<initialise variable>while(condition){ //loop statements<Increment/decrement variable>;
}
Expression is evaluated first◦ If expression = TRUE then statement is executed◦ If expression = FALSE then statement is bypassed
![Page 11: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/11.jpg)
Example:while ((amount >0 && amount <=balance) {
balance = balance – amount;cout << “Please take your money” << amount;cout << “This is your balance” << balance;
}
![Page 12: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/12.jpg)
Program While1.cpp illustrates the working of a while loop.
![Page 13: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/13.jpg)
#include<iostream>using namespace std;
int main(){
int x=1;while(x<=11){
cout<<x<<" ";x=x+2;
}return 0;
}
1 3 5 7 9 11
![Page 14: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/14.jpg)
The do-while loop is similar to the while loop.
The difference only in the location where the condition is checked. In do-while, condition is checked at the end of loop.
Syntax
<initialise variable> do { //loop statements
<Increment/decrement variable>; } while(condition);
![Page 15: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/15.jpg)
Example
do {cout << “Enter a value";cin >> i;if ( i<=0){ cout << “This is negative
number";}
} while (i>0);
![Page 16: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/16.jpg)
Program DoWhileDemo.cpp illustrates the working of a do-while loop.
![Page 17: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/17.jpg)
#include<iostream>using namespace std;
int main(){
int x=1;do{
cout<<x<<" ";x=x+2;
}while(x<=11);return 0;
}
1 3 5 7 9 11
![Page 18: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/18.jpg)
nois i > 10?
print i
i = i+1
is i > 10?
print i
i = i+1
do-whilewhile
yes
no
yes
![Page 19: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/19.jpg)
Allow program controls to transfer from one part to another part of program unconditionally
4 types of jump statement:◦ break◦ continue◦ return◦ goto
![Page 20: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/20.jpg)
break
◦ The break statement causes termination of the loop and transfers the control outside the loop.
◦ Program BreakDemo.cpp illustrates the use of break statement.
![Page 21: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/21.jpg)
#include<iostream>using namespace std;int main(){
int i; cout<<"Entering into the loop\n";for(i=1;i<20;i++)
{if(i==10)break; // exit from for loopcout<<i<<" ";
}cout<<"\nExiting out of the loop\n";
return 0;}
Entering into the loop1 2 3 4 5 6 7 8 9 Exiting out of the loop
![Page 22: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/22.jpg)
continue◦ can only be used inside a loop.
◦ The continue statement will transfer the control to the beginning of the loop.
◦ Program ContinueDemo.java illustrates the use of continue statement.
![Page 23: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/23.jpg)
#include<iostream>using namespace std;int main(){
for(int i=1;i<=10;i++){
if(i%2==0)continue; cout<<i<<" ";
}return 0;
}
1 3 5 7 9
The continue statement is executed when i%2==0. The continue statement ends the current iteration so that the rest of the statement in the loop body is not executed; therefore, i is printed when i%2!=0.
![Page 24: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/24.jpg)
return ◦ terminate execution of current function AND
return value contained in the expression to the function that invoked it.
Example:float CalcSum() { float sum; sum = 15 + 20; return sum; }
![Page 25: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/25.jpg)
goto ◦ useful when you want to exit from a deeply
nested loop.◦ control flow statement that causes the CPU to
jump to another spot in the code.
Syntaxgoto identifier ;
Example:goto tryAgain;
![Page 26: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/26.jpg)
#include <iostream>#include <cmath>using namespace std;void main(){
tryAgain: // this is a statement labeldouble number;
cout << "Enter a non-negative number: "; cin >> number;
if (number < 0.0) goto tryAgain; // this is the goto statement cout << "The sqrt of " << number << " is " << sqrt(number) <<
endl;}
![Page 27: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/27.jpg)
In this presentation, you learnt the following:
Looping statements are used to execute a set of instructions repeatedly, as long as the specific condition is satisfied.
In C++, the looping control structures include while, do-while and for.
The break statement will transfer the control to the statement outside the loop.
![Page 28: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/28.jpg)
The continue statement will transfer the control to the beginning of the loop.
The return statement will terminate execution of current function and return the value.
The goto may be necessary for exiting a loop from within a deeply nested loop.
![Page 29: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/29.jpg)
1. Consider the following code fragment:
cin >> x >> y; if ((x >= 3) && (y <= 7))
cout << "A"; else cout << "B"; if ((x >= 3) || (y <= 7)) cout << "C"; else cout << "D";
if (( x > 3) && (y < 7)) cout << "E"; else cout << "F"; if (( x > 3) || (y < 7)) cout << "G"; else
cout << "H";
◦ What is the output produced if the user enters: 3 and 7 3 and 6 4 and 7 4 and 8
![Page 30: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/30.jpg)
3 and 7ACFH
3 and 6ACFG
4 and 7 ACFG
4 and 8BCFG
![Page 31: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/31.jpg)
2. What is the output?
#include<iostream>using namespace std;int main(){
int sum=0, item=0;while (item<5) {
item++;if(item==2)
continue;sum+=item;
}cout<<"The sum is"<<sum;
}
![Page 32: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/32.jpg)
item<5
0 1 2 3 4
item++
1 2 3 4 5
if(item==2) continue;sum+=item;
1 4 8 13
![Page 33: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/33.jpg)
3. Write a program that can produce an output as below by using the for, while, and do-while loop
8642
![Page 34: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/34.jpg)
for loop//solution 1#include<iostream>using namespace std;int main(){
for(int k=4; k>=1;k--)cout<<k*2<<endl;
return 0;}
![Page 35: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/35.jpg)
//solution 2#include<iostream>using namespace std;int main(){
for(int k=8; k>=1;k--){
cout<<k<<endl;k--;
}return 0;}
![Page 36: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/36.jpg)
while loop//solution 1#include<iostream>using namespace std;int main(){
int k=4; while(k>=1){
cout<<k*2<<endl;k--;
}return 0;}
![Page 37: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/37.jpg)
//solution 2#include<iostream>using namespace std;int main(){
int k=8;while(k>=1){
cout<<k<<endl;k--;k--;
}return 0;}
![Page 38: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/38.jpg)
do-while loop//solution 1#include<iostream>using namespace std;int main(){
int k=4; do{
cout<<k*2<<endl;k--;
}while(k>=1);return 0;}
![Page 39: FP 201 - Unit 3 Part 2](https://reader034.fdocuments.net/reader034/viewer/2022042518/555e36b2d8b42a63048b49d8/html5/thumbnails/39.jpg)
//solution 2#include<iostream>using namespace std;int main(){
int k=8;do{
cout<<k<<endl;k--;k--;
}while(k>=1);return 0;}