Conditional Statements

If Statements

If statements check to see if a conditional statement is true or false. If the result of a conditional statement returns true, it executes the code within its body. Lets look at a very quick example.

First, integer variable i is initialized to the value of 1. We then create an if statement that compares the value of i to the value of 2. Since i holds 1, which is less than two, the if statement executes the code within its block.

Following this we assign to i the value of 4 and execute the if statement with the same exact conditions as the one before. Since the comparison “i < 2” returns false, the if statement does not run the code within its block. Let’s write this in Java.