Chapter 3. Jython Control Flow

Control statements include conditionals such as if, and loop constructs such as for and while. They form the structure that holds a program together and shapes its behavior. Although the basics of conditionals and loops are similar across a wide variety of programming languages, each language has its own specific implementation. Coming from Java to Python, you will find the basic statements familiar, but some of the details will be new. In this chapter, we will tour the ways in which Python manages flow of control, including the use of indentation to mark block boundaries.

Statements and Expressions

A Python program consists of a series of statements. A statement can be:

  • An expression combining variables and operators, which may include function or method calls

  • An assignment

  • One of several keyword statements, such as if, for, or try

The following are all valid statements:

1 + 2 * 3

range(2, 3)

inputFile.close( )

if x == None: return

In Python, simple statements are normally separated by the end of a line (any operating system’s end-of-line combination will work). However, Python will allow a statement to continue beyond the end of a line if there is an open parenthesis, bracket, brace, or triple-quote string. Such a statement can continue over any number of lines until the delimiter is closed. A statement can also be continued to the next line by ending it with a backslash \. The backslash only allows the statement to be continued for one more line; to continue ...

Get Jython Essentials now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.