# 循环语句

Python 中的循环语句有 for 和 while。

## while循环

```python
while 判断条件(condition)：
    执行语句(statements)……
```

实例:

```python
n = 100

sum = 0
counter = 1
while counter <= n:
    sum = sum + counter
    counter += 1

print("1 到 %d 之和为: %d" % (n,sum))
```

> 以上实例输出结果：\
> 1 到 100 之和为: 5050

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-1.py)

### 无限循环

```python
var = 1
while var == 1 :  # 表达式永远为 true
   num = int(input("输入一个数字  :"))
   print ("你输入的数字是: ", num)

print ("Good bye!")
```

**注意：以上的无限循环你可以使用 CTRL+C 来中断循环。**

> 以上实例输出结果：\
> 输入一个数字 :5\
> 你输入的数字是: 5\
> 输入一个数字 :

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-2.py)

### while循环使用else语句

```python
while <expr>:
    <statement(s)>
else:
    <additional_statement(s)>
```

实例:

```python
count = 0
while count < 5:
   print (count, " 小于 5")
   count = count + 1
else:
   print (count, " 大于或等于 5")
```

> 以上实例输出结果：\
> 0 小于 5\
> 1 小于 5\
> 2 小于 5\
> 3 小于 5\
> 4 小于 5\
> 5 大于或等于 5

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-3.py)

### 简单语句组

类似if语句的语法，如果你的while循环体中只有一条语句，你可以将该语句与while写在同一行中。

实例：

```python
flag = 1

while (flag): print ('欢迎访问菜鸟教程!')

print ("Good bye!")
```

**注意：以上的无限循环你可以使用 CTRL+C 来中断循环。**

> 以上实例输出结果：\
> 欢迎访问菜鸟教程!\
> 欢迎访问菜鸟教程!\
> 欢迎访问菜鸟教程!\
> 欢迎访问菜鸟教程!\
> ······

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-4.py)

## for语句

for循环可以遍历任何序列的项目，如一个列表或者一个字符串。

```python
for <variable> in <sequence>:
    <statements>
else:
    <statements>
```

实例：

```python
languages = ["C", "C++", "Perl", "Python"] 
for x in languages:
    print (x)
```

> 以上实例输出结果：\
> C\
> C++\
> Perl\
> Python

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-5.py)

### range()函数

如果你需要遍历数字序列，可以使用内置range()函数。它会生成数列。

实例1：

```python
for i in range(5):
    print(i)
```

> 以上实例输出结果：\
> 0\
> 1\
> 2\
> 3\
> 4

可以使用range指定区间的值。

实例2：

```python
for i in range(5,9):
    print(i)
```

> 以上实例输出结果：\
> 5\
> 6\
> 7\
> 8

可以使range以指定数字开始并指定不同的增量(甚至可以是负数)

实例3：

```python
for i in range(0,10,3):
    print(i)
```

> 以上实例输出结果：\
> 0\
> 3\
> 6\
> 9

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-6.py)

## break和continue语句

break语句可以跳出for和while的循环体。**如果你从for或while循环中终止，任何对应的循环else块将不执行。**

continue语句被用跳过当前循环块中的剩余语句，然后继续进行下一轮循环。

break实例：

```python
n = 5
while n > 0:
    n -= 1
    if n == 2:
        break
    print(n)
print('循环结束。')
```

> 以上实例输出结果：\
> 4\
> 3\
> 循环结束。

continue实例：

```python
n = 5
while n > 0:
    n -= 1
    if n == 2:
        continue
    print(n)
print('循环结束。')
```

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-7.py)

> 以上实例输出结果：\
> 4\
> 3\
> 1\
> 0\
> 循环结束。

## pass语句

Python pass是空语句，是为了保持程序结构的完整性。

pass不做任何事情，一般用做占位语句。

实例：

```python
while True:
    pass  # 等待键盘中断 (Ctrl+C)
```

最小的类实例:

```python
class MyEmptyClass:
    pass
```

[demo](https://github.com/lujw666/learn/blob/develop/python/00-basic/05-conditions\&cycles/02-循环语句-8.py)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lujw666.gitbook.io/learn/python/00-basic/00-conditions-and-cycles/02-xun-huan-yu-ju.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
