Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / ref / compound.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ref.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ref.html" title='Python Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="top-level.html" />
<link rel="prev" href="simple.html" />
<link rel="parent" href="ref.html" />
<link rel="next" href="if.html" />
<meta name='aesop' content='information' />
<title>7. Compound statements</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.14 The exec statement"
href="exec.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="Python Reference Manual"
href="ref.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.1 The if statement"
href="if.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="exec.html">6.14 The exec statement</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ref.html">Python Reference Manual</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="if.html">7.1 The if statement</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION009000000000000000000"></A><A NAME="compound"></A>
<BR>
7. Compound statements
</H1>
<a id='l2h-568' xml:id='l2h-568'></a>
<P>
Compound statements contain (groups of) other statements; they affect
or control the execution of those other statements in some way. In
general, compound statements span multiple lines, although in simple
incarnations a whole compound statement may be contained in one line.
<P>
The <tt class="keyword">if</tt>, <tt class="keyword">while</tt> and <tt class="keyword">for</tt> statements implement
traditional control flow constructs. <tt class="keyword">try</tt> specifies exception
handlers and/or cleanup code for a group of statements. Function and
class definitions are also syntactically compound statements.
<P>
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 <tt class="keyword">if</tt> clause a following <tt class="keyword">else</tt> clause would
belong:
<a id='l2h-570' xml:id='l2h-570'></a>
<P>
<div class="verbatim"><pre>
if test1: if test2: print x
</pre></div>
<P>
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
<tt class="keyword">print</tt> statements are executed:
<P>
<div class="verbatim"><pre>
if x &lt; y &lt; z: print x; print y; print z
</pre></div>
<P>
Summarizing:
<P>
<dl><dd class="grammar">
<div class="productions">
<table>
<tr>
<td><a id='tok-compound_stmt' xml:id='tok-compound_stmt'>compound_stmt</a></td>
<td>::=</td>
<td><a class='grammartoken' href="if.html#tok-if_stmt">if_stmt</a></td></tr>
<tr>
<td></td>
<td></td>
<td><code>| <a class='grammartoken' href="while.html#tok-while_stmt">while_stmt</a></code></td></tr>
<tr>
<td></td>
<td></td>
<td><code>| <a class='grammartoken' href="for.html#tok-for_stmt">for_stmt</a></code></td></tr>
<tr>
<td></td>
<td></td>
<td><code>| <a class='grammartoken' href="try.html#tok-try_stmt">try_stmt</a></code></td></tr>
<tr>
<td></td>
<td></td>
<td><code>| <a class='grammartoken' href="function.html#tok-funcdef">funcdef</a></code></td></tr>
<tr>
<td></td>
<td></td>
<td><code>| <a class='grammartoken' href="class.html#tok-classdef">classdef</a></code></td></tr>
<tr>
<td><a id='tok-suite' xml:id='tok-suite'>suite</a></td>
<td>::=</td>
<td><a class='grammartoken' href="compound.html#tok-stmt_list">stmt_list</a> NEWLINE
| NEWLINE INDENT <a class='grammartoken' href="compound.html#tok-statement">statement</a>+ DEDENT</td></tr>
<tr>
<td><a id='tok-statement' xml:id='tok-statement'>statement</a></td>
<td>::=</td>
<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>
<tr>
<td><a id='tok-stmt_list' xml:id='tok-stmt_list'>stmt_list</a></td>
<td>::=</td>
<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>
</table>
</div>
<a class="grammar-footer"
href="grammar.txt" type="text/plain"
>Download entire grammar as text.</a>
</dd></dl>
<P>
Note that statements always end in a
<code>NEWLINE</code><a id='l2h-571' xml:id='l2h-571'></a> possibly followed by a
<code>DEDENT</code>.<a id='l2h-572' xml:id='l2h-572'></a> Also note that optional
continuation clauses always begin with a keyword that cannot start a
statement, thus there are no ambiguities (the `dangling
<tt class="keyword">else</tt>' problem is solved in Python by requiring nested
<tt class="keyword">if</tt> statements to be indented).
<a id='l2h-569' xml:id='l2h-569'></a>
<P>
The formatting of the grammar rules in the following sections places
each clause on a separate line for clarity.
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="if.html">7.1 The <tt class="keyword">if</tt> statement</a>
<LI><A href="while.html">7.2 The <tt class="keyword">while</tt> statement</a>
<LI><A href="for.html">7.3 The <tt class="keyword">for</tt> statement</a>
<LI><A href="try.html">7.4 The <tt class="keyword">try</tt> statement</a>
<LI><A href="function.html">7.5 Function definitions</a>
<LI><A href="class.html">7.6 Class definitions</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.14 The exec statement"
href="exec.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="Python Reference Manual"
href="ref.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.1 The if statement"
href="if.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="exec.html">6.14 The exec statement</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ref.html">Python Reference Manual</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="if.html">7.1 The if statement</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>