Control Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Control Structures

Description:

Selection: if' construct. Syntax: if (expression) statement ; expression is the condition for the if' construct. ... while' construct. Loop control variable. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 65
Provided by: cen7150
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Chapter 10
  • Control Structures

2
Flow of Control
  • Sequence
  • Selection
  • Repetition

3
Selection 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.

4
if construct
  • Syntax if (expression)
  • statement

5
Compound-statement Action
  • Syntax if (expression)
  • compound-statement

6
Flags
  • Flag an integer variable that simulates a
    Boolean variable. Contains 0 (false) or 1 (true).

7
Naming 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)

8
Nested if statement
  • Example
  • Above could be rewritten as

9
Short-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.

10
Short-circuit evaluation
  • Example

11
Complementing 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)

12
DeMorgans 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)

13
DeMorgans Theorem
  • Example

14
Common 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.
15
Common 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!

16
Common Mistakes
  • Wrong condition
  • Correct method

which is equivalent to this (since lt has a
higher precedence than )
17
Common Mistakes
  • Wrong condition
  • Correct method

18
Common Mistakes
  • Forgetting the braces for compound statements
  • Correct method

19
if-else construct
  • Syntax
  • if (expression)
  • statement1
  • else
  • statement2

if (expression) compound-statement1
else compound- statement2
20
if-else construct
  • May be used to avoid redundant code
  • Use if-else construct

21
if-else construct
  • Another example

22
Style
  • Two common styles

if (expression) compound-statement1 else
compound- statement2
if (expression) compound-statement1
else compound- statement2
23
Removing common statements
  • Common statements in the then and else parts
    should be moved out of the if construct, if
    appropriate

24
Removing common statements
  • After moving common statements out of if
    construct

25
Logical assignment for Flags
  • Example

if-else statement may be replaced by an
assignment statement.
26
Logical assignment for Flags
  • Another example

27
Nested if-else statements
  • Example

28
Nested if-else statements
  • Which if is the else associated with?
  • else is associated with nearest if.

29
Nested if-else statements
  • To override default association, use braces to
    mark out block.

30
Nested if-else statements
  • Example

31
Nested if-else statements
  • Example

32
Common Mistakes
  • Wrong matching of else with if.
  • Wrong placement of semi-colon.

33
Conditional Operator (?)
  • Ternary operator
  • condition ? expr1 expr2
  • First operand is condition.
  • If condition is true, take value of expr1
    otherwise, take value of expr2.

34
Conditional Operator (?)
  • Example

equivalent to
35
Conditional Operator (?)
  • Example

equivalent to
36
switch construct
  • Multi-way selection statement

37
switch 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.

38
switch construct
  • Example

39
Repetition Structure
  • Counter-controlled repetiton number of
    iterations known.
  • Sentinel-controlled repetiton iterate until a
    sentinel value is entered, or terminating
    condition is true.

40
while 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.

41
while construct
  • Example Print n asterisks.
  • count_star is the loop control variable.

42
while construct
  • Example Compute sum of first 100 positive
    integers.

43
while construct
  • Which of these is/are same as previous code?

44
while 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).

45
Counter-control repetition
  • A counter is used to keep track of number of
    iterations.

46
Sentinel-control repetition
  • A sentinel is used to denote end of data.

47
do-while construct
  • Loop structure with post-test condition.
  • do
  • statement
  • while (expression)
  • Loop body is executed at least once.

48
do-while construct
  • Examples

49
Flag-controlled loops
  • When loop condition is complex, flags may be used.

50
for construct
  • Another pre-test loop structure.
  • Provides more compact form for counter-controlled
    loops.
  • for ( initialisation-expression
  • loop-condition
  • update-expression )
  • statement

51
for construct
  • The for construct is similar to this while
    construct.
  • initialisation-expression
  • while ( loop-condition )
  • statement
  • update-expression

52
for construct
  • Example

53
for construct
  • The initialisation-expression and
    update-expression are often comma-separated lists
    of expressions.
  • The comma operator evaluates the list from left
    to right.

54
for 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.

55
for construct
  • If update-expression is omitted, you must ensure
    that necessary update operations are done in loop
    body.

56
for construct
  • If loop-condition is omitted, then the test is
    always true.
  • This loop is infinite

57
Common Mistakes
  • Wrong placement of semi-colon.

58
Common 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?

59
break statement
  • Used in loops, break causes execution to break
    out of the loop that contains the statement.

60
continue statement
  • The continue statement causes execution to skip
    remaining loop body and proceed to next iteration.

61
Nested loops combined structures
  • An example of nested for loops.

62
Nested loops combined structures
  • Example 2

63
Nested loops combined structures
  • Example 3

64
Homework
  • Try exercises behind chapter 10.
Write a Comment
User Comments (0)
About PowerShow.com