Lecture 03
@NCStateECE
Connect to slido.com
#ece209
ECE 209
Computer Systems Programming
Spring 2021
Lecture 05
Announcements
Z3 was moved to Thursday, 2/18
Program 1 moved to Friday, 2/26
Problem session PS2: 2/17 — CLion!!!
Quiz 2: 2/24
Conditional statement
Iterative statements (loops)
Conditional
Execute statement
if condition is true. T
F
Conditional
Execute statement
if condition is true.
if (expr) statement
0: false
non-zero: true
T
F
if (x > 10) x += 4;
if (x > 10) ;
if (x != 0) {
y = 100;
z = f + g;
}
if (expr) statement
if (x)
y = 100;
if (x)
y = 100;
z = f + g;
if ((x > 10) && (x < 20)) x += 4;
if (expr) statement
Conditional
Execute statement1
if condition is true.
Otherwise, execute
statement2.
T F
Conditional
Execute statement1
if condition is true.
Otherwise, execute
statement2.
T F
if (expr) statement
else statement
if (x > 10) x += 4;
else x = 10;
if ((x >= 0) && (x <= 100)) {
y = 1; z = 2;
}
else {
y = 2; z = 1;
}
if (expr) statement else statement
Multiple Choices...
if (x < 10) {
printf("Less than 10.\n");
}
else if (x <= 20) {
printf("Between 10 and 20.\n");
}
else {
printf("Greater than 20.\n");
}
Multiple Choices...
if (x < 10) {
printf("Less than 10.\n");
}
else if (x <= 20) {
printf("Between 10 and 20.\n");
}
else {
printf("Greater than 20.\n");
}
if (expr) statement else statement
Common Mistakes
if (x = 10)
printf("Less than 10.\n");
else
z = 1; y = 2;
// Determine toll based on hour of travel
if (timeHour < 6) { // Before 6:00 am
tollAmount = 1.55;
}
else if (timeHour < 10) { // 6 am to 9:59 am
tollAmount = 4.65;
}
else if (timeHour < 18) { // 10 am to 5:59 pm
tollAmount = 2.35;
}
else { // 6 pm and after
tollAmount = 1.55;
}
Conditional statement
Iterative statements (loops)
Iteration
T
F
As long condition is true,
execute statement.
Statement will (usually)
change something, so that
condition eventually
becomes false.
Iteration
T
F
As long condition is true,
execute statement.
Statement will (usually)
change something, so that
condition eventually
becomes false.
While Loop
T
F
As long condition is true,
execute statement.
while (expr) statement
While Loop
T
F
currPower = 2;
userChar = 'y';
while (userChar == 'y') {
printf("%d\n", currPower);
currPower *= 2;
scanf("%d", &userChar);
}
While Loop
T
F
currPower = 2;
scanf("%d",&userInput);
while (currPower <= userInput) {
currPower *= 2;
}
printf("%d\n", currPower);
Do-While Loop
T
F
Execute statement
as long condition is true.
do statement
while (expr);
For Loop
T
FEvaluate init expression.
As long condition is true,
execute statement and
evaluate re-init expression.
for (expr;expr;expr) statement
More complex syntax, but fundamentally no different
than while loop.
For Loop
T
F
for (init;cond;re-init) statement
Expressions
Typically, init and re-init have side effects.
Separated by semicolons.
These are not statements!!
For Loop
T
F
for (i=0; i<=100; i+=10) {
printf("%d\n", i);
}
For Loop
T
Fscanf("%d",&n);
sum = 0;
for (int i = 1; i<=n; i++) {
sum += i*i;
}
printf("%d\n", sum);
Can declare a new variable in for-loop,
but only visible within the loop.
For Loop
T
FCommon re-init expressions
i++ i+1
++i i >> 1
i += 1
i = i + 1
i += n
i -= n
i >>= 1
The first four are completely equivalent!
WRONG!
Common Mistakes
Using assignment instead of equals.
Forgetting to change condition variable.
Incorrect terminating condition (e.g. off-by-one).
Lecture 05
Slide Number 2
Slide Number 3
Conditional
Conditional
Slide Number 6
Slide Number 7
Conditional
Conditional
Slide Number 10
Multiple Choices…
Multiple Choices…
Common Mistakes
Slide Number 14
Slide Number 15
Iteration
Iteration
While Loop
While Loop
While Loop
Do-While Loop
For Loop
For Loop
For Loop
For Loop
For Loop
Common Mistakes