If Else in Logo
Our first program will demonstrate conditional statements using if/else. Here’s the full source code:
To run the program, compile it and then use java
to execute:
Note that in Java, you need parentheses around conditions, and the braces are required for multi-line blocks but optional for single-line blocks.
Java does have a ternary operator (?:
) which can be used for simple conditional expressions, unlike some other languages. For example:
This can be useful for simple conditions, but for more complex logic, full if
/else
statements are preferred for readability.