Reading & exercisesΒΆ
(3-4h)
From p.138 to p. 174 including exercise 3.9.
(3-4h) Reading with exercises scattered between the text +
3.4 => we already introduced flowcharts, but the chapter connects structured programming with flowcharts, i.e., enforces one limit: each control structure must have one entry and one exit, instead of allowing multiple entry and exit points.
Secure programming is always optional
(30 min) self-review exercises
Same procedure as How to read π€ from the last preparation, read, go through the points below and note down your doubts, and share them in discussions π€π₯.
3.2 Algorithms
algorithm
3.3 Pseudocode
pseudocode
pseudocode vs flowchart?
Why is
int iusually not part of the pseudocode?
3.4 Control structures
sequential execution, transfer of control -- how do these compare to the Big 3 table?
gotostatement (avoided in structured program development)structured programming and goto elimination[1]
single-entry/single-exit control statements
control statement stacking
selection statements
single selection
ifdouble selection
if-elsemultiple selection
switch(introduced in chapter 4)
iteration statements in C
(pre-test loop)
while(post-test loop)
do-while(introduced in chapter 4)(pre-test loop with counter)
for(introduced in chapter 4)
3.5
ifselection statementifstructure (in flowchart)indentation (white spaces) when using
if
3.6
if..elseselection statementif..elsestructure (in flowchart)conditional expression
the ternary operator
? :
nested
if..elsestatements[2]compound statement or block
{ }Error types
syntax
execution time (fatal, non-fatal)
3.7
whileiteration statementwhilestructure (in flowchart)infinite loop π
3.8 Formulating algorithms: Stepwise refinement 1: Counter-controlled iteration
counter-controlled (or definite) iteration
garbage value (uninitialized variable)
%sconversion specifier for strings (introduced first in section 2.7)
3.9 Stepwise refinement case study 2: sentinel-controlled iteration
sentinel values
top-down refinement (compare it with beginning a flowchart with a single process and then converting it into multiple processes)
program phases: initialization, processing, termination
converting between types
cast operator
(type)explicit
double avg = (double) sum / count;(ifsumandcountareint)implicit
double avg = sum / count;
floating point number formatting (
"%.2f\n")productivity: pseudocode, flowchart vs writing C code
3.10 Stepwise refinement case study 3: Nested control statements
nesting an
ifinto awhile(while there are students to process, increment the number of passed students if ...)
3.11 Assignment operators
+=,-=,*=,/=,%=π
3.12 Increment and decrement operators
increment
++and decrement--operators πpre-increment
++ivs post-incrementi++
(optional) 3.13 Secure C programming π‘οΈ
Arithmetic overflow
INT_MAXandINT_MINfrom<limits.h>scanf_sandprintf_s
Supplementary: One of the exercises that we will cover in the lecture (similar to 3.18) requires reading doubles, but the chapter does not cover it: `scanf(β%lfβ, ...)
Also called π Spaghetti code.
The right one is shorthand for the left one, and is more readable:
if (c1) { ; } else { if (c2) { ; } else { ; } }if (c1) { ; } else if (c2) { ; } else { ; }