|
Original |
Translation |
|
4
|
Compound statements consist of one or more 'clauses.' A clause consists of a header and a 'suite.' The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which :keyword:`if` clause a following :keyword:`else` clause would belong::
|
|
|
5
|
if test1: if test2: print(x)
|
|
|
6
|
Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the :func:`print` calls are executed::
|
|
|
7
|
if x < y < z: print(x); print(y); print(z)
|
|
|
8
|
|
|
|
9
|
|
10
|
The formatting of the grammar rules in the following sections places each clause on a separate line for clarity.
|
|
|
11
|
The :keyword:`if` statement
|
|
|
12
|
The :keyword:`if` statement is used for conditional execution:
|
|
|
13
|
It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section :ref:`booleans` for the definition of true and false); then that suite is executed (and no other part of the :keyword:`if` statement is executed or evaluated). If all expressions are false, the suite of the :keyword:`else` clause, if present, is executed.
|
|