Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="top-level.html" /> | |
13 | <link rel="prev" href="simple.html" /> | |
14 | <link rel="parent" href="ref.html" /> | |
15 | <link rel="next" href="if.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>7. Compound statements</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.14 The exec statement" | |
25 | href="exec.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="Python Reference Manual" | |
28 | href="ref.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="7.1 The if statement" | |
31 | href="if.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="exec.html">6.14 The exec statement</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="ref.html">Python Reference Manual</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="if.html">7.1 The if statement</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION009000000000000000000"></A><A NAME="compound"></A> | |
56 | <BR> | |
57 | 7. Compound statements | |
58 | </H1> | |
59 | <a id='l2h-568' xml:id='l2h-568'></a> | |
60 | <P> | |
61 | Compound statements contain (groups of) other statements; they affect | |
62 | or control the execution of those other statements in some way. In | |
63 | general, compound statements span multiple lines, although in simple | |
64 | incarnations a whole compound statement may be contained in one line. | |
65 | ||
66 | <P> | |
67 | The <tt class="keyword">if</tt>, <tt class="keyword">while</tt> and <tt class="keyword">for</tt> statements implement | |
68 | traditional control flow constructs. <tt class="keyword">try</tt> specifies exception | |
69 | handlers and/or cleanup code for a group of statements. Function and | |
70 | class definitions are also syntactically compound statements. | |
71 | ||
72 | <P> | |
73 | Compound statements consist of one or more `clauses.' A clause | |
74 | consists of a header and a `suite.' The clause headers of a | |
75 | particular compound statement are all at the same indentation level. | |
76 | Each clause header begins with a uniquely identifying keyword and ends | |
77 | with a colon. A suite is a group of statements controlled by a | |
78 | clause. A suite can be one or more semicolon-separated simple | |
79 | statements on the same line as the header, following the header's | |
80 | colon, or it can be one or more indented statements on subsequent | |
81 | lines. Only the latter form of suite can contain nested compound | |
82 | statements; the following is illegal, mostly because it wouldn't be | |
83 | clear to which <tt class="keyword">if</tt> clause a following <tt class="keyword">else</tt> clause would | |
84 | belong: | |
85 | <a id='l2h-570' xml:id='l2h-570'></a> | |
86 | ||
87 | <P> | |
88 | <div class="verbatim"><pre> | |
89 | if test1: if test2: print x | |
90 | </pre></div> | |
91 | ||
92 | <P> | |
93 | Also note that the semicolon binds tighter than the colon in this | |
94 | context, so that in the following example, either all or none of the | |
95 | <tt class="keyword">print</tt> statements are executed: | |
96 | ||
97 | <P> | |
98 | <div class="verbatim"><pre> | |
99 | if x < y < z: print x; print y; print z | |
100 | </pre></div> | |
101 | ||
102 | <P> | |
103 | Summarizing: | |
104 | ||
105 | <P> | |
106 | <dl><dd class="grammar"> | |
107 | <div class="productions"> | |
108 | <table> | |
109 | <tr> | |
110 | <td><a id='tok-compound_stmt' xml:id='tok-compound_stmt'>compound_stmt</a></td> | |
111 | <td>::=</td> | |
112 | <td><a class='grammartoken' href="if.html#tok-if_stmt">if_stmt</a></td></tr> | |
113 | <tr> | |
114 | <td></td> | |
115 | <td></td> | |
116 | <td><code>| <a class='grammartoken' href="while.html#tok-while_stmt">while_stmt</a></code></td></tr> | |
117 | <tr> | |
118 | <td></td> | |
119 | <td></td> | |
120 | <td><code>| <a class='grammartoken' href="for.html#tok-for_stmt">for_stmt</a></code></td></tr> | |
121 | <tr> | |
122 | <td></td> | |
123 | <td></td> | |
124 | <td><code>| <a class='grammartoken' href="try.html#tok-try_stmt">try_stmt</a></code></td></tr> | |
125 | <tr> | |
126 | <td></td> | |
127 | <td></td> | |
128 | <td><code>| <a class='grammartoken' href="function.html#tok-funcdef">funcdef</a></code></td></tr> | |
129 | <tr> | |
130 | <td></td> | |
131 | <td></td> | |
132 | <td><code>| <a class='grammartoken' href="class.html#tok-classdef">classdef</a></code></td></tr> | |
133 | <tr> | |
134 | <td><a id='tok-suite' xml:id='tok-suite'>suite</a></td> | |
135 | <td>::=</td> | |
136 | <td><a class='grammartoken' href="compound.html#tok-stmt_list">stmt_list</a> NEWLINE | |
137 | | NEWLINE INDENT <a class='grammartoken' href="compound.html#tok-statement">statement</a>+ DEDENT</td></tr> | |
138 | <tr> | |
139 | <td><a id='tok-statement' xml:id='tok-statement'>statement</a></td> | |
140 | <td>::=</td> | |
141 | <td><a class='grammartoken' href="compound.html#tok-stmt_list">stmt_list</a> NEWLINE | <a class='grammartoken' href="compound.html#tok-compound_stmt">compound_stmt</a></td></tr> | |
142 | <tr> | |
143 | <td><a id='tok-stmt_list' xml:id='tok-stmt_list'>stmt_list</a></td> | |
144 | <td>::=</td> | |
145 | <td><a class='grammartoken' href="simple.html#tok-simple_stmt">simple_stmt</a> (";" <a class='grammartoken' href="simple.html#tok-simple_stmt">simple_stmt</a>)* [";"]</td></tr> | |
146 | </table> | |
147 | </div> | |
148 | <a class="grammar-footer" | |
149 | href="grammar.txt" type="text/plain" | |
150 | >Download entire grammar as text.</a> | |
151 | </dd></dl> | |
152 | ||
153 | <P> | |
154 | Note that statements always end in a | |
155 | <code>NEWLINE</code><a id='l2h-571' xml:id='l2h-571'></a> possibly followed by a | |
156 | <code>DEDENT</code>.<a id='l2h-572' xml:id='l2h-572'></a> Also note that optional | |
157 | continuation clauses always begin with a keyword that cannot start a | |
158 | statement, thus there are no ambiguities (the `dangling | |
159 | <tt class="keyword">else</tt>' problem is solved in Python by requiring nested | |
160 | <tt class="keyword">if</tt> statements to be indented). | |
161 | <a id='l2h-569' xml:id='l2h-569'></a> | |
162 | <P> | |
163 | The formatting of the grammar rules in the following sections places | |
164 | each clause on a separate line for clarity. | |
165 | ||
166 | <P> | |
167 | ||
168 | <p><br /></p><hr class='online-navigation' /> | |
169 | <div class='online-navigation'> | |
170 | <!--Table of Child-Links--> | |
171 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
172 | ||
173 | <UL CLASS="ChildLinks"> | |
174 | <LI><A href="if.html">7.1 The <tt class="keyword">if</tt> statement</a> | |
175 | <LI><A href="while.html">7.2 The <tt class="keyword">while</tt> statement</a> | |
176 | <LI><A href="for.html">7.3 The <tt class="keyword">for</tt> statement</a> | |
177 | <LI><A href="try.html">7.4 The <tt class="keyword">try</tt> statement</a> | |
178 | <LI><A href="function.html">7.5 Function definitions</a> | |
179 | <LI><A href="class.html">7.6 Class definitions</a> | |
180 | </ul> | |
181 | <!--End of Table of Child-Links--> | |
182 | </div> | |
183 | ||
184 | <DIV CLASS="navigation"> | |
185 | <div class='online-navigation'> | |
186 | <p></p><hr /> | |
187 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
188 | <tr> | |
189 | <td class='online-navigation'><a rel="prev" title="6.14 The exec statement" | |
190 | href="exec.html"><img src='../icons/previous.png' | |
191 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
192 | <td class='online-navigation'><a rel="parent" title="Python Reference Manual" | |
193 | href="ref.html"><img src='../icons/up.png' | |
194 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
195 | <td class='online-navigation'><a rel="next" title="7.1 The if statement" | |
196 | href="if.html"><img src='../icons/next.png' | |
197 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
198 | <td align="center" width="100%">Python Reference Manual</td> | |
199 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
200 | href="contents.html"><img src='../icons/contents.png' | |
201 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
202 | <td class='online-navigation'><img src='../icons/blank.png' | |
203 | border='0' height='32' alt='' width='32' /></td> | |
204 | <td class='online-navigation'><a rel="index" title="Index" | |
205 | href="genindex.html"><img src='../icons/index.png' | |
206 | border='0' height='32' alt='Index' width='32' /></A></td> | |
207 | </tr></table> | |
208 | <div class='online-navigation'> | |
209 | <b class="navlabel">Previous:</b> | |
210 | <a class="sectref" rel="prev" href="exec.html">6.14 The exec statement</A> | |
211 | <b class="navlabel">Up:</b> | |
212 | <a class="sectref" rel="parent" href="ref.html">Python Reference Manual</A> | |
213 | <b class="navlabel">Next:</b> | |
214 | <a class="sectref" rel="next" href="if.html">7.1 The if statement</A> | |
215 | </div> | |
216 | </div> | |
217 | <hr /> | |
218 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
219 | </DIV> | |
220 | <!--End of Navigation Panel--> | |
221 | <ADDRESS> | |
222 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
223 | </ADDRESS> | |
224 | </BODY> | |
225 | </HTML> |