[java基础]循环结构1 循环结构:for循环,while循环,do_while循环在,增强型for循环 /** 文件路径:G:\JavaByHands\循环语句\ 文件名称:WhileTest.java 编写时间:2016/6/7 作 者:郑晨辉 编写说明:while do while 代码示例 */ public class WhileTest{ public static void main(String[] args){ //初始条件 int i = 0; //进入循环,while循环
int x=10; do { System.out.println("value of x:"+x); x++; } while(x<20); //do while循环 1 int x=10; 2 while(x<20) { 3 System.out.println("value of x:"+x); 4 x++; 5 } //while循环 while(布尔表达式) 只要布尔表达式内容为真 循环体就会一直循环下去 do while 即使不满足条件 也
循环结构 while 循环 只要表达式成立,循环就一直持续 我们大多数情况会让循环停下来,我们需要一个让表达式失效的方式,来结束循环 public static void main(String[] args) { // 输出 1+2+···+100 int i = 0; int sum =0; while (i<=100){ sum += i; i++ ; Syste
for while . break:退出循环 continue:退出本次循环 例子 for i range(0,101,2): print(i) -------------------------------------------------------------------------------- while n<=100: if (n>10): break prin t(i) n = n + 1 print ('end') 小结 循环是让计算机做重复任务的有效的方法. break语句
for循环格式: for index in range(0,3):#等同于range(3),取0\1\2 print(index) index = 0 starnames = ['xr1','xr2','xr3'] for index in range(len(starnames)): print(starnames[index]) 结果: xr1xr2xr3 range函数: range(1,5) 取1-4 range(1,5,2) 取1-4,1是起始下标,5是终止下标,步长为2 range(
1.使用while循环输入1234568910 n = 1 while n < 11: if n == 7: pass else: print(n) n = n + 1 2.求1 - 100的所有数的和 n = 1 s = 0 while n < 101: s = s + n n = n + 1 print(s) 3.输出1 - 100内的所有奇数 n = 1 while n < 101: temp = n % 2 if temp == 0: pass else: print(n) n
注:运行环境 Python3 1.循环语句 (1)for循环 注:for i in range(a, b): #从a循环至b-1 for i in range(n): #从0循环至n-1 import numpy as np # 导入NumPy库 if __name__ == "__main__": , ): #从1循环至2 print("i=",i) #打印i值 输出: i= 1i= 2 (2)while循环 import numpy as np #
1.使用while循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n < 11: if n == 7: pass else: print(n) n = n + 1 2.求1-100的所有数的和 n = 1 s = 0 while n < 101: s = s + n n = n + 1 print(s) 3.输出 1-100 内的所有奇数 n = 1 while n < 101: temp = n % 2 if temp == 1: print(n) el