Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 6
Hung-Yu Wei
Department of Electrical Engineering
National Taiwan University
11/07/2006
Chapter 5
More on control statements
repetition
for
do…while
selection
switch (multiple selection)
5.1 Counter-controlled repetition
Counter=control variable
Essential elements
Name of a control variable (loop counter)
Initial value of the control variable
Continuation condition for the loop
The increment (or decrement) of the
control variable
Example: Essential elements
1 2 3 4 5 6 7 8 9 10
for
Syntax
for (initialization; condition; increment)
increment or decrement
Example 1
int n;
for(n=1;n<=10;n++)
Example 2
for(int n=1;n<=10;n++)
Compare for with while
For
for (initialization; loopCondition;increment)
{
statement;
}
While
initialization
while (loopCondition)
{
statement;
increment;
}
Same example: for v.s. while
while ( counter <= 10 )
{
cout << counter << “ “;
counter++;
}
1 2 3 4 5 6 7 8 9 10 Output Results
Using {} with for
Similar to the while and if case
For multiple statements
Example
for ( ; ; )
{
statement1;
statement2;
……
}
increment
They are the same
Counter=Counter+1
Counter += 1
++Counter
Counter++
UML: for loop
Some examples
for (int i=1;i<=100;i++)
for (int i=100;i>=1;i--)
for (int i=7;i<=77;i+=7)
for (int i=20;i>=2;i-=2)
for (int i=2;i<=20;i+=3)
for (int i=99;i>=0;i-=11)
Example
Sum even integers from 2 through 20
int main()
{
int total = 0;
for ( int number = 2; number <= 20; number += 2 )
total += number;
Empty body
Solving problem: compound interest
Compound interest formula ( 複利 )
p: principal (original amount)
r: annual interest rate
n : number of years a = p (1 + r ) n
1 2 3 4 5 6 7 8 9 10
int counter = 1;
while ( counter <= 10 )
{
cout << counter << “ “;
counter++;
}
int counter = 1;
do
{ cout << counter << " ";
counter++;
} while ( counter <= 10 );
1 2 3 4 5 6 7 8 9 10 Output Results
UML: do…while
switch
Multiple selection
In contrast to single selection (if )
Syntax
switch (controlling expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
default:
statements;
break;
}
Switch and IF
switch (x) { if (x == 1) {
case 1: cout << "x is 1";
cout << "x is 1"; }
break; else if (x == 2) {
case 2: cout << "x is 2";
cout << "x is 2"; }
break; else {
default: cout << " x is not 1 nor 2 ";
cout << “x is not 1 nor 2"; }
}
Switch and break(w/o break)
switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
Example program
Determine and count letter grades
Input letter grades (A,B,C,D,F)
Determine grades
Use switch
Count grades
Reading characters from input
Fig 5.9, 5.10, 5.11
Reading character
Characters are usually stored as type char
However, characters can also be stored as integer
ASCII character set (see Appendix B)
X,Y and x,y are different
static_cast
Explicit conversion
Example
cout << “The character (“ << ‘a’ << ”) has the
value “ << static_cast<int>(‘a’) <<endl;
Results
The character (a) has the value 97
cin.get()
Read 1 character from keyboard and store
it as an integer
EOF(End-Of-File)
Linux/Unix: Ctrl+d
Windows: Ctrl+z
Usage
while ((grade=cin.get() ) != EOF)
{
statements;
}
Welcome to the grade book for
CS101 C++ Programming!
1 2 3 4
Broke out of loop at count = 5
Example: continue
1 // Fig. 5.14: fig05_14.cpp
2 // continue statement terminating an iteration of a for statement.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
8 {
9 for ( int count = 1; count <= 10; count++ ) // loop 10 times
10 {
11 if ( count == 5 ) // if count is 5,
12 continue; // skip remaining code in loop
13
14 cout << count << " ";
15 } // end for
16
17 cout << "\nUsed continue to skip printing 5" << endl;
18 return 0; // indicate successful termination
19 } // end main
1 2 3 4 6 7 8 9 10
Used continue to skip printing 5
Logical Operators
Handle more complex conditions
How do we use if with 2 conditions
Old answer
if (condition1)
{
if (condition2)
{
statements;
}
}
New answer
Use logical operators!
&&
Logical AND
Both conditions are true
Intersection of 2 sets
Example
if ( gender ==1 && age >=65)
seniorFemles++;
For clarity
( gender ==1) && (age >=65)
Truth table (AND)
expression 1 expression 2 expression 1 && expression 2
Logical OR (||)
false || false: false
false || true: true
true || false: true
true || true: true