General Form
The condition must evaluate to true/false. The code block can be any code.
{
if (YOUR-CONDITION) {
YOUR-CODE
}
}
With Condition True
Here we hard-code the condition to true, so we do yes print.
{
if (true) {
prn("I am in the true block!");
}
}
With Condition False
Here we hard-code the condition to false, so we do not print.
{
if (false) {
prn("Nuts, I never get here!");
}
}
Using Vars
We'll often use variable(s) in the condition, in this case a method param var.
{
if (isFun) {
prn("Yes Fun");
}
}
Using Logical Expression
Here we use a logical expression in the condition (evaluates to true/false)
{
int a=10, b=15;
if (a < b) {
prn("a is smaller");
}
}
Using Logical Operator
Java uses && and || for logical AND and OR
{
int a=502, b=205;
if ((a > 500) && (b > 300)) {
prn("Both conditions true");
}
}
With Proper Objects
We'll often use proper objects in both the condition and the code block.
{
if (rec.isSquare()) {
rec.draw(g, c);
}
}