• Logical Operators

    Why are logical operators important?:

    • logical operators allow your boolean expressions to be more direct
    • using logical operators can simplify nested if/else statements which are hard to read

    What are logical operators?:

    • &&: and
    • ||: or

    How to use logical operators?:

    • logical operators are placed between 2 boolean expressions, e.g. (1 < 2) && (2 < 3)
    • expressions containing logical operators simplify like this:
      • AND Operator &&
        Expression 1Expression 2Result
        true true true
        true false false
        false true false
        false false false
      • OR Operator ||
        Expression 1Expression 2Result
        true true true
        true false true
        false true true
        false false false
      • && asks "is expression1 AND expression2 true?"
      • || asks "is expression1 OR expression2 true?"
  • Logical Operator Examples

    How does C interpret logical operators?

    This program shows how logical operators are interpretted in C

    logical_operators.c:

    Program output:

    T && T: 1
    T && F: 0
    F && T: 0
    F && F: 0
    
    T || T: 1
    T || F: 1
    F || T: 1
    F || F: 0

    Comparing logical operators with nested if/else:

    This program compares using logical operators with nested if/else

    logical_operators_vs_nested_if.c:

      Things to note:
    • both blocks of code do the same thing
    • the block of code using logical operators is more direct and the reader can easily understand the intent
    • the nested if/else statements allows you to handle "less than 0" differently than "greater than 100"
    • the logical operator will save you from copying and pasting code if you want to handle "less than 0" and "greater than 100" the same
  • else if

    Why use else if:

    using else if conveys more intent than nested if/else statement

    How to use else if:

  • else if Example

    Comparing else if with nested if/else:

    This program compares using else if with nested if/else

    else_if_vs_nested_if.c:

      Things to note:
    • using else if makes the code easier to read and understand
    • using else if allows the reader to quickly understand the intent of the writer
  • switch

    Why use switch:

    using switch conveys more intent than nested if/else statement

    How to use switch:

  • switch Example

    Comparing switch with nested if/else:

    This program compares using switch with nested if/else

    switch_vs_nested_if.c:

      Things to note:
    • comparing 0(an int) is different than comparing '0'(a char)
    • do not forget the break; statement at the end of each case
      • without the break; statement, the switch will continue executing code until it reaches a break; statement or exits the switch block of code
  • else if vs switch

    Comparing else if with switch:

    This program compares using else if with switch

    else_if_vs_switch.c:

      Things to note:
    • else if can match ranges
    • else if can use variables in the condition, e.g. variable1 <= variable2
    • switch can only test for equality with constants (no variables)
  • switch with Characters

    Write a program that translates alphabets to their military phonetic alphabet

    Alphabet Translation
    alphabetmilitary phonetic alphabet
    a alpha
    b bravo
    c charlie
    (anything else) ran out of funding
  • Assignment

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

    • a file named UHusername_25_1.c
      • example:
      • zhaol_25_1.c
    • Write a program that asks the user for an alphabet letter. If the letter entered is upper case, the program will check for the winning characters A, E, I, O, and U. If the letter entered is lower case, the program will check for the winning characters q and r. The program will tell the user if they've entered in a winning character or not. If the user enters anything besides an upper case or lower case alphabet, then the program will tell the user that the character entered is invalid. The program exits after getting one character from the user.

      • There are multiple ways to write such a program, but please follow these guidelines:
      • avoid nested if/else statements
      • use switch, else if, and logical operators whenever possible
      • since the switch, else if, and logical operators perform similar functions, be sure to think about which one is a more "natural" fit for the operation at hand (this can be subjective, but for this simple assignment, it should be pretty clear)
    • Test Cases:
      • Test Case 1:
        Please enter in a letter
        [user enters A]
        This is a winning character
        
      • Test Case 2:
        Please enter in a letter
        [user enters Z]
        This uppercase character is not a winning character
        
      • Test Case 3:
        Please enter in a letter
        [user enters q]
        This is a winning character
        
      • Test Case 4:
        Please enter in a letter
        [user enters s]
        This lowercase character is not a winning character
        
      • Test Case 5:
        Please enter in a letter
        [user enters +]
        This is an invalid character