Control Structures - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Control Structures

Description:

a representation for true and false values. true has the value of not zero ... There must be parenthesis around the expression. Selection. 10. Example. float sum, num; ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 26
Provided by: anniegro
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Selection
2
Control Structures
  • Structures that allow the programmer to control
    the flow of execution of program statements
  • They fall into 2 categoriesselection and
    repetition
  • Some Examples
  • if statements
  • switch statements
  • for loops
  • while loops

3
Boolean Expressions
  • The Boolean data type
  • a representation for true and false values
  • true has the value of not zero
  • false has the value of zero
  • not a built-in data type
  • User-defined type
  • typedef int Boolean
  • define TRUE 1
  • define FALSE 0

4
boolean.h
  • a header file that you create so you can use the
    Boolean type in all your applications
  • define TRUE 1
  • define FALSE 0
  • typedef int Boolean
  • Must include boolean.h to use the boolean data
    type
  • Why string quotes rather than brackets for
    boolean.h?

5
Typedefs
  • typedef ltdata typegt ltnew type namegt
  • typedef is a reserved word
  • creates a synonym for some data type
  • Why use typedefs?
  • Gives more meaning to the types of values
  • typedef float salaryType
  • typedef float percentType

6
Relational Operators
  • Operators used for relating numbers to each other
  • equal to
  • not equal to !
  • less than lt
  • greater than gt
  • less than or equal to lt
  • greater than or equal to gt
  • Used in boolean expressions
  • Arithmetic expressions can be used in boolean
    expressions
  • (10 4 3 - 8) lt (18 30 / 4 -20)

7
Logical Operators
  • Operators used to connect boolean expressions
  • - logical and, binary
  • - logical or, binary
  • ! - logical not, unary
  • Used to perform boolean logic
  • To avoid confusion and to make code more
    readable, it is best to parenthesize comparisons
    using logical operators
  • !((3 gt 4) (6 gt 5 6 lt 12))
  • (( -5.0 -6.2) ((7 lt 3) (6 (3 3))))

8
Short Circuit Evaluation
  • An optimization
  • Only evaluates as many expressions as are
    necessary
  • Using
  • returns TRUE as soon as any expression evaluates
    to TRUE
  • Using
  • returns FALSE as soon as any expression evaluates
    to FALSE
  • !((3 gt 4) (6 gt 5 6 lt 12))

9
if Statements
  • if ( ltboolean expressiongt )
  • ltstatementgt
  • A decision making statement used to make a
    program execute some statements only when a
    certain condition is true
  • If the boolean expression evaluates to TRUE, then
    the statement is executed
  • If the boolean expression evaluates to FALSE,
    then the statement after the if statement is
    executed
  • There must be parenthesis around the expression

10
Example
  • float sum, num
  • sum 0
  • scanf(d, num)
  • if (num gt 0.0)
  • sum sum num
  • printf(The sum is d\n, num)

11
Compound Statements
  • Several simple statements associated with each
    other in a block
  • The block is denoted by
  • doSomething()
  • doSomethingElse()
  • doYetAnotherThing()
  • Each statement in a block ends with a semi-colon
  • The closing bracket of a block does NOT end with
    a semi-colon
  • That would cause a syntax error

12
  • Compound statements are commonly used with if
    statements
  • They are needed for the case where there are
    multiple actions that need to take place if the
    boolean expression evaluates to TRUE
  • Simple statements can also be expressed using the
    block notation
  • if (xgt0)
  • x 5
  • printf(d is a positive integer., x)

13
Whats the Output?
  • int a 5
  • int b 10
  • if (a lt b)
  • b a
  • printf(d d\n,
  • a,b)
  • printf(d d\n,a,b)
  • int a 10
  • int b 5
  • if ((a lt b)(b-a gt 0))
  • a b
  • b--
  • prinf(d d\n,a,b)
  • prinf(d d\n,a,b)

14
Lets Write Some if Statements
  • Write a program that
  • Reads 3 real numbers
  • Counts the number of positive reals
  • Accumulates the sum of the positive reals

15
Two-Way Selection
  • if statements only provide one way branching
  • if else statements provide two way branching
  • Syntax
  • if ( ltboolean expressiongt )
  • lttrue statementgt
  • else
  • lt false statementgt

16
Control Flow of an if else Statement
  • If the boolean expression evaluates to TRUE, the
    statement following the expression is executed
  • Program control returns to the line of code
    following the complete if else statement
  • If the boolean expression evaluates to FALSE, the
    statement following the else is executed
  • Program control returns to the line of code
    following the complete if else statement

17
An Example
  • int temp 0
  • int a 10
  • int b 5
  • if (a gt b)
  • a
  • else
  • temp a
  • a b
  • b temp
  • printf(d d\n,a,b)

18
Nested Selection Statements
  • Since each selection statement is a simple
    statement, they can be used as statements that
    are part of other selection statements
  • Why would you need this?
  • Sometimes you have certain requirements for
    certain ranges of data
  • Sometimes you are looking for certain
    combinations of values for variables

19
For Instance
  • / a, b, and c represent the coefficients in the
    quadratic equation ax2 bx c 0 /
  • float a,b,c
  • if (a 0)
  • if (b ! 0)
  • / what goes here? /
  • else if (c 0)
  • / fill in the blank /
  • else
  • / last case /

20
Mutually Exclusive Conditions
  • Only one can be true at a time
  • Nesting selection statements prevents expressions
    from being evaluated where the result is going to
    always be false
  • if (score gt 69)
  • printf(Satisfactory)
  • if ((score lt 69) (score gt 50))
  • printf(Unsatisfactory)
  • if (score lt 50)
  • printf(Failing)

21
Lets Do It
  • Problem Calculating traffic fines
  • Fine scale
  • 5 - 10 mph over limit, 25 per mph over the limit
  • 11 - 15 mph over the limit, 35 per mph over the
    limit
  • 16 - 20 mph over the limit, 40 per mph over the
    limit
  • gt 20 over the limit, 1000
  • Notice how important indentation is when nesting
    selection!

22
Switch Statements
  • Multiway selection
  • You are not restricted to 1 or 2 courses of
    action
  • Syntax
  • switch ( ltselectorgt )
  • case ltlabel 1gt ltstatementgt
  • break
  • case ltlabel2gt ltstatementgt
  • break
  • default ltstatementsgt

23
  • The ltselectorgt is a variable of any ordinal type
  • e.g. int, char, typedef int myType
  • The labels are what the selector is compared
    toeffectively
  • if (selector label) / do whatever is
    following that

  • label until we hit a break /
  • The ltstatementgt can be any statement, simple or
    complex
  • e.g. assignment, selection, function calls
  • The break says Im done with this branch and
    returns program control flow to the line
    following the switch statement
  • if you leave this out, a very common mistake,
    sequential evaluation of statements takes place

24
An Example
25
When can I use a switch statement?
  • Whenever your selection is based on an ordinal
    type
  • Soit is not a replacement for if and if else
    selection statements
Write a Comment
User Comments (0)
About PowerShow.com