Session 6 (Thursday 09/10/15)
CHAPTER TWO - Repetition and Selection

Repetition with the for and while loops
Selection with an if statement - even and odd numbers


Selection with an if statement - NESTED IF for BMI Body Mass Index assignment

CS 1510
Introduction to Computing


Review of session #5

Careful review and continued discussion of Session #5 and chapter two.


  1. Python example: WHILE and IF illustrated - separating EVEN and ODD numbers to calculate the average overall and for each category.

  2. VIP: Programming Assignment Two is due on Tuesday, September 15th by 10 PM.

  3. For the BMI status: There are FOUR CDC categories.

    Steps 1, 2, 3 and 4 of the program for computing BMI are:
    
      1. Prompt user for height (feet and inches) 
                     and weight (pounds)  of the person.
    
      2. Convert to metric - to centimeters and to kilograms.
    
      3. Compute the BMI.
    
      4. Display the person's BMI, along with its CDC category.
    
  4. How do you choose or select between FOUR different outcomes? The FOUR CDC categories.
    Divide a number by 4 and examine the remainder: 
    
    Is the remainder 0?  If not, it is 1 or 2 or 3.    Need a 2nd IF ELSE.
    
    Is the remainder 1?  If not, it has to be 2 or 3.  Need a 3rd IF ELSE.
    
    Is the remainder 2?  If not, is has to be 3!       The ELSE is enough!
    

    BMI_Example_SELECT4.txt output indicating which of FOUR roads was chosen given the data item.

    The last four letters of the string "UNI PANTHERS" illustrates nicely!
                                                 ----
                                                 HERS
    
      H 72 0   <--  Remainder is 0 - Divisible by 4
      E 69 1   <--  Remainder is 1
      R 82 2   <--  Remainder is 2 - even number, not divisible by 4
      S 83 3   <--  Remainder is 3 - ASCII mod 4 is 3
    

    What does the code look like???
    
        theASCIIvalue = ord( theChar )
        theRemainder = theASCIIvalue % 4
    
        if theRemainder == 0 :
            message = "Remainder is 0 - Divisible by 4"
        else :
            if theRemainder == 1 :
                message = "Remainder is 1"
            else :
                if theRemainder == 2 :
                    message = "Remainder is 2 - even number, not divisible by 4"
                else :
                    message = "Remainder is 3 - ASCII mod 4 is 3"
    
  5. Read it, study it, try it out: NestedIF_For_BMI.py Python code.

  6. NestedIF_For_BMI sample output.

  7. Compare to the IF_ELIF_ELSE.txt solution. One IF, two ELIFs adn one ELSE == FOUR WAY selection.
    Textbook section 2.2.9. More On Python Decision Statements...
                            IF ... ELIF ... ELIF ... ELIF ... ELSE ...
            Section 2.2.9. - pages 116-220.