SlideShare a Scribd company logo
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=1
j=?
variable values
Slide 21
Combining
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << "* ";
cout << endl;
}
output
i=1
j=?
variable values
Slide 22
Combining
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << "* ";
cout << endl;
}
output
i=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=2
j=?
variable values
Slide 35
Combining
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << "* ";
cout << endl;
}
* * * * *
output
i=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=4
variable values
Combining
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << "* ";
cout << endl;
}
* * * * *
* * * *
output
i=2
j=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=2
j=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=1
j=?
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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=1
j=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=2
j=?
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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=2
j=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=3
j=?
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=3
j=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=3
j=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=3
j=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=3
j=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=3
j=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=3
j=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=3
j=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=3
j=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=4
j=?
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=4
j=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=4
j=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=4
j=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=4
j=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=4
j=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=4
j=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=5
j=?
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=5
j=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=5
j=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=5
j=2
variable values
Slide 89
Fixed
for (int i=1; i <= 5; i++)
{
for (int j=1; j <= 5 – i + 1; j++)
cout << "* ";
cout << endl;
}
* * * * *
* * * *
* * *
* *
*
output
i=5
j=2
variable values
Slide 90
Fixed
for (int i=1; i <= 5; i++)
{
for (int j=1; j <= 5 – i + 1; j++)
cout << "* ";
cout << endl;
}
* * * * *
* * * *
* * *
* *
*
output
i=6
j=?
variable values
Slide 91
Fixed
for (int i=1; i <= 5; i++)
{
for (int j=1; j <= 5 – i + 1; j++)
cout << "* ";
cout << endl;
}
* * * * *
* * * *
* * *
* *
*
output
i=6
j=?
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;
}
38457
733887
7384
94877
243
3994452
374008
23146
11833432
237844
First Execution
Slide 99
rand()
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
cout << rand() << endl;
return 0;
}
38457
733887
7384
94877
243
3994452
374008
23146
11833432
237844
First Execution
38457
733887
7384
94877
243
3994452
374008
23146
11833432
237844
Second Executio
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-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.
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;
}
464
53735
342
23
6578
889
93723
7165
7422457
78614
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;
}
464
53735
342
23
6578
889
93723
7165
7422457
78614
First Execution
6877
245768
215
57618
78511
79738
3461
175117
35
257868
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;
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);
Slide 107

More Related Content

What's hot

All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programs
Gouthaman V
 
Modeling FSMs
Modeling FSMsModeling FSMs
Modeling FSMs
Mohamed Samy
 
C++: Interface as Concept
C++: Interface as ConceptC++: Interface as Concept
C++: Interface as Concept
Jussi Pohjolainen
 
Reversible booth ppt
Reversible booth pptReversible booth ppt
Reversible booth ppt
Rahul Krishnamurthy
 
Chap08alg
Chap08algChap08alg
Chap08alg
Munkhchimeg
 
Functional C++
Functional C++Functional C++
Functional C++
Kevlin Henney
 
Verilogforlab
VerilogforlabVerilogforlab
Verilogforlab
Shankar Bhukya
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
MVC meets Monad
MVC meets MonadMVC meets Monad
MVC meets Monad
Gianluca Aguzzi
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
corehard_by
 
Building Hierarchy
Building HierarchyBuilding Hierarchy
Building Hierarchy
Mohamed Samy
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
Kevlin Henney
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
Shai Geva
 
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Codemotion
 
Introduction to AspectJ
Introduction to AspectJIntroduction to AspectJ
Introduction to AspectJ
mukhtarhudaya
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
brweber2
 
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
Istanbul Tech Talks
 
Los dskn
Los dsknLos dskn
Los dskn
Brenda Jazmin
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 
Circles graphic
Circles graphicCircles graphic
Circles graphic
alldesign
 

What's hot (20)

All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programs
 
Modeling FSMs
Modeling FSMsModeling FSMs
Modeling FSMs
 
C++: Interface as Concept
C++: Interface as ConceptC++: Interface as Concept
C++: Interface as Concept
 
Reversible booth ppt
Reversible booth pptReversible booth ppt
Reversible booth ppt
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Functional C++
Functional C++Functional C++
Functional C++
 
Verilogforlab
VerilogforlabVerilogforlab
Verilogforlab
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
MVC meets Monad
MVC meets MonadMVC meets Monad
MVC meets Monad
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
Building Hierarchy
Building HierarchyBuilding Hierarchy
Building Hierarchy
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
 
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
 
Introduction to AspectJ
Introduction to AspectJIntroduction to AspectJ
Introduction to AspectJ
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
 
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
 
Los dskn
Los dsknLos dskn
Los dskn
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Circles graphic
Circles graphicCircles graphic
Circles graphic
 

Similar to 2621008 - C++ 3

Ch4
Ch4Ch4
Introduction to ES2015
Introduction to ES2015Introduction to ES2015
Introduction to ES2015
kiranabburi
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
? ?
 
Midterm cpp q_a_may_2014(2)
Midterm cpp q_a_may_2014(2)Midterm cpp q_a_may_2014(2)
Midterm cpp q_a_may_2014(2)
Mzr Zia
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Lecture04
Lecture04Lecture04
Lecture04
elearning_portal
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
جامعة القدس المفتوحة
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
Nattawut Phetmak
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
Anne Lee
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
alish sha
 
Advanced pointer
Advanced pointerAdvanced pointer
Advanced pointer
Rubal Bansal
 
C++20 features
C++20 features C++20 features
C++20 features
LogeekNightUkraine
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
Programming Homework Help
 

Similar to 2621008 - C++ 3 (20)

Ch4
Ch4Ch4
Ch4
 
Introduction to ES2015
Introduction to ES2015Introduction to ES2015
Introduction to ES2015
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
Midterm cpp q_a_may_2014(2)
Midterm cpp q_a_may_2014(2)Midterm cpp q_a_may_2014(2)
Midterm cpp q_a_may_2014(2)
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Lecture04
Lecture04Lecture04
Lecture04
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Qno 1 (d)
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Advanced pointer
Advanced pointerAdvanced pointer
Advanced pointer
 
C++20 features
C++20 features C++20 features
C++20 features
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 

Recently uploaded

Osteoporosis - Definition , Evaluation and Management .pdf
Osteoporosis - Definition , Evaluation and Management .pdfOsteoporosis - Definition , Evaluation and Management .pdf
Osteoporosis - Definition , Evaluation and Management .pdf
Jim Jacob Roy
 
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptx
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptxMuscles of Mastication by Dr. Rabia Inam Gandapore.pptx
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptx
Dr. Rabia Inam Gandapore
 
Artificial Intelligence Symposium (THAIS)
Artificial Intelligence Symposium (THAIS)Artificial Intelligence Symposium (THAIS)
Artificial Intelligence Symposium (THAIS)
Josep Vidal-Alaball
 
Journal Article Review on Rasamanikya
Journal Article Review on RasamanikyaJournal Article Review on Rasamanikya
Journal Article Review on Rasamanikya
Dr. Jyothirmai Paindla
 
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.GawadHemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
NephroTube - Dr.Gawad
 
Diabetic nephropathy diagnosis treatment
Diabetic nephropathy diagnosis treatmentDiabetic nephropathy diagnosis treatment
Diabetic nephropathy diagnosis treatment
arahmanzai5
 
Histololgy of Female Reproductive System.pptx
Histololgy of Female Reproductive System.pptxHistololgy of Female Reproductive System.pptx
Histololgy of Female Reproductive System.pptx
AyeshaZaid1
 
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
Donc Test
 
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptxEar and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
Dr. Rabia Inam Gandapore
 
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
Oleg Kshivets
 
OCT Training Course for clinical practice Part 1
OCT Training Course for clinical practice Part 1OCT Training Course for clinical practice Part 1
OCT Training Course for clinical practice Part 1
KafrELShiekh University
 
Cardiac Assessment for B.sc Nursing Student.pdf
Cardiac Assessment for B.sc Nursing Student.pdfCardiac Assessment for B.sc Nursing Student.pdf
Cardiac Assessment for B.sc Nursing Student.pdf
shivalingatalekar1
 
Top-Vitamin-Supplement-Brands-in-India List
Top-Vitamin-Supplement-Brands-in-India ListTop-Vitamin-Supplement-Brands-in-India List
Top-Vitamin-Supplement-Brands-in-India List
SwisschemDerma
 
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
rishi2789
 
Ketone bodies and metabolism-biochemistry
Ketone bodies and metabolism-biochemistryKetone bodies and metabolism-biochemistry
Ketone bodies and metabolism-biochemistry
Dhayanithi C
 
Integrating Ayurveda into Parkinson’s Management: A Holistic Approach
Integrating Ayurveda into Parkinson’s Management: A Holistic ApproachIntegrating Ayurveda into Parkinson’s Management: A Holistic Approach
Integrating Ayurveda into Parkinson’s Management: A Holistic Approach
Ayurveda ForAll
 
Top Effective Soaps for Fungal Skin Infections in India
Top Effective Soaps for Fungal Skin Infections in IndiaTop Effective Soaps for Fungal Skin Infections in India
Top Effective Soaps for Fungal Skin Infections in India
SwisschemDerma
 
Chapter 11 Nutrition and Chronic Diseases.pptx
Chapter 11 Nutrition and Chronic Diseases.pptxChapter 11 Nutrition and Chronic Diseases.pptx
Chapter 11 Nutrition and Chronic Diseases.pptx
Earlene McNair
 
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdfCHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
rishi2789
 
Efficacy of Avartana Sneha in Ayurveda
Efficacy of Avartana Sneha in AyurvedaEfficacy of Avartana Sneha in Ayurveda
Efficacy of Avartana Sneha in Ayurveda
Dr. Jyothirmai Paindla
 

Recently uploaded (20)

Osteoporosis - Definition , Evaluation and Management .pdf
Osteoporosis - Definition , Evaluation and Management .pdfOsteoporosis - Definition , Evaluation and Management .pdf
Osteoporosis - Definition , Evaluation and Management .pdf
 
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptx
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptxMuscles of Mastication by Dr. Rabia Inam Gandapore.pptx
Muscles of Mastication by Dr. Rabia Inam Gandapore.pptx
 
Artificial Intelligence Symposium (THAIS)
Artificial Intelligence Symposium (THAIS)Artificial Intelligence Symposium (THAIS)
Artificial Intelligence Symposium (THAIS)
 
Journal Article Review on Rasamanikya
Journal Article Review on RasamanikyaJournal Article Review on Rasamanikya
Journal Article Review on Rasamanikya
 
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.GawadHemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
Hemodialysis: Chapter 4, Dialysate Circuit - Dr.Gawad
 
Diabetic nephropathy diagnosis treatment
Diabetic nephropathy diagnosis treatmentDiabetic nephropathy diagnosis treatment
Diabetic nephropathy diagnosis treatment
 
Histololgy of Female Reproductive System.pptx
Histololgy of Female Reproductive System.pptxHistololgy of Female Reproductive System.pptx
Histololgy of Female Reproductive System.pptx
 
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
TEST BANK For Community Health Nursing A Canadian Perspective, 5th Edition by...
 
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptxEar and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
Ear and its clinical correlations By Dr. Rabia Inam Gandapore.pptx
 
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
Local Advanced Lung Cancer: Artificial Intelligence, Synergetics, Complex Sys...
 
OCT Training Course for clinical practice Part 1
OCT Training Course for clinical practice Part 1OCT Training Course for clinical practice Part 1
OCT Training Course for clinical practice Part 1
 
Cardiac Assessment for B.sc Nursing Student.pdf
Cardiac Assessment for B.sc Nursing Student.pdfCardiac Assessment for B.sc Nursing Student.pdf
Cardiac Assessment for B.sc Nursing Student.pdf
 
Top-Vitamin-Supplement-Brands-in-India List
Top-Vitamin-Supplement-Brands-in-India ListTop-Vitamin-Supplement-Brands-in-India List
Top-Vitamin-Supplement-Brands-in-India List
 
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
CHEMOTHERAPY_RDP_CHAPTER 2 _LEPROSY.pdf1
 
Ketone bodies and metabolism-biochemistry
Ketone bodies and metabolism-biochemistryKetone bodies and metabolism-biochemistry
Ketone bodies and metabolism-biochemistry
 
Integrating Ayurveda into Parkinson’s Management: A Holistic Approach
Integrating Ayurveda into Parkinson’s Management: A Holistic ApproachIntegrating Ayurveda into Parkinson’s Management: A Holistic Approach
Integrating Ayurveda into Parkinson’s Management: A Holistic Approach
 
Top Effective Soaps for Fungal Skin Infections in India
Top Effective Soaps for Fungal Skin Infections in IndiaTop Effective Soaps for Fungal Skin Infections in India
Top Effective Soaps for Fungal Skin Infections in India
 
Chapter 11 Nutrition and Chronic Diseases.pptx
Chapter 11 Nutrition and Chronic Diseases.pptxChapter 11 Nutrition and Chronic Diseases.pptx
Chapter 11 Nutrition and Chronic Diseases.pptx
 
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdfCHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
CHEMOTHERAPY_RDP_CHAPTER 1_ANTI TB DRUGS.pdf
 
Efficacy of Avartana Sneha in Ayurveda
Efficacy of Avartana Sneha in AyurvedaEfficacy of Avartana Sneha in Ayurveda
Efficacy of Avartana Sneha in Ayurveda
 

2621008 - C++ 3

  • 3. Slide 3 Loop Types  Sentinel  while  do-while  Counting  for
  • 5. Slide 5 while Syntax while(test condition) { statement }
  • 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
  • 7. Slide 7 Example #1 while (7) cout << “hello” << endl;
  • 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; }
  • 11. 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
  • 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));
  • 14. Slide 14 for Syntax for (startExpression; testExpression; countExpression) } statement }
  • 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;
  • 17. Slide 17 Goal Output * * * * * * * * * * * * * * * Example #2
  • 18. Slide 18 Outputting 5 Lines for (int i = 1; i <= 5; i++) { cout << endl; }
  • 19. Slide 19 Outputting 5 Stars for (int i = 1; i <= 5; i++) { cout << “* ”; }
  • 20. Slide 20 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  • 21. Slide 21 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  • 22. Slide 22 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=1 variable values
  • 23. Slide 23 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * output i=1 j=1 variable values
  • 24. Slide 24 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * output i=1 j=2 variable values
  • 25. Slide 25 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * output i=1 j=2 variable values
  • 26. Slide 26 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * output i=1 j=3 variable values
  • 27. Slide 27 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * output i=1 j=3 variable values
  • 28. Slide 28 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * output i=1 j=4 variable values
  • 29. Slide 29 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * output i=1 j=4 variable values
  • 30. Slide 30 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * output i=1 j=5 variable values
  • 31. Slide 31 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=5 variable values
  • 32. Slide 32 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  • 33. Slide 33 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  • 34. Slide 34 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=? variable values
  • 35. Slide 35 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=1 variable values
  • 36. Slide 36 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=1 variable values
  • 37. Slide 37 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=2 variable values
  • 38. Slide 38 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=2 variable values
  • 39. Slide 39 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=3 variable values
  • 40. Slide 40 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=3 variable values
  • 41. Slide 41 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=4 variable values
  • 42. Slide 42 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=4 variable values
  • 43. Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  • 44. Slide 44 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=2 j=5 variable values oops!!!
  • 45. 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
  • 46. Slide 46 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  • 47. Slide 47 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } output i=1 j=1 variable values
  • 48. Slide 48 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * output i=1 j=1 variable values
  • 49. Slide 49 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * output i=1 j=2 variable values
  • 50. Slide 50 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * output i=1 j=2 variable values
  • 51. Slide 51 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * output i=1 j=3 variable values
  • 52. Slide 52 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * output i=1 j=3 variable values
  • 53. Slide 53 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * output i=1 j=4 variable values
  • 54. Slide 54 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * output i=1 j=4 variable values
  • 55. Slide 55 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=5 variable values
  • 56. Slide 56 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  • 57. Slide 57 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  • 58. Slide 58 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=? variable values
  • 59. Slide 59 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=1 variable values
  • 60. Slide 60 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=1 variable values
  • 61. Slide 61 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=2 variable values
  • 62. Slide 62 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=2 variable values
  • 63. Slide 63 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=3 variable values
  • 64. Slide 64 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=3 variable values
  • 65. Slide 65 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=4 variable values
  • 66. Slide 66 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=4 variable values
  • 67. Slide 67 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  • 68. Slide 68 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  • 69. Slide 69 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=3 j=? variable values
  • 70. Slide 70 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=3 j=1 variable values
  • 71. Slide 71 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=3 j=1 variable values
  • 72. Slide 72 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=3 j=2 variable values
  • 73. Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * output i=3 j=2 variable values
  • 74. Slide 74 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * output i=3 j=3 variable values
  • 75. Slide 75 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=3 variable values
  • 76. Slide 76 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=4 variable values
  • 77. Slide 77 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=4 variable values
  • 78. Slide 78 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=4 j=? variable values
  • 79. Slide 79 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=4 j=1 variable values
  • 80. Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * output i=4 j=1 variable values
  • 81. Slide 81 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * output i=4 j=2 variable values
  • 82. Slide 82 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=2 variable values
  • 83. Slide 83 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=3 variable values
  • 84. Slide 84 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=3 variable values
  • 85. Slide 85 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=5 j=? variable values
  • 86. Slide 86 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=5 j=1 variable values
  • 87. Slide 87 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=1 variable values
  • 88. Slide 88 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=2 variable values
  • 89. Slide 89 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=2 variable values
  • 90. Slide 90 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=6 j=? variable values
  • 91. Slide 91 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=6 j=? variable values
  • 92. Slide 92 * * * * * * * * * * * * * * *
  • 93. 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; } * * * * * * * * * * * * * * *
  • 94. 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; } * * * * * * * * * * * * * * *
  • 95. 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; } * * * * * * * * * * * * * * *
  • 97. Slide 97 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
  • 98. Slide 98 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 First Execution
  • 99. Slide 99 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 First Execution 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 Second Executio
  • 100. Slide 100 Pseudo-Random Generation The numbers aren't actually random -- they're generated by a formula, so they're just pseudorandom.
  • 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);