Table of Contents > Programming in Jython > "If" Statements

"If" Statements

"If" statments in Jython let you tell the computer to do one thing in one case and another thing in a different case. By using "if" statements in your program, you can use simple logic.


if and else

To use an "if" statment in your program, you'll define a set of conditions and some action that you want the computer to perform if the conditions are met. Then you'll define another set of actions that you want the computer to perform if the conditions are not met. This set of actions gets an "else" statement.

For example:

if x < 0:
  print x, " is less than zero."
else:
  print x, " is not less than zero."
jump to top

elif

Suppose you have three or more sets of circumstances that you want to define. You could put "if" statements inside of other "if" statements but that gets very complicated. A better solution is to use the world "elif," which is short for "else if."

For example:

if temperature < 55:
  print "It's cold outside today!"
elif (temperature >= 55) and (temperature <= 75):
  print "The weather is fine today."
else:
  print "It's a scorcher!"
jump to top