September 23 - Conditional Statements in Programming: `if` and `if-else`

Conditional Statements: if and if-else

Introduction to if Statements

  • if statements are decision-making structures that allow a program to execute a block of code only if a specified condition evaluates to true (or 'yes').

  • If the condition is false, the operations within the if statement block are skipped.

Structure and Curly Brackets
  • Multiple Statements: When an if statement needs to perform multiple operations, these operations must be enclosed within curly brackets ({}). These brackets mark the start and end of the if statement's block of code.

    • Example: if (condition) { statement1; statement2; }

  • Single Statement: In many programming languages, if an if statement has only one operation to perform, the curly brackets are optional. The single statement can often be written on the same line as the if condition for conciseness.

    • Example: if (average > 95) grade = 'A';

The Role of Semicolons (;)
  • If an if statement does not use curly brackets to define its block, it is terminated by the first semicolon (;) it encounters after the if condition.

    • This means only the first line of code ending with a semicolon following the if condition will be considered part of the if statement.

    • Any subsequent lines of code are outside the if statement block and will execute regardless of whether the if condition is true or false.

    • Example:
      java if (x > y) System.out.println("x is greater than y"); // This is part of the if System.out.println("This always executes"); // This is not part of the if

Indentation
  • Indentation (e.g., using tabs or spaces) is a common practice in programming to visually mark the beginning of statements that fall under an if condition, an else block, or any function.

  • It significantly improves code readability and helps in understanding the scope of code blocks.

Relational Operators
  • Relational operators are used within if conditions to compare values and establish relationships.

    • Examples shown: xeqyx eq y (x is not equal to y), comparisons like average > 95, x=1x = 1 (assignment, often confused with x==1x == 1 for equality check in conditions).

Program Execution Flow and Errors

Syntax Errors vs. Logical Errors
  • Syntax Error: Occurs when the code violates the grammatical rules of the programming language.

    • These errors prevent the program from compiling or running.

    • Examples: Missing access specifiers (public), incorrect class name matching the file name, incorrectly structured main method.

    • A syntax error implies the program is not written in the way the language expects it to be.

  • Logical Error: Occurs when the code is syntactically correct and compiles/runs, but its logic is flawed, leading to incorrect or unexpected output.

    • The program executes, but the results are not what was intended.

    • For instance, if if (x = y) is used instead of if (x == y) in C++/Java within a conditional statement, it might lead to a logical error if x=y always evaluates to true (due to assignment returning a non-zero value).

Control Flow
  • When an if condition is false and there is no else block, the program simply skips the if's code block and proceeds to the next statement after the if block.

if-else Statements

  • if-else statements provide an alternative execution path when the if condition is false.

  • Structure:
    java if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false } // Program continues here after either if or else block

  • Flow: If the if condition is true, the if block executes. If false, the else block executes. Only one of the blocks will ever execute. After the chosen block completes, the program proceeds to the statements immediately following the entire if-else structure.

Boolean Flags

  • A boolean variable can be used as a