<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7. Compound statements</TITLE>
<META NAME="description" CONTENT="7. Compound statements">
<META NAME="keywords" CONTENT="ref">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ref.css">
<LINK REL="next" HREF="top-level.html">
<LINK REL="previous" HREF="simple.html">
<LINK REL="up" HREF="ref.html">
<LINK REL="next" HREF="if.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="if.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="ref.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="exec.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="if.html">7.1 The if statement</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="ref.html">Python Reference Manual</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="exec.html">6.13 The exec statement</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009000000000000000000"></A>
<BR>
7. Compound statements
</H1>
<a name="l2h-669"></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 name="l2h-670"></a>
<a name="l2h-671"></a>

<P>
<dl><dd><pre class=verbatim>
if test1: if test2: print x
</pre></dl>

<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>
<dl><dd><pre class=verbatim>
if x &lt; y &lt; z: print x; print y; print z
</pre></dl>

<P>
Summarizing:

<P>
<dl><dd><pre class=verbatim>
compound_stmt:  if_stmt | while_stmt | for_stmt
              | try_stmt | funcdef | classdef
suite:          stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement:      stmt_list NEWLINE | compound_stmt
stmt_list:      simple_stmt (";" simple_stmt)* [";"]
</pre></dl>

<P>
Note that statements always end in a
<tt>NEWLINE</tt><a name="l2h-672"></a> possibly followed by a
<tt>DEDENT</tt>.<a name="l2h-673"></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 name="l2h-674"></a>

<P>
The formatting of the grammar rules in the following sections places
each clause on a separate line for clarity.

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html1173"
 HREF="if.html">7.1 The <tt class=keyword>if</tt> statement</A>
<LI><A NAME="tex2html1174"
 HREF="while.html">7.2 The <tt class=keyword>while</tt> statement</A>
<LI><A NAME="tex2html1175"
 HREF="for.html">7.3 The <tt class=keyword>for</tt> statement</A>
<LI><A NAME="tex2html1176"
 HREF="try.html">7.4 The <tt class=keyword>try</tt> statement</A>
<LI><A NAME="tex2html1177"
 HREF="function.html">7.5 Function definitions</A>
<LI><A NAME="tex2html1178"
 HREF="class.html">7.6 Class definitions</A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="if.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="ref.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="exec.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="if.html">7.1 The if statement</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="ref.html">Python Reference Manual</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="exec.html">6.13 The exec statement</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
