Slide 1
Lecture 03
Slide 2
Loop Basics
Slide 3
Loop Types
Sentinel while do-while
Counting for
Slide 4
Sentinel Loops
Slide 5
while Syntax
while(test condition){ statement}
Slide 6
When a program encounters a while loop, the test condition is evaluated first. If the condition is TRUE, the program executes the body of the loop. The program then returns to the test condition and reevaluates. If the condition is still TRUE, the body executes again.
while Syntax
Slide 7
Example #1
while (7) cout << “hello” << endl;
Slide 8
Example #2#include<iostream>using namespace std;int main(){int input;cout <<"enter number between 5 and 89, inclusive: ";cin >> input;while (input < 5 || input > 89){cout << "that value is unacceptable... try again: ";cin >> input;}cout << "the value"<<input<< "is in the interval[5, 89]"<< endl; cin.get();//system("PAUSE");return 0;}
Slide 9
Example #3#include<iostream>using namespace std;int main(){long sum = 0;int counter = 1; while (counter<=100){sum += counter;counter++ ;}cout << "sum of first 100 integers is "<< sum ;cin.get();system("PAUSE");return 0;}
do-while Syntax
Do{ statement}while (test condition);
Copyright © 2003 Pearson Education, Inc. Slide 11
The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop.The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.
do-while Syntax
Slide 12
Example #1
do{cout << "Enter an integer between 2 and 174 (inclusive)"<< "that is a multiple of 3: ";cin >> input;} while (!(input >= 2 && input <= 174 && input%3 == 0));
Slide 13
Counting Loops
Slide 14
for Syntax
for (startExpression; testExpression; countExpression)} statement }
Slide 15
• The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in the startExpression(such as int x = 1;).
• This startExpression is evaluated only once at the beginning of the loop.
• The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of the loop repeats. When the testExpression becomes FALSE, the looping stops and the program continues with the statement immediately following the for loop body in the program code.
• The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value.
• Braces are not required if the body of the for loop consists of only ONE statement. Please indent the body of the loop for readability.
for Syntax
Slide 16
Example #1
int max;float average;long sum = 0;short i = 0;cout << "enter positive integer: ";cin >> max;for(i=0; i <= max; i++)sum += i;average = sum/max;cout << "average is " << average << endl;
Slide 17
Goal Output* * * * * * * * ** * ** * *
Example #2
Slide 18
Outputting 5 Lines
for (int i = 1; i <= 5; i++) { cout << endl;}
Slide 19
Outputting 5 Stars
for (int i = 1; i <= 5; i++) { cout << “* ”;}
Slide 20
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
output
i=1j=?
variable values
Slide 21
Combiningfor (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
output
i=1j=?
variable values
Slide 22
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
output
i=1j=1
variable values
Slide 23
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
*
output
i=1j=1
variable values
Slide 24
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
*
output
i=1j=2
variable values
Slide 25
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* *
output
i=1j=2
variable values
Slide 26
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* *
output
i=1j=3
variable values
Slide 27
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * *
output
i=1j=3
variable values
Slide 28
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * *
output
i=1j=4
variable values
Slide 29
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * *
output
i=1j=4
variable values
Slide 30
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * *
output
i=1j=5
variable values
Slide 31
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=5
variable values
Slide 32
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=6
variable values
Slide 33
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=6
variable values
Slide 34
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=2j=?
variable values
Slide 35
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=2j=1
variable values
Slide 36
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * **
output
i=2j=1
variable values
Slide 37
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * **
output
i=2j=2
variable values
Slide 38
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** *
output
i=2j=2
variable values
Slide 39
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** *
output
i=2j=3
variable values
Slide 40
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** * *
output
i=2j=3
variable values
Slide 41
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** * *
output
i=2j=4
variable values
Slide 42
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=2j=4
variable values
Slide 43
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=2j=5
variable values
Slide 44
Combining
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* ";
cout << endl;}
* * * * ** * * * *
output
i=2j=5
variable values
oops!!!
Slide 45
Goal vs Actual
Goal* * * * * * * * ** * ** * *
Actual* * * * * * * * * ** * * * ** * * * ** * * * *
j=1
j=2
j=3
j=4
j=5
i=1
i=2
i=3
i=4
i=5
Slide 46
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
output
i=1j=?
variable values
Slide 47
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
output
i=1j=1
variable values
Slide 48
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
*
output
i=1j=1
variable values
Slide 49
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
*
output
i=1j=2
variable values
Slide 50
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* *
output
i=1j=2
variable values
Slide 51
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* *
output
i=1j=3
variable values
Slide 52
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * *
output
i=1j=3
variable values
Slide 53
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * *
output
i=1j=4
variable values
Slide 54
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * *
output
i=1j=4
variable values
Slide 55
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=5
variable values
Slide 56
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=6
variable values
Slide 57
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=1j=6
variable values
Slide 58
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=2j=?
variable values
Slide 59
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * *
output
i=2j=1
variable values
Slide 60
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * **
output
i=2j=1
variable values
Slide 61
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * **
output
i=2j=2
variable values
Slide 62
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** *
output
i=2j=2
variable values
Slide 63
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** *
output
i=2j=3
variable values
Slide 64
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * *
output
i=2j=3
variable values
Slide 65
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * *
output
i=2j=4
variable values
Slide 66
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=2j=4
variable values
Slide 67
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=2j=5
variable values
Slide 68
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=2j=5
variable values
Slide 69
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=3j=?
variable values
Slide 70
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * *
output
i=3j=1
variable values
Slide 71
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * **
output
i=3j=1
variable values
Slide 72
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * **
output
i=3j=2
variable values
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** *
output
i=3j=2
variable values
Slide 74
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** *
output
i=3j=3
variable values
Slide 75
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * *
output
i=3j=3
variable values
Slide 76
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * *
output
i=3j=4
variable values
Slide 77
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * *
output
i=3j=4
variable values
Slide 78
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * *
output
i=4j=?
variable values
Slide 79
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * *
output
i=4j=1
variable values
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * *
output
i=4j=1
variable values
Slide 81
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * *
output
i=4j=2
variable values
Slide 82
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * *
output
i=4j=2
variable values
Slide 83
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * *
output
i=4j=3
variable values
Slide 84
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * *
output
i=4j=3
variable values
Slide 85
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * *
output
i=5j=?
variable values
Slide 86
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * *
output
i=5j=1
variable values
Slide 87
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * **
output
i=5j=1
variable values
Slide 88
Fixed
for (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * **
output
i=5j=2
variable values
Slide 89
Fixedfor (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * **
output
i=5j=2
variable values
Slide 90
Fixedfor (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * **
output
i=6j=?
variable values
Slide 91
Fixedfor (int i=1; i <= 5; i++){ for (int j=1; j <= 5 – i + 1; j++) cout << "* ";
cout << endl;}
* * * * ** * * ** * * * **
output
i=6j=?
variable values
Slide 92
* * * * * * * * * * * * * * *
Slide 93
Solution Rows
for (short i = 0; i < 5; i++){ for (short j = 1; j <= i; j++) cout << " ";
for (short j = i; j < 5; j++) cout << "* ";
cout << endl;}
* * * * *
* * * * * * * * *
*
Slide 94
Solution Spaces
for (short i = 0; i < 5; i++){ for (short j = 1; j <= i; j++) cout << " ";
for (short j = i; j < 5; j++) cout << "* ";
cout << endl;}
* * * * *
* * * * * * * * *
*
Slide 95
Solution Stars
for (short i = 0; i < 5; i++){ for (short j = 1; j <= i; j++) cout << " ";
for (short j = i; j < 5; j++) cout << "* ";
cout << endl;}
* * * * *
* * * * * * * * *
*
Slide 96
Random Number Generation
Slide 97
rand()
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
cout << rand() << endl;
return 0;
}
Slide 98
rand()#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
cout << rand() << endl;
return 0;
}
3845773388773849487724339944523740082314611833432237844
First Execution
Slide 99
rand()#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
cout << rand() << endl;
return 0;
}
3845773388773849487724339944523740082314611833432237844
First Execution
3845773388773849487724339944523740082314611833432237844
Second Execution
Slide 100
Pseudo-Random Generation
The numbers aren't actually random -- they're generated by a formula,
so they're just pseudorandom.
Slide 101
The pseudo-random number generator is initialized using the argument passed as seed.For every different seed value used in a call to srandsrand, the pseudo-random pseudo-random number generator number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.
In order to generate random-like numbers, srand srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime><ctime>). This is distinctive enough for most trivial randomization needs.
Slide 102
srand()
#include <iostream>#include <ctime> #include <cstdlib>using namespace std;int main(){ srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}
Slide 103
srand()#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){ srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}
46453735342236578889937237165742245778614
First Execution
Slide 104
srand()#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){ srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}
46453735342236578889937237165742245778614
First Execution
6877245768215576187851179738346117511735257868
Second Execution
Slide 105
limiting range: [0, MAX_RAND]
#include <iostream>#include <ctime>#include <cstdlib>
using namespace std;int main(){ long next_value; srand(time(NULL)); for (int i = 1; i <= 100000; i++) if (rand()%100 <= 34) cout << "hello" << endl; // ~35,000 times else cout << "goodbye" << endl; // ~65,000 times return 0;}
Slide 106
#include <iostream>using namespace std;void main (){int x, y;double z;if(typeid(x) == typeid(y))cout << "x and y are of same type."<<endl;elsecout<< "x and y are of different type. " <<endl;cout << "The type of z is "<< typeid(z).name()<<endl;cout<< "The type of y is "<< typeid(y).name() << endl;}
typeid ()
The expected output of the program is given below.x and y are of same type.The type of z is doubleThe type of y is int
The operator typeid () identifies the type of variable. Here we only show the use of the operator. It returns reference to type of its argument. It is coded as:
typeid(object);
Slide 107