Tingimuslause
Lihtne tingimuslause
Python
if boolean_condition:
do_something_if_true()
Java
if (booleanCondition) {
doSomethingIfTrue();
}
if-else
Python
if boolean_condition:
do_something_if_true()
else:
do_something_else()
Java
if (booleanCondition) {
doSomethingIfTrue();
} else {
doSomethingElse();
}
if-elseif-else
Python
if boolean_condition:
do_something_if_true()
elif some_other_condition:
do_some_other_thing()
else:
do_something_else()
Java
if (booleanCondition) {
doSomethingIfTrue();
} else if (otherCondition) {
doSomeOtherThing();
} else {
doSomethingElse();
}
Switch-case
Python
month = 2
match month:
case 1:
month_string = "January"
break
case 2:
month_string = "February"
break
case 3:
month_string = "March"
break
case _:
month_string = "Invalid"
break
print(month_string) # February
Java
int month = 2;
String monthString;
switch (month) {
case 1:
monthString = "January";
break;
case 2:
monthString = "February";
break;
case 3:
monthString = "March";
break;
default:
monthString = "Invalid";
break;
}
System.out.println(monthString); // February
Ternary operator
Python
>>> "true" if True else "false"
'true'
>>> 'true' if False else 'false'
'false'
Java
jshell> true ? "true" : "false"
$1 ==> "true"
jshell> false ? "true" : "false"
$2 ==> "false"
Tõeväärtus
Python
a = True
b = False
Java
boolean a = true;
boolean b = false;
Tõeavaldised
Python
a and b
a or b
not a
Java
a && b
a || b
!a