4-3 code bloc if and else

This lesson zooms in on the if keyword and on code blocks. We declare an int variable: int valeur = 10;. The condition if (valeur == 10) compares the value of valeur with 10. Note that the comparison uses the double equals operator == and not the assignment operator =. When the comparison is true, the block that follows is executed; otherwise it is skipped.

Inside the block we put System.out.println("Value is 10");. With the if keyword you can use the compact one-line syntax without curly braces, in which case only the statement that follows the parentheses belongs to the if. With valeur == 10 it prints the message; with valeur == 11 the comparison is false and the line is ignored.

Always prefer the block form

  • Without braces, only the very next statement is part of the if; any further line runs unconditionally.
  • With braces, the boundaries of the block are explicit and refactoring is safer.
  • Even with a single line of code, the block form makes the program easier to read for the next developer.

That second behaviour is a frequent source of bugs: a developer adds a new line right under the if thinking it is part of the conditional, while in reality it always runs. The recommendation, even when you have only one statement, is to keep the curly braces. The next lessons continue with if/else variants and the else if ladder.

Summary

This lesson introduces Java's if and else code blocks, demonstrating how to use conditional statements to execute code based on comparisons. The tutorial covers basic if syntax, how to write conditions like `value == 10`, and explains the difference between single-line if statements and those using curly braces. The instructor emphasizes best practices for code readability by always using braces, even for single-line statements.

Key points

  • The if keyword executes code only when a condition is true
  • A condition is a comparison (e.g., value == 10) that evaluates to true or false
  • If the condition is true, the code block executes; if false, it is skipped
  • Single-line if statements work without braces, but braces improve code clarity and readability
  • Best practice: always use braces {} around if blocks, even for one-line statements, to make the code structure more visible
  • The else keyword will be covered in a following lesson to handle false conditions

FAQ

What is the difference between an if statement with and without braces?

Both execute the same way functionally. An if without braces applies only to the single line immediately following it. However, using braces is strongly recommended because it makes the code block more visible and easier to understand, preventing errors if the code is modified later.

What happens when an if condition is false?

When the condition is false, the code inside the if block is skipped entirely, and execution moves directly to the next statement after the block.

What is considered a 'condition' in an if statement?

A condition is a comparison that evaluates to either true or false, such as `value == 10` (equals), `value != 11` (not equals), or other logical expressions.