Expanding the logical expression - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Expanding the logical expression

Description:

Expanding the logical expression. There are 3 more boolean or ... The NOT is usually confusing and should be used sparingly. Nested if else 'an if within an if' ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 29
Provided by: jason353
Category:

less

Transcript and Presenter's Notes

Title: Expanding the logical expression


1
Expanding the logical expression
  • There are 3 more boolean or logical operators
  • Which allow for more complicated expressions.
  • They are
  • logical OR
  • logical AND
  • ! Logical NOT

2
  • These allow for making compound logical
    expressions.
  • Is x between 2 and 5 inclusive?
  • 2 5
  • (xgt2) (xlt5)
  • Note for the AND to be true both propositions
    must be true.

3
  • The (AND)
  • Proposition1 Proposition2
  • Proposition1 Proposition2 RESULT
  • True True True
  • True False False
  • False True False
  • False False False
  • So for the result to be true both propositions
    must be true

4
Another Example - Multiple ifs
  • include ltstdio.hgt
  • // Assign a letter grade to a mark out of 100
    Inefficient/
  • int main ( void )
  • int grade
  • char letter_grade
  • printf("Please enter a mark out of 100 ")
  • scanf(d,grade)
  • if ( grade gt 80 )
  • letter_grade 'A'
  • / Continues on Next Slide /

5
  • if ( grade gt 70 grade lt 80 ) /After
    Previous Slide /
  • letter_grade 'B'
  • if ( grade gt 60 grade lt 70 )
  • letter_grade 'C'
  • if ( grade gt 50 grade lt 60 )
  • letter_grade 'D'
  • if (grade lt 50)
  • letter_grade 'F'
  • printf(" A mark of d is an c\n, grade,
    letter_grade)
  • return(0)

6
  • Compound logical expressions with (OR).
  • Is today the first or 15th of the month?
  • (dayOfMonth 1) (dayOfMonth 15)
  • This is true is either proposition is true
    Obviously in this case both cant be true.

7
  • The (OR)
  • Proposition1 Proposition2
  • Proposition1 Proposition2 RESULT
  • True True True
  • True False True
  • False True True
  • False False False
  • So for the result to be false both propositions
    must be false.

8
  • The NOT operator (!) inverts the value of a
    logical expression. It is a unary operator.
  • x7
  • The expression !(x7) is False since
    the expression (x7) is true.
  • The NOT is usually confusing and should be
    used sparingly

9
Nested if elsean if within an if
10
  • Example Printing out the number of days in each
    month. Assume that the months are integers from
    1 to 12. For now just do february.
  • int year, month, numberOfdays
  • if (month 2)
  • if ((year4) 0)
  • numberOfDays29
  • else
  • numberOfDays28

11
Dangling elses..danger
  • Suppose I write the last code without bracesTo
    which if does the else belong. The indenting is
    irrelevant
  • if (month 2)
  • if ((year4) 0)
  • numberOfDays29
  • else
  • numberOfDays28
  • Rule the else belongs to the nearest if. Better
    use braces
  • For nested ifs

12
  • Suppose I write this without braces
  • if (expression)
  • if ( expression)
  • statement
  • else
  • statement
  • In fct this means the same as before so use
    braces.

13
  • if (expression)
  • if ( expression)
  • statement
  • else
  • statement
  • Unambiguous to user.

14
  • Next slide If you enter a month and year how
    many days in the month.
  • Solution the 1,3,5,7,8,10,12th month are 31
  • the 4, 6, 9, 11th month are 30
  • february is 28 except on a leap year
    when it is 29.

15
  • include ltstdio.hgt
  • int main(void)
  • int month, year, numberOfDays
  • printf("please enter month and year as
    integers\n")
  • scanf("dd",month,year)
  • if ((month 1) (month 3) (month
    5) (month 7)
  • (month 8) (month 10) (month
    12))
  • numberOfDays31
  • if ((month 4) (month 6) (month
    9) (month 11))
  • numberOfDays30
  • if (month 2)
  • if ((year4) 0)
  • numberOfDays29
  • else
  • numberOfDays28

16
  • The code is inefficient because if its January
    we are doing the next two ifs for nothing. Also
    if we have done the first two ifs and they failed
    we know its not

17
  • We will do more ifs but now for a break we start
    LOOPS

18
Looping
19
Repetition or Looping
  • The brute force of computing is the ability to
    repeat an action with changes as many times as
    necessary.
  • The model for looping can be seen in English in a
    number of ways
  • Do 20 push-ups or closer to coding
  • Repeat the following 20 times
  • ( Do a pushup)

20
  • In the example of the last slide we know how
    many times we want to do something. This is not
    always true.
  • e.g. You are baking a cake. The recipe might
    say
  • stir until the mixture is smooth.
  • Here we do not know how many times we keep
    repeating until the mixture is smooth.

21
Looping Types of Loops
  • Three main types of looping in C
  • for (pretest)
  • while (pretest)
  • do while (posttest)

22
Looping Types of Loops
  • for A fixed number of times
  • while Indefinite number of times.
  • Check first to see if to repeat.
  • do while Indefinite number of times.
  • Do it first and then see if you wish
    to repeat

23
Example 1 For Loops
  • Example Output integers from 1 to 3
  • include ltstdio.hgt
  • int main(void)
  • int i
  • for (i0 ilt3i)
  • printf("countd\n", i ) / Note the
    Output /
  • return 0
  • Output
  • count 0
  • count 1
  • count 2

24
  • Look at the main lines
  • int i
  • for (i0 ilt3i)
  • printf("countd\n", i )

25
Syntax of the for loop
  • for(expression1, condition, expression3)
  • statement
  • Expression1 is called the initialization
    expression. i1
  • Expression 3 is the increment. Changes something.
    i
  • Condition is a boolean expression, loop keeps
    going while it is true.

26
  • Example 2 For Loops
  • We prompt the user for how many numbers we
    should average, then we input that many numbers
    and average them.
  • Note that we do this by keeping a running sum.

27
  • int count, sum, number, i
  • float average
  • printf(" How many numbers are to be
    averaged?\n")
  • scanf("d", count)

28
  • sum 0
  • for (i1 iltcount i)
  • printf("\nEnter next number\n")
  • scanf("d", number)
  • sum number
  • printf("\n")
  • average (float)sum / (float)count
Write a Comment
User Comments (0)
About PowerShow.com