## 8.1 The if statement 

The if statement is used for conditional execution: 

if_stmt ::= "if" assignment_expression ":" suite ("elif" assignment_expression ":" suite )* ["else" ":" suite ] 

It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the if statement is executed or evaluated). If all expressions are false, the suite of the$_{else}$clause, if present, is executed. 

## 8.2 The while statement 

The$_{while}$ statement is used for repeated execution as long as an expression is true: 

while_stmt ::= "while" assignment_expression ":" suite ["else" ":" suite ] 

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the$_{else}$clause, if present, is executed and the loop terminates. 

A $_{break}$statement executed in the first suite terminates the loop without executing the$_{else}$ clause’s suite. A $_{continue}$statement executed in the first suite skips the rest of the suite and goes back to testing the expression. 

## 8.3 The for statement 

The$_{for}$ statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: 

for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite ] 

The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list . The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements ), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a$_{StopIteration}$ exception), the suite in the$_{else}$clause, if present, is executed, and the loop terminates. 

A $_{break}$statement executed in the first suite terminates the loop without executing the$_{else}$ clause’s suite. A $_{continue}$statement executed in the first suite skips the rest of the suite and continues with the next item, or with the$_{else}$clause if there is no next item. 

The for-loop makes assignments to the variables in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: 

for i in range(10 ): print(i) i =5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range 

