• Boolean Expressions

    What are boolean expressions?

    • Expressions that return true or false (on/off, 1/0)
    • Expressions that use the logical operators, read more here http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B (Section Comparision Operators)
    • Main logical operators
      • >, greater than
      • <, less than
      • >=, greater than or equal to
      • <=, less than or equal to
      • ==, equal to
      • !=, not equal to

    Why are boolean expressions important?:

    It allow the program to ask true/false questions

    How are boolean expressions used?:

    • Program outputs:
      1 means true
      0 means false
      
      2 <  3: 1
      2 >  3: 0
      
      2 <= 3: 1
      3 <= 3: 1
      2 >= 3: 0
      3 >= 3: 1
      
      3 == 3: 1
      2 == 3: 0
      
      3 != 3: 0
      2 != 3: 1
    • Note that the C language treats 1 as true and 0 as false
  • if/else

    What is the if/else control statement?

    The if/else control statement is a group of code that tells the program to execute some code (and not others) based on a condition

    Why is the if/else control statement important?

    • it allows a program to have logic and branching
      • that means: it allows your program to handle different scenarios
      • a program without branching is like a train that:
        • starts at Honolulu
        • ends at Waipahu
      • a program with branching is like a train that:
        • starts at Honolulu
        • pauses at Pearl City and thinks about where to go
        • it could end up in Mililani
        • it could end up in Waipahu

    How to use the if/else control statement?

    A group of code that includes:

    • a condition
    • a block of code that is executed when the condition is true
    • a block of code that is executed when the condition is false
      • the false block of code is optional
      • when there's no false block of code, then the control statement is called the if control statement

    Syntax:

    • the keywords are if and else
    • the if keyword starts the if/else control statement, if (condition)
    • the condition follows the if keyword
    • the condition is wrapped in parenthesis, (condition)
    • the true and false blocks of code are wrapped in curly braces, { /*true block of code*/ }
    • the else keyword is between the ending curly braces of the true block and the starting curly braces of the false block, { /* true block */ } else { /* false block */ }
    • read more at http://www.tutorialspoint.com/cprogramming/if_else_statement_in_c.htm
  • if/else Example

    Example if/else, if.c:

    Things to note:

  • Demonstration of if/else

    More if/else examples, more_if.c:

  • Assignment

    Homework Problem 6.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_06_1.c

      example:

      • zhaol_06_1.c
    • Rename if.c (e.g. zhaol_06_1.c) and then modify it so magical numbers are from 100 to 199:

      • test case1:
        Enter a magical number:
        [user enters 5]
        Your number needs more magic :(
        
      • test case2:
        Enter a magical number:
        [user enters 105]
        Your number is very magical!
      • test case3:
        Enter a magical number:
        205
        Your number is too magical :(