Java学习ing(2)(循环):

版权声明:随笔罢了,尽管拿去吧 https://blog.csdn.net/chunbaizhi/article/details/83579942

#Java学习ing(2):

  • 循环:
    • while:
while(判断句);
输入:
int i = 0;
while (true)
	{System.out.println(++i);
	if (i ==5)
		break;}
输出:
0
1
2
3
4
5

    • for循环:
for ('起点';‘范围’;‘步长’)
输入:
for(int i = 0; i<5;++i)
	{
	System.out.println(i)
	}
输出:
5
1
2
3
4
  • 跳出循环:
    • break
输入:
while 1:
	{System.out.println(++i);
	if i ==5;
		break
输出:
1
2
3
4
5
	}
    • continue
输入:
while 1:
	{System.out.println(++i);
	continue;
	if i == 5;
		break
	}	
输出:
1
2
3
4
5
  • i++ 和 ++i的区别:
    • i++(a为一个临时设定的值):
i = a
i = i+1
    • ++i(a为一个临时设定的值)
i = i+1
a = i

我的一个学长跟我说++i比i++更加高效

至于为啥我也不知道,等我学的更精深再回来改进吧

猜你喜欢

转载自blog.csdn.net/chunbaizhi/article/details/83579942