杨桃的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!
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值