Decision and Control Flow
Jython's control flow is standard Python: if/elif/else for decisions, and truthiness rules for conditions. The syntax is identical to CPython, which is the point - you bring your Python knowledge over unchanged.
if / elif / else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(grade) # B
Comparison and Logic
a, b = 5, 10
print(a < b) # True
print(a == b) # False
print(a != b) # True
print(3 < a < 10) # chained comparison: True
x = 7
if x % 2 == 0 and x > 4:
print("even and big")
elif x % 2 != 0 or x < 3:
print("odd or small") # this branch runs
Truthiness
Empty containers, zero, None, and False are falsy; everything else is truthy - same as Python:
items = []
if not items:
print("no items") # runs
msg = ""
print(bool(msg)) # False
Conditional Expressions
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adult
Conditions Involving Java Objects
import java.util
lst = java.util.ArrayList()
if lst.isEmpty(): # Java method returning boolean
print("java list is empty")
s = java.lang.String("")
if len(s) == 0: # len works on java.lang.String
print("empty string")
Style Notes
- Indentation defines blocks - keep it consistent (4 spaces).
- No braces or colons-on-next-line: the colon ends the header, the indented block is the body.
- Keep condition expressions simple; extract complex boolean logic into named booleans for readability.
Summary
Control flow in Jython is plain Python: if/elif/else with indented blocks, chained comparisons, and/or/not logic, truthiness for empty values, and the conditional (ternary) expression. Java objects plug into conditions naturally - Java booleans and len()-able strings behave as expected in Python conditionals.