Title: Control Structures
1Chapter 10
2Flow of Control
- Sequence
- Selection
- Repetition
3Selection if construct
- Syntax if (expression)
- statement
- expression is the condition for the if
construct. - If expression is evaluated to non-zero (true),
statement is executed. - If expression is evaluated to zero (false),
statement is skipped.
4if construct
- Syntax if (expression)
- statement
5Compound-statement Action
- Syntax if (expression)
- compound-statement
-
6Flags
- Flag an integer variable that simulates a
Boolean variable. Contains 0 (false) or 1 (true).
7Naming a Flag
- Appropriate naming is desirable.
- Example a flag attended implies
- attended if it contains 1 (true)
- did not attend if it contains 0 (false)
8Nested if statement
- Above could be rewritten as
9Short-circuit evaluation
- Evaluation stops as soon as value of expression
is known. Evaluation is from left to right. - For logical AND (), if the partial expression
is false, the whole expression is false. - For logical OR (), if the partial expression is
true, the whole expression is true.
10Short-circuit evaluation
11Complementing a Condition
- Comlementing or negating a logical expression
means changing the polarity. - Examples
- !(a 30) is equivalent to (a ! 30)
- !(a gt b) is equivalent to (a lt b)
12DeMorgans Theorem
- NOT(a AND b) same as NOT(a) OR NOT (b)
- NOT(a OR b) same as NOT(a) AND NOT(b)
- In C
- !(expr1 expr2) same as !(expr1)
!(expr2) - !(expr1 expr2) same as !(expr1)
!(expr2)
13DeMorgans Theorem
14Common Mistakes
- Do not use and ! on floating-point numbers.
- Mixing up with .
- Wrong placament of semi-colon, resulting in empty
statement
printf() is outside if construct.
15Common Mistakes
- Translating condition in English to C
- Let lower be 10, and upper be 30.If x is 20,
(lower lt x) is true, so it is evaluated to 1.
Since (1 lt upper) is also true, condition is
true!
16Common Mistakes
which is equivalent to this (since lt has a
higher precedence than )
17Common Mistakes
18Common Mistakes
- Forgetting the braces for compound statements
19if-else construct
- Syntax
- if (expression)
- statement1
- else
- statement2
if (expression) compound-statement1
else compound- statement2
20if-else construct
- May be used to avoid redundant code
21if-else construct
22Style
if (expression) compound-statement1 else
compound- statement2
if (expression) compound-statement1
else compound- statement2
23Removing common statements
- Common statements in the then and else parts
should be moved out of the if construct, if
appropriate
24Removing common statements
- After moving common statements out of if
construct
25Logical assignment for Flags
if-else statement may be replaced by an
assignment statement.
26Logical assignment for Flags
27Nested if-else statements
28Nested if-else statements
- Which if is the else associated with?
- else is associated with nearest if.
29Nested if-else statements
- To override default association, use braces to
mark out block.
30Nested if-else statements
31Nested if-else statements
32Common Mistakes
- Wrong matching of else with if.
- Wrong placement of semi-colon.
33Conditional Operator (?)
- Ternary operator
- condition ? expr1 expr2
- First operand is condition.
- If condition is true, take value of expr1
otherwise, take value of expr2.
34Conditional Operator (?)
equivalent to
35Conditional Operator (?)
equivalent to
36switch construct
- Multi-way selection statement
37switch construct
- May only test constant integral expressions,
i.e., expressions that evaluate to integers or
characters. - The vis are integral values the sis are
compound statements. - After expression is evaluated, control jumps to
appropriate case label. - break statements are inserted to avoid falling
through.
38switch construct
39Repetition Structure
- Counter-controlled repetiton number of
iterations known. - Sentinel-controlled repetiton iterate until a
sentinel value is entered, or terminating
condition is true.
40while construct
- Loop structure with pre-test condition.
- while (expression)
- statement
- expression is loop condition.
- If expression is true, statement in loop body is
executed, and expression tested again. - If expression is false, loop terminates.
41while construct
- Example Print n asterisks.
- count_star is the loop control variable.
42while construct
- Example Compute sum of first 100 positive
integers.
43while construct
- Which of these is/are same as previous code?
44while construct
- Loop control variable.
- Initialisation before the loop is entered, the
variable must be initialised. - Testing condition involving the loop control
variable is tested before the start of each loop
iteration if condition is true, loop body is
executed. - Updating loop control variable is updated during
each iteration (usually at the beginning or the
end of the loop body).
45Counter-control repetition
- A counter is used to keep track of number of
iterations.
46Sentinel-control repetition
- A sentinel is used to denote end of data.
47do-while construct
- Loop structure with post-test condition.
- do
- statement
- while (expression)
- Loop body is executed at least once.
48do-while construct
49Flag-controlled loops
- When loop condition is complex, flags may be used.
50for construct
- Another pre-test loop structure.
- Provides more compact form for counter-controlled
loops. - for ( initialisation-expression
- loop-condition
- update-expression )
- statement
51for construct
- The for construct is similar to this while
construct. - initialisation-expression
- while ( loop-condition )
- statement
- update-expression
-
52for construct
53for construct
- The initialisation-expression and
update-expression are often comma-separated lists
of expressions. - The comma operator evaluates the list from left
to right.
54for construct
- Any of the three expressions in the for header
may be omitted, but the semi-colons must stay. - If initialisation-expression is omitted, you must
perform necessary initialisation before loop.
55for construct
- If update-expression is omitted, you must ensure
that necessary update operations are done in loop
body.
56for construct
- If loop-condition is omitted, then the test is
always true. - This loop is infinite
57Common Mistakes
- Wrong placement of semi-colon.
58Common Mistakes
- Omitting semi-colons in for header.
- Mixing up semi-colons with commas in for
header. - Off-by-one error, where the loop executes one
more or one fewer iteration than intended. How
many iterations does this loop execute?
59break statement
- Used in loops, break causes execution to break
out of the loop that contains the statement.
60continue statement
- The continue statement causes execution to skip
remaining loop body and proceed to next iteration.
61Nested loops combined structures
- An example of nested for loops.
62Nested loops combined structures
63Nested loops combined structures
64Homework
- Try exercises behind chapter 10.