If Statements
If Statements
If Statement Syntax
Conditionals are pieces of code that make a decision about what the
program is going to do next. The most common conditional is the if
statement.
If Statement Syntax
It is best practice to also indent the lines of code inside the curly braces to
visually differentiate them from the commands that will always run.
if(5 > 4) {
System.out.println("1st command if true");
System.out.println("2nd command if true");
}
System.out.println("I will always print!");
If Statement
If Statement
If statements test to see if a certain condition is true. If yes, then a specific
commands are run. The simple if statement does not do anything if the
boolean expression is false.
if (7 != 10){
System.out.println("The above statement is true");
System.out.println("The above statement is still true");
}
System.out.println("This is not related to the if statement");
challenge
challenge
challenge
.guides/img/compound-conditional
The code on the left is a nested if statement - which means an if statement
is inside another if statement.
The code with the compound conditional (on the right) has fewer lines of
code, and is easier for a human to read. In fact, it almost reads like a
sentence.