Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CS1010E Lecture 5
Henry Chia
hchia@comp.nus.edu.sg
Use a loop?
What is the loop termination condition?
How to generalize the statement body?
Lecture Outline
Problem based approach to introduce more
selection and repetition constructs.
/*
*/
int sumDigits(int n)
{
int sum = 0;
while (n > 0)
{
sum = sum + n%10;
n = n/10;
}
return sum;
}
Programming exercises.
CS1010E Lecture 5 p.2/40
1. sum sumDigits(num)
sum = sumDigits(num);
check = 13 - sum%13;
2. checkDigit 13 sum%13
return check;
}
E H
N R
10 11
12 13
10
11
12
13
Y
CS1010E Lecture 5 p.6/40
Nested if..elses.
if (checkDigit == 1)
printf("B\n");
else if (checkDigit == 2)
printf("A\n");
... // other cases as nested if..else
else if (checkDigit == 12)
printf("X\n");
else
printf("Y\n");
CS1010E Lecture 5 p.8/40
switch Statement
switch Statement
switch (int_expression)
{
case label1: statement(s);
case label2: statement(s);
// other cases
default: statements(s);
}
CS1010E Lecture 5 p.9/40
switch Statement
switch Statement
Sentinel-Controlled Input
Flowchart: while
FALSE
TRUE
looping
statement
block
checkDigit = computeCheckDigit(matric);
printCheckLetter(checkDigit);
}
while Statement
do..while Statement
while (condition)
{
statement(s);
}
do
{
statement(s);
} while (condition);
Priming Read
Flowchart: do..while
looping
statement
block
TRUE
condition
FALSE
Sentinel-Controlled Input
Count-Controlled Input
Write a program that reads from the user a
specified number, say n, then proceeds to read n
matric numbers as input, and prints out the
check letter for each input.
do
{
printf("Enter matric number: ");
scanf("%d", &matric);
if (matric > 9999 && matric < 100000)
{
checkDigit = computeCheckDigit(matric);
printCheckLetter(checkDigit);
}
} while (matric > 9999 && matric < 100000);
for Statement
for (init; condition; update)
{
statement(s);
}
checkDigit = computeCheckDigit(matric);
printCheckLetter(checkDigit);
count = count + 1;
for statement
Flowchart: for
init
update
condition
FALSE
TRUE
looping
statement
block
CS1010E Lecture 5 p.22/40
Count-Controlled Looping
return;
}
CS1010E Lecture 5 p.25/40
Nested Loops
Shorthand Operators
Assignment
Compound
Increment/Decrement
Assignment
a = a + x;
a += x;
a = a - x;
a -= x;
a = a * x;
a *= x;
a = a / x;
a /= x;
a = a % x;
a %= x;
Timeline tracing
A timeline trace shows the progress of value
changes in variables with respect to time.
Allows one to make inferences on program
behaviour.
Version 1:
int getBMIcat(double w)
{
int cat;
if
if
if
if
(w < 18.5)
(18.5 <= w < 25)
(25 <= w < 30)
(30 <= w)
cat
cat
cat
cat
=
=
=
=
1;
2;
3;
4;
return cat;
i 1
.
j ..
..
.
1
..
.
2
..
.
3 4
.. ..
. .
5
..
.
2
.
6 .. 1
.. .. ..
. . .
2
..
.
3
..
.
4 5
.. ..
. .
3
.
6 ..
.. ..
. .
Version 2:
int getBMIcat(double
{
if (w < 18.5)
else if (w < 25)
else if (w < 30)
else if (w >= 30)
}
w)
return
return
return
return
1;
2;
3;
4;
CS1010E Lecture 5 p.31/40
while (b > 0)
{
r = a%b;
a = b;
b = r;
}
return a;
}
CS1010E Lecture 5 p.33/40
Lecture Summary
Selection and Repetition Statements:
int countTriple(int n)
{
int a, b, c, count = 0;
Selection
if..else is the most general form.
Nested if..else for one-of-many choices.
switch for case values only.
Repetition
while is the most general form.
do..while for at least one loop.
for when loop is count-controlled.
Nested loops.
Familiarity with count-controlled and sentinel-controlled
input.
return count;
}
CS1010E Lecture 5 p.37/40
Lecture Summary
Programming Strategy
Selection
Refrain from using multiple return statements in
if..else constructs to avoid issue of complete
path coverage.
Repetition
Consider the looping condition. What makes it
continue?
Consider a generalized statement body.
Timeline tracing for nested loops.
Develop programs incrementally.