cs112

Compiler Errors and Logic Errors

  • Compiler Error: Occurs when an 'else' statement is not correctly associated with a preceding 'if' statement, leading to the error message: "L is without a proceeding if."

  • Example of correction: Placing a semicolon before the 'else' statement eliminates the syntax error but can lead to a Logic Error.

    • Logic Error: This happens when the flow of control continues without an associated action, which can result in unintended behavior in the program.

Conditional Statements

Double Conditional

  • Definition: A structure that has two branches for control flow.

  • Example: If average >= 60, then perform one action; otherwise, perform another.

Multiple Conditions

  • Evaluation of Multiple Conditions:

    • Start with the first condition; if true, execute its statement and exit further evaluation.

    • If the first condition is false, move on to check the next condition.

    • This process continues until either a condition is true or all have been checked.

    • If none are true, the final else is executed (if provided).

Differences Between Single and Multiple Conditionals

  • Single Conditions: Each condition operates independently without the 'else' linking them. All would execute sequentially.

    • Example: If conditions were separate without 'else', multiple actions could trigger:

      • If temperature is 91, print "Go to the beach."

      • Print also "Go to the park." (

        • Both actions executed as separate continuations)

  • Multiple Conditional Expressions: The use of the 'else' statement connects conditions, ensuring only one block executes based on the first true condition. If true, stop checking further conditions.