杨桃的Python基础教程——第6章:Python控制结构(二)循环结构——while循环

本人CSDN博客专栏:https://blog.csdn.net/yty_7
Github地址:https://github.com/yot777/Python-Primary-Learning

6.2 循环结构——while循环

Pythonwhile循环的一般形式:

while 判断条件:

    statements

注意:

1、和if语句一样,要注意冒号和缩进。

2、在Python中没有do..while循环。

Python循环结构——while循环举例

n = 100
sum = 0
counter = 1
while counter <= n:
     sum = sum + counter
     counter += 1
print("Sum of 1 until %d is %d" %(n,sum))

运行结果:
Sum of 1 until 100 is 5050

Java循环结构——while循环举例

public class Test3 {
  public static void main(String[] args) {
    int n = 100;
    int sum = 0;
    int counter = 1;
    while(counter <= n){
      sum = sum + counter;
      counter += 1;
    }
    System.out.printf("Sum of 1 until %d is %d",n,sum);
  }
}

运行结果:
Sum of 1 until 100 is 5050

参考教程:

廖雪峰的Python教程

https://www.liaoxuefeng.com/wiki/1016959663602400

廖雪峰的Java教程

https://www.liaoxuefeng.com/wiki/1252599548343744

Python3 教程 | 菜鸟教程
https://www.runoob.com/python3/
 

如果您觉得本篇本章对您有所帮助,欢迎关注、评论、点赞!Github欢迎您的Follow、Star!
 

发布了25 篇原创文章 · 获赞 3 · 访问量 2165

猜你喜欢

转载自blog.csdn.net/yty_7/article/details/104162196