Академический Документы
Профессиональный Документы
Культура Документы
Content
While loop
Type casting
For loop
Increment/decrement
Nested loop
Do-while loop
Break statement
Switch statement
c1117 lecture 6
Looping constructs
int
int total
total =0;
=0; int
int x;
x;
cout
cout <<
<< "Enter
"Enter 33 positive
positive integers:
integers: ";
";
cin
cin >>
>> x;
x; total
total == total
total ++ x;
x;
cin
cin >>
>> x;
x; total
total == total
total ++ x;
x;
cin
cin >>
>> x;
x; total
total == total
total ++ x;
x;
cout
cout <<
<< The
The sum
sum is
is <<
<< total
total <<
<< endl;
endl;
c1117 lecture 6
Looping constructs
c1117 lecture 6
x;
x;
x;
x; cin
cin >>
>> x;
x; }}
x;
x; cin
cin >>
>> x;
x; }}
x;
x; cin
cin >>
>> x;
x; }}
We dont
know how
many times
it takes!!
4
While loop
c1117 lecture 6
While loop
see sum-loop.cc
c1117 lecture 6
While loop
While condition the while body is executed only if
the condition is evaluated to be true
while
while (( condition
condition ))
single_statement1;
single_statement1;
next_statement;
next_statement;
while
while (( condition
condition )) {{
group_statement1;
group_statement1;
group_statement2;
group_statement2;
....
....
}}
next_statement;
next_statement;
While loop
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
x;
x;
Memory
address
1004
10
x
total
1008
x;
x;
10
10
Main memory
Command prompt
c1117 lecture 6
While loop
true!
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
Condition return
true, the body is
executed.
c1117 lecture 6
x;
x;
Memory
address
1004
10
x
total
1008
x;
x;
10
10
Main memory
Command prompt
9
While loop
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
Read in the
2nd
input
x;
x;
Memory
address
1004
15
1008
10
total
x;
x;
10
10
15
Main memory
Command prompt
c1117 lecture 6
10
While loop
true!
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
x;
x;
Memory
address
1004
15
1008
10
total
x;
x;
10
10
15
15
Main memory
Command prompt
11
While loop
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
Read in the
3nd
input
x;
x;
Memory
address
1004
-1
1008
25
total
x;
x;
10
10
15
15
-1
Main memory
Command prompt
c1117 lecture 6
12
While loop
false!
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
x;
x;
Memory
address
1004
-1
1008
25
total
x;
x;
10
10
15
15
-1
-1
Main memory
Command prompt
13
While loop
false!
int
int total
total =0;
=0; int
int
cin
cin >>
>> x;
x;
while(x
while(x >> 0){
0){
total
total == total
total ++
cin
cin >>
>> x;
x;
}}
x;
x;
Memory
address
1004
-1
1008
25
total
x;
x;
10
10
15
15
-1
-1
Main memory
Command prompt
14
Example 1
Assignment
tnemngissA
15
c1117 lecture 6
16
Example 1
c1117 lecture 6
17
Example 1
The no. of character printed before the
loop is zero.
string input;
cin >> input;
int i = 0;
while (i < input.length()){
cout << input[input.length()- i - 1];
i = i + 1;
}
See reverse.cc
c1117 lecture 6
18
int i
while
cout
}
c1117 lecture 6
= 0;
(i < input.length()){
The condition is
<< input[input.length()- i - 1];
always true, the loop
will not stop.
19
c1117 lecture 6
20
Example 2
when n=0
n(n-1)(n-2)2*1
when n>0
c1117 lecture 6
21
Example 2
c1117 lecture 6
22
Example 2
The body is executed n
times, from i = 1 to n.
int
cin
int n;
n;
cin >>
>> n;
n;
int
int ii == 0;
0; int
int result
result == 1;
1;
while(
while( ii << n){
n){
result
result == (i
(i ++ 1)
1) ** result;
result;
ii == ii ++ 1;
1;
}}
cout
cout <<
<< "The
"The factorial
factorial is:"
is:" <<
<< result
result <<
<< endl;
endl;
See factorial.cc
c1117 lecture 6
23
Example 3
c1117 lecture 6
24
Example 3
c1117 lecture 6
25
Example 3
bool
bool isPrime(unsigned
isPrime(unsigned int
int
unsigned
unsigned int
int ii == 2;
2;
while(
while( ii << x){
x){
if(x
if(x %% ii ==
== 0)
0) return
return
ii == ii ++ 1;
1;
}}
return
return true;
true;
}}
x){
x){
false;
false;
Can we do better?
c1117 lecture 6
26
Example 3
c1117 lecture 6
27
Example 3
Therefore:
c1117 lecture 6
28
Example 3
Warning is given by
compiler due to the
//
// Assume
Assume xx is
is greater
greater than
than 33 incompatible type.
bool
bool isPrime(unsigned
isPrime(unsigned int
int x){
x){
unisgned
unisgned int
int ii == 3;
3;
unsigned
unsigned int
int sqrt_x
sqrt_x == sqrt(x)
sqrt(x) ++ 1;
1;
while(
while( ii << sqrt_of_x){
sqrt_of_x){
if(x
if(x %% ii ==
== 0)
0) reutrn
reutrn false;
false;
ii == ii ++ 2;
2;
}}
return
return true;
true;
}}
Why we need to increment 1 here?
c1117 lecture 6
29
Type casting
30
Precision problem
unsigned int sqrt_x = static_cast<int>sqrt(x)+1;
c1117 lecture 6
18
18
31
Type casting
= 'c';
static_cast<int>(ch);
"x = " << x << endl;
xx == 99
99
c1117 lecture 6
32
for loop
string
cin
string input;
input;
cin >>
>> input;
input;
int
int ii == 0;
0;
while
while (i
(i << input.length()){
input.length()){
cout
cout <<
<< input[input.length()input[input.length()- ii -- 1];
1];
ii == ii ++ 1;
1;
}} string input;
cin
string input;
cin >>
>> input;
input;
for(int
for(int ii == 0;
0; ii << input.length();
input.length(); ii == ii ++ 1){
1){
result
result == ii ** result;
result;
}}
c1117 lecture 6
33
for loop
c1117 lecture 6
34
Initializing the
loop variable
c1117 lecture 6
Scoping rule
c1117 lecture 6
36
Increment/decrement
// k = k + 1;
// i = i 1;
c1117 lecture 6
37
Nested loop
c1117 lecture 6
38
do-while loop
c1117 lecture 6
Semicolon
should be
added here
39
Example
c1117 lecture 6
40
break
c1117 lecture 6
41
break
int
int num,
num, sum;
sum;
cout
cout <<
<< Enter
Enter aa
cin
cin >>
>> num;
num;
while(num
while(num >> 0){
0){
sum
sum +=
+= num;
num;
cout
cout <<
<< "Enter
"Enter
cin
cin >>
>> num;
num;
}}
cout
cout <<
<< "The
"The sum
sum
number
number (0
(0 to
to end)
end) :";
:";
aa number
number (0
(0 to
to end)
end) :";
:";
is
is "" <<
<< sum
sum <<
<< endl;
endl;
42
break
int
int num,
num, sum;
sum;
while(true){
while(true){
cout
cout <<
<< "Enter
"Enter
cin
cin >>
>> num;
num;
if(num
if(num ==
== 0)
0)
break;
break;
sum
sum +=
+= num;
num;
}}
cout
cout <<
<< "The
"The sum
sum
aa number
number (0
(0 to
to end)
end) :";
:";
c1117 lecture 6
43
Switch statement
c1117 lecture 6
44
Switch statement
switch(label){
switch(label){
Which alternative is
case
case constant_1:
constant_1:
Statement_sequence_1
executed depends on
Statement_sequence_1
break;
the label,
break;
case
statement_sequence_1
case constant_2:
constant_2:
Statement_sequence_2
Statement_sequence_2
is executed if
break;
break;
label=constant_1
...
...
case
case constant_n:
constant_n:
Statement_sequence_n
colon must be applied
Statement_sequence_n
break;
for each case.
break;
default:
default:
Default_statement_sequence
Default_statement_sequence
}}
c1117 lecture 6
45
Switch statement
char
char grade;
grade; cin
cin >>
>> grade;
grade;
switch(grade){
switch(grade){
case
case 'A':
'A':
cout
cout <<
<< "Excellent!"
"Excellent!" <<
<< endl;
endl; break;
break;
case
case 'B':
'B':
cout
cout <<
<< "Very
"Very good!"
good!" <<
<< endl;
endl; break;
break;
case
case 'C':
'C':
cout
cout <<
<< "Passing."
"Passing." <<
<< endl;
endl; break;
break;
case
case 'F':
'F':
cout
cout <<
<< "Fail."
"Fail." <<
<< endl;
endl; break;
break;
default:
default:
cout
cout <<
<< "It
"It is
is not
not aa possible
possible grade"
grade" <<
<< endl;
endl;
}}
c1117 lecture 6
46