Skip to content

Java Programming · Week 3 notes

If Statements

Introduction to If Statements

Overview: Understanding If Statements in Java

What are If Statements?

If statements are fundamental control structures in Java that allow you to make decisions in your code. They enable your program to execute different blocks of code based on whether a specified condition is true or false.

Key Concepts

  • Basic If Statement
    • Executes a block of code if a condition is true
    • Example:
      if (condition) { // code to be executed if condition is true }
  • If-Else Statement
    • Provides an alternative block of code to execute if the condition is false
    • Example:
      if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
  • If-Else If-Else Statement
    • Allows checking multiple conditions
    • Example:
      if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if all conditions are false }
  • Nested If Statements
    • If statements inside other if statements
    • Example:
      if (outerCondition) { if (innerCondition) { // code to be executed if both conditions are true } }

Why are If Statements important?

  • They allow programs to make decisions
  • Enable creation of more complex and interactive applications
  • Form the basis for more advanced control structures
  • Essential for implementing business logic and algorithms

Select a lesson from the sidebar to dive deeper into each of these concepts!

Basic If Statements

Understanding Basic If Statements in Java

The basic if statement is the simplest form of conditional statement in Java. It allows you to execute a block of code only if a specified condition is true.

Syntax of Basic If Statement

if (condition) { // code to be executed if condition is true }

How It Works

  1. The condition inside the parentheses is evaluated.
  2. If the condition is true, the code block inside the curly braces is executed.
  3. If the condition is false, the code block is skipped.

Example

int age = 18; if (age >= 18) { System.out.println("You are eligible to vote."); }

In this example, the message will be printed because the condition (age >= 18) is true.

Important Points

  • The condition must be a boolean expression (evaluates to true or false).
  • If the code block contains only one statement, curly braces are optional (but recommended for clarity).
  • You can use any valid boolean expression as the condition, including method calls that return boolean values.

Practice

Try writing if statements with different conditions. Experiment with various comparison operators (==, !=, >, <, >=, <=) and logical operators (&&, ||, !).

Key Takeaways

  • Basic if statements allow conditional execution of code.
  • The condition must evaluate to a boolean value.
  • If statements are fundamental to creating decision-making logic in programs.
  • Always consider the readability of your code when using if statements.
  • Practice writing if statements to become comfortable with their usage.

If-Else Statements

Understanding If-Else Statements in Java

If-Else statements extend the basic if statement by providing an alternative block of code to execute when the condition is false.

Syntax of If-Else Statement

if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }

How It Works

  1. The condition inside the parentheses is evaluated.
  2. If the condition is true, the code block after the if is executed.
  3. If the condition is false, the code block after the else is executed.

Example

int age = 15; if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); }

In this example, the second message will be printed because the condition (age >= 18) is false.

Important Points

  • The else block is optional but provides a way to handle the false condition explicitly.
  • Only one of the two blocks (if or else) will be executed, never both.
  • You can chain multiple if-else statements for more complex conditions.

Practice

Try writing if-else statements for different scenarios. For example, check if a number is positive or negative, or determine if a year is a leap year.

Key Takeaways

  • If-Else statements provide two-way decision making in your code.
  • They improve code readability by explicitly handling both true and false conditions.
  • If-Else statements are fundamental for creating branching logic in programs.
  • Consider using if-else when you have two mutually exclusive code paths.

If-Else If-Else Statements

Understanding If-Else If-Else Statements in Java

If-Else If-Else statements allow you to check multiple conditions and execute different code blocks based on which condition is true.

Syntax of If-Else If-Else Statement

if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else if (condition3) { // code to be executed if condition3 is true } else { // code to be executed if all conditions are false }

How It Works

  1. Each condition is evaluated in order.
  2. If a condition is true, its corresponding code block is executed.
  3. Once a true condition is found and its block executed, the rest of the conditions are skipped.
  4. If none of the conditions are true, the else block (if present) is executed.

Example

int score = 85; if (score >= 90) { System.out.println("Grade: A"); } else if (score >= 80) { System.out.println("Grade: B"); } else if (score >= 70) { System.out.println("Grade: C"); } else if (score >= 60) { System.out.println("Grade: D"); } else { System.out.println("Grade: F"); }

In this example, "Grade: B" will be printed because the score is 85.

Important Points

  • You can have any number of else if clauses.
  • The else block is optional but provides a way to handle all other cases.
  • Only one block in the entire if-else if-else chain will be executed.
  • The order of conditions matters - place more specific conditions before general ones.

Practice

Try writing if-else if-else statements for different scenarios. For example, categorize BMI values, determine tax brackets based on income, or classify weather based on temperature.

Key Takeaways

  • If-Else If-Else statements allow for multi-way decision making in your code.
  • They are useful when you have multiple conditions to check.
  • The order of conditions is important for correct logic implementation.
  • Consider using switch statements for cleaner code when checking multiple values of a single variable.