6. 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
8. 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;
}
9. 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;
}
12. 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));
15. 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
16. 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;
101. 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-randompseudo-random
number generatornumber 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, srandsrand 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.
102. 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;
}
103. 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;
}
464
53735
342
23
6578
889
93723
7165
7422457
78614
First Execution
104. 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;
}
464
53735
342
23
6578
889
93723
7165
7422457
78614
First Execution
6877
245768
215
57618
78511
79738
3461
175117
35
257868
Second Execution
105. 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;
}
106. 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;
else
cout<< "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 double
The 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);