September 23 - Conditional Statements in Programming: `if` and `if-else`
Conditional Statements: if and if-else
Introduction to if Statements
ifstatements are decision-making structures that allow a program to execute a block of code only if a specified condition evaluates totrue(or 'yes').If the condition is
false, the operations within theifstatement block are skipped.
Structure and Curly Brackets
Multiple Statements: When an
ifstatement needs to perform multiple operations, these operations must be enclosed within curly brackets ({}). These brackets mark thestartandendof theifstatement's block of code.Example:
if (condition) { statement1; statement2; }
Single Statement: In many programming languages, if an
ifstatement has only one operation to perform, the curly brackets are optional. The single statement can often be written on the same line as theifcondition for conciseness.Example:
if (average > 95) grade = 'A';
The Role of Semicolons (;)
If an
ifstatement does not use curly brackets to define its block, it is terminated by the first semicolon (;) it encounters after theifcondition.This means only the first line of code ending with a semicolon following the
ifcondition will be considered part of theifstatement.Any subsequent lines of code are outside the
ifstatement block and will execute regardless of whether theifcondition istrueorfalse.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
ifcondition, anelseblock, or any function.It significantly improves code readability and helps in understanding the scope of code blocks.
Relational Operators
Relational operators are used within
ifconditions to compare values and establish relationships.Examples shown:
(x is not equal to y), comparisons likeaverage > 95,(assignment, often confused withfor 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 structuredmainmethod.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 ofif (x == y)in C++/Java within a conditional statement, it might lead to a logical error ifx=yalways evaluates to true (due to assignment returning a non-zero value).
Control Flow
When an
ifcondition isfalseand there isno elseblock, the program simply skips theif's code block and proceeds to the next statement after theifblock.
if-else Statements
if-elsestatements provide an alternative execution path when theifcondition isfalse.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 blockFlow: If the
ifcondition istrue, theifblock executes. Iffalse, theelseblock executes. Only one of the blocks will ever execute. After the chosen block completes, the program proceeds to the statements immediately following the entireif-elsestructure.
Boolean Flags
A
booleanvariable can be used as a