一、循环结构

循环结构则是根据判断条件的成立与否,决定程序段落的执行次数,而这个程序段落就称为循环主体。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPEAAACGCAIAAACDjvsnAAAKYElEQVR4nO2dTWgTTwOH56Q5aMlBStEK9SD0ILIHEfHUg1+ghb0oXpSCHxQEiad6iLTgoQhCTlKoh+KpJwm0h+JpkSKhlJKClFJKKUEk5FDa4iEUD/s/7Ou8k93Z2Ul2kp2Z/J6DrNnJ7Hw8+WWym26ID4BdkKwbYDCEhEcv+gjoPZgDWUiEaIFoscQCbJnoLrxIOgBDJkuiXpJGsmaHqqXb0Q0gD4ZMFvFKQyBotAwrNHJaOWYM2fT0dKlUyrYNMsIl7vUjGYycVo4ZQ0YIGRkZybwNfpJtkjnt89YeYrmBPGYMmT5O+7x8ZcsIojpqM1uG+z4ApzvAjCHTymlfGNIyxvvI6W5ixpBl7nRUOG4G022B03E1w2lVmDFkmTvtR5YKfsTRuLVEqJJondzHueWBDGYMmQ5OA1OA08A2DHa6Xq8XCoXZ2dlMmgS0xUinA5tzuRwhZHp6Ort2AR0xzGnWZqAtnudlaUuGx5aH/HO62WyWSqWhoSE6fCMjI2NAG4KpgdPJkNa1B2s21h5aMTExAaelILzPiIHZc3NzmTQJcIHTsnCdBhoCp2WB06YAp2WB06YAp2WB06YAp2WB06YAp2WB06YAp5PxPM/zPELI0NCQ53mVSiXrFgERcDoZ13XZi66FQiHrFgERcDqZarVKhc7lcvV6PesWARFwWgoa1Qhp/YHTUgRRjZA2Ajgti+u6CGkjgNPJ1Ov1hYWFu3fvPn36tFwuN5vNrFsERMDpWCqVytu3bx3HGRoampiYWFxcnJubc103l8vdu3evVCptb29n3UbAAU63EETy48eP8/n8jRs3Zmdnq9VqtNjKykqhUBgdHR0ZGZmcnER4awWc9n1eJB8eHso8cX9/H+GtG/3rtGQky4Pw1oS+c7rjSJYH4R2H4zgd/LWstjiOE9fTrjutPJLlQXizELtuUyboTrf62YNIlgfh7cPpzsgw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gMuGjnN6uvHLyrinMZ6GgRo5LQfs/aIlhE8mL4xcNp0dHFavIYWrz0UrunhtAVk77TPaN1WtYKldpqWwGnT0cJptjYZO5XoG1cznDYdvZzOHDhtAXC6BThtAXC6BThtAXC6BThtAWmNzOfzxCLOnDkDp01HfcpWKhVPHTMzMzMzM4SQfD6fWEzhcQPgtIno7nQA+XcPyB4Dp00ETouA0yYCp0XAaROB0yLgtImod/rXr1/7qiGEDA8PK682Edx10kTMOLtM8NsXQBo4DWwDTgPbgNPANuA0sA04DWwDTgPbgNPANuA0sA3dnQ79BpLrulm3COiO7k6Xy2XWaXwBAySiu9M+E9UIaSCDAU7TqEZIAxkMcNr3fcdxENJAEjOcLpfLCGkgiRlOAyAPnAa28R+tqpf8Qfg41AAAAABJRU5ErkJggg==" alt="" />

二、While循环

while是循环语句,也是条件判断语句。当事先不知道循环该执行多少次的时,就要用到while循环。while循环的格式如下:
while (循环条件判断){
语句1 ;
语句2 ;
语句n ;
循环条件更改 ;
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAAD5CAIAAABeVMXbAAARFklEQVR4nO2dP4jb5sPHny0ebvBUDFlcaMB08hDKNZSiqdyQAw8dbmuGDoaUQ1PjwfBLe8MNPfAYCIUj042G63BkCOKGYjIEH2S4hhuuHoJrSkgggyk3PO+gt+pjPX8ly3oe6fl+hkOWHj3PI+nj7yP5ZItQwBEEASHk+PjYdkcApZQS2x1wETjqFHBUABx1Cji6wmQyiaKo2+0SQgaDQRRF8/ncdqd8B46uEIYhYWg0GnDUOnB0hfl83mg0EkfDMLTdIwBHOZIoRYg6AhxNk0QpQtQR4KiAMAwRou7guqPxJbYndLtd2/vbRVx3lBDXe1ggXm2sOa7vFK8Om1cba47rO8Wrw+bVxprj+k7x6rB5tbHmuL5TvDpsXm2sOa7vFK8Om1cba47rO8Wrw0YIuea4ubmx3S/LuG5ADkf5VZIPIHOsbt4BtmQynan/hJCIY7lcmtdQS2rrKPvZOF+AXST8OF3bB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ODqfz683yfb2NiHkl19+2Wgrhtzc3Nje36VSE0c3DZ59YxE4agQctQgcNQKOWgSOGgFHLQJHNUwmk+jf6/rBYBBF0fX1te1O+QUc1RCGYeo+j+l0artTfgFHNczn80ajkQja6/Vs98g74KgeNkoRouUDR/UkUYoQtQIcNSKOUoSoFeCoEfP5fG9vz3YvPKV6juJZjL5RPUeJZ9+5s90F+8BRp/FqY2XAUafxamNlwFGn8WpjZcBRp/FqY2XAUachhOA+fF8cTa0lq4SIftw0d+vrv50Inivig6PqDyAVlas9zlpbPuAo9cFR4VomGZk7a5NFhm8MdbdZO3/77bfvvvvuzz//NK+hBnjkqFaUZCaflDkqNFHcpNusnVtbW4SQP/74w7yGGuCRo+pKWM9S2qXmC+sxzGD1ImFh1s6YL774IvCJ+L/fvjuaBCdlRBT6x0csn6ayMjn6Twj56quvFM35gxeOplDUTHSO5uinsDaTSqIoGo1G7J00P/zww//8YzQa/bdbzPegFXI7qq2Ed5S3OeWrQvoCHY05ODj47LPPiH/nozy1ctRkEOFrTuWorHV1vsoayu1oYupff/1lXkMtqZWjshUzjfWyRVpHtd0w6TM+H+Wpp6MmDimijl2UmpaN9eaVq9eCozx1c1QhkPm5ptAwbTCb90SxChzlqZujlQaOCnHdADgKR103AI7CUdcNgKNw1HUD4Cgcdd0Ar57FuLW1BUd5XHdUCJ4r4hVwVAAcdQo4KgCOOgUcFQBHnQKOCoCjTgFHBcBRp6iko5smwLNvXAKOCoCjTgFHBcBRp4CjAuCoU8BRAXDUKeCoADjqFHBUABx1CjgqAI46BRwVAEedAo4KgKNOAUcFwFGngKMrhGEYBEF883+n0wmC4OzszHanfAeOrjAej9kvb7RaLdzSYR04mob9XUX2FwaBLeBomiRKEaKOAEcFxFGKEHUEOCpgPB4jRN0BjorB5bw7wFHgOnAUuM7/ATOnsJn3BICZAAAAAElFTkSuQmCC" alt="" />

示例一、使用While进行100以内累加操作

package loop;

public class test1 {

    public static void main(String[] args) {
// 使用While进行100以内累加操作
int sum=0; //定义变量保存累加的值
int i=1; //定义整型变量i
while(i<=100){
sum+=i;
i++;
}
System.out.println("100以内整数之和: "+sum);
} }

结果:100以内整数之和: 5050

While是先判断条件是否成立,如果成立则进入循环,不成立则跳过循环

示例二、同上i的值变为101

package loop;

public class test {

    public static void main(String[] args) {
// 使用While进行100以内累加操作
int sum=0; //定义变量保存累加的值
int i=101; //定义整型变量i
while(i<=100){
sum+=i;
i++;
}
System.out.println("100以内整数之和: "+sum); } }

结果:100以内整数之和: 0

三、do-while循环

do…while循环也是用于未知循环执行次数的时候,而while循环及do…while循环最大不同就是进入while循环前,while语句会先测试判断条件的真假,再决定是否执行循环主体,而do…while循环则是“先做再说”,每次都是先执行一次循环主体,然后再测试判断条件的真假,所以无论循环成立的条件是什么,使用do…while循环时,至少都会执行一次循环主体,语句格式:
do{
语句1 ;
语句2 ;
….
语句n ;
循环条件改变 ;
}while (循环条件判断);
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS8AAACuCAIAAADoJxxGAAAOwklEQVR4nO2dP2gbSR+Gp8qpMHeqDkOOY68IuBQkHCaVODhwcQKVhhBwExAkBHWXQiBDICZgUHUY0pirVB0CN+aKoMKFIClkFyG4CCKkECKFc4QgUoT9ikn2W+3szP72z2h3Zt6niFer2dnZnXnm3ZUUifmAhud5jLHZbFZ2Q4C1sLIbYAywEegGNlKBjUA3sJEKbAS6gY1UYCPQDWxMZjKZjMfjzc1NxthwOByPx8vlsuxGAQuBjcl0u10WotFolN0iYCewMZn5fF6r1QIbR6NR2S0CdgIbSQTxiGAE+oCNJIJ4RDACfcBGKt1uF8EItGK2jY1Gg9kL5HcNs21kzOz2q7H76ICI2f1t93i1++iAiNn9bfd4tfvogIjZ/W33eLX76ICI2f1t93i1++iAiNn9bfd4ZYxdrVJ2i4BezB7NqWwMCovvJYglFWUoO01bXlbJeJVs9QBTcMXGsIqx29LFo1iteBeRfHCw0TmcsDHRE1mFqYyV1YNsBEScsDFcWJaNsStl9attpIQnscGw0Slcs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GEvkDEzFYFPlPWmWBjenIFpiIwQLJefaqDGzMDj0wEYMFcmUvHz9+zHZOYOP/oQdmWhyPQUAENsZDDEw1iEGQCtiYQNrARAyCzMDGFCgCEzEI8gMbsxAEJmIQFAhsBKAqwEYAqgJsBKAq/A9g1uCbYToZawAAAABJRU5ErkJggg==" alt="" />
 

示例三、使用do-While进行100以内累加操作

package loop;

public class test {

    public static void main(String[] args) {
// 使用While进行100以内累加操作
int sum=0; //定义变量保存累加的值
int i=1; //定义整型变量i
do{
sum+=i;
i++;
}while(i<=100);
System.out.println("100以内整数之和: "+sum); } }

结果:100以内整数之和: 5050

四、for循环

对于while和do…while两种循环来讲,操作时并不一定非要明确的知道循环的次数,而如果开发者已经明确的知道了循环次数的话,那么就可以使用另外一种循环语句——for循环。 格式如下:
for (赋值初值;判断条件;赋值增减量){
语句1 ;
….
语句n ;
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANAAAADvCAIAAAAM6s1bAAAQiUlEQVR4nO2dPWjcVgDH39TeENorlOCmGa5DweNBSzlKC0ehcEMN16mGUPBSOGgp2prB4EChJmC4qQSSwXTyVA4MxXQIN3gQJMM5QzAegjEZjsOEpBRjMoTX4bmSTu9DT9LTk/T0/w2HTnp670n63V8fp9MRCv6n0+kQQk5PT8vuiMuQsjtQISCcBSBcCISzAIQLgXAWgHAhEM4CEI5SSj3P6/f7rVaLENLr9QaDweXlZdmdchMIRymlk8mERPA8r+weOQuEu6Lb7TLbWq3WfD4vuzvOAuGuCEIO8VYoEC6k2+0i3ooGwoVMJhPEW9GUKVxw2ARkdLvdEjdQEZQpHCHI1wTcW0UQrtK4t4ogXKVxbxVBuErj3iqCcJXGvVUE4SoNIWS2zIsXL8ruVC5qLBw/e3SMrPJgPH8Ngi+pKKPT+bTlhTVMl6n7dekaCBfb3vxbYUmhKIoBYVuKfuqIm9gfnWWHcOba1hZOWF4onKLyRBVk86aSUr8/OkA4o22nSTgqD4xYbeo4iWkq7INaOOF4YTd0+pO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Fw0C4SuPe4kO4SuPe4kO4SuPe4kO4SuPe4kO4SkMIebnMxcVF2Z3KhfvCEeXjZPiRsreyKxdme8v3DbeYWyXzJky8xCVrgm9R3yodKdMuBYSzirWEiyaZoiqFRrIZDQr34MGD3377LXNtVaARwqnDhhdOEUsyL9WaZus5jQj34MGDL774ghDy7bffZq6tCjRCOEVtgVsxKWP+8TXwIzV32Wk7H6jGeP/99/t1ht1E7bhwsoSLiRIbKSumI5ZB4aK2OYPjwqlrI6JjuMS59BvKswiEkOj+lBDy5ZdfFvQTa5tEfyYC4a7g5+I/qbF6hJPyC8dg2tX9GI7HEeESUz3WRCzh+AGhYbK3RQjHePToUebaqokjwslml41RlAxUUwun3wd9eOHqfh2OxxHh+ECS1Zm4x9TcpQo7kP8DA+GsknOD1R0IZxsIB+GsAuEgnFUgHISzCoSDcFbB/zRAuJLBM37L3gKGgXAhEM4CEC4EwlkAwoVAOAtAuBAIZ4GqC/fvv/+e2uLmzZuEkMPDQ2stJnJ+fl72FjBM1YWzCR4qbQEIFwLhLADhQiCcBSBcCISzAIQLgXAWgHAhEM4CEC4EwlkAwoVAOAtAuBAIZwEIFwLhLADhKKV0e3u73++zf6Lp9XqDwQD/JlgQEI5SSmezWfTOW8/zyu6Rs0C4K4bDIbOt1WrN5/Oyu+MsEO6KIOQQb4UC4UKGwyHirWggXMhsNkO8FQ2EWwInp0XzH1e5jKIfcEc4AAAAAElFTkSuQmCC" alt="" />

示例四、使用for进行100以内累加操作

package loop;

public class test2 {

    public static void main(String[] args) {
// 使用for进行100以内累加操作
int sum=0; //定义变量保存累加的值
for(int i=1; i<=100;i++){
sum=sum+i;
}
System.out.println("100以内整数之和: "+sum); } }

结果:100以内整数之和: 5050

4.1循环嵌套

示例五、for循环打印99乘法表

package loop;

public class test3 {

    public static void main(String[] args) {
// 使用for循环打印99乘法表
for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
} } }

结果:

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

五、break语句

break语句可以强迫程序中断循环,当程序执行到break语句时,即会离开循环,继续执行循环外的下一个语句,如果break语句出现在嵌套循环中的内层循环,则break 语句只会跳出当前层的循环。以下图的for循环为例,在循环主体中有break语句时,当程序执行到break,即会离开循环主体,而继续执行循环外层的语句。

示例六、实现输出1-10,遇到4时使用break;

package loop;

public class test4 {

    public static void main(String[] args) {
// 实现输出1-10,遇到4时程序退出
for(int i=1;i<=10;i++){
if (i==4){
break;
}
System.out.print(i+" ");
}
System.out.println("循环结束");
} }

结果:1 2 3 循环结束

六、continue语句

continue语句可以强迫程序跳到循环的起始处,当程序运行到continue语句时,即会停止运行剩余的循环主体,而是回到循环的开始处继续运行。以下图的for循环为例,在循环主体中有continue语句,当程序执行到continue,即会回到循环的起点,继续执行循环主体的部分语句。
continue是跳过当前循环进入下次循环

示例七、同上实现输出1-10,遇到4时使用continue;

package loop;

public class test4 {

    public static void main(String[] args) {
// 实现输出1-10,遇到4时使用continue
for(int i=1;i<=10;i++){
if (i==4){
continue;
}
System.out.print(i+" ");
}
System.out.println("循环结束");
} }

结果:1 2 3 5 6 7 8 9 10 循环结束

七、return语句

结束当前方法的执行并退出,返回调用该方法的语句处。

示例入、同上实现输出1-10,遇到4时使用return;

package loop;

public class test4 {

    public static void main(String[] args) {
// 实现输出1-10,遇到4时使用return
for(int i=1;i<=10;i++){
if (i==4){
return;
}
System.out.print(i+" ");
}
System.out.println("循环结束");
} }

结果:1 2 3

可以看到“循环结束”没有输出,当i=4时,满足执行return,结束整个方法的执行

Java从零开始学八(循环结构)的更多相关文章

  1. Java从零开始学七(选择结构)

    一. 程序的结构: 一般来说程序的结构包含有下面三种: 1.顺序结构 2.选择结构 3.循环结构 二.顺序结构 程序至上而下逐行执行,一条语句执行完之后继续执行下一条语句,一直到程序的末尾

  2. 051 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 13 Eclipse下程序调试——debug入门1

    051 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 13 Eclipse下程序调试--debug入门1 本文知识点: 程序调试--debug入门1 程序 ...

  3. 053 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 15 流程控制知识总结

    053 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 15 流程控制知识总结 本文知识点: 流程控制知识总结 流程控制知识总结 选择结构语句 循环结构语句 ...

  4. 052 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 14 Eclipse下程序调试——debug2 多断点调试程序

    052 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 14 Eclipse下程序调试--debug2 多断点调试程序 本文知识点: Eclipse下程序调 ...

  5. 050 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 12 continue语句

    050 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 12 continue语句 本文知识点:continue语句 continue语句 continue ...

  6. 049 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 11 break语句

    049 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 11 break语句 本文知识点:break语句 break语句 break语句前情回顾 1.swi ...

  7. 048 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 10 案例——阶乘的累加和

    048 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 10 案例--阶乘的累加和 本文知识点:通过案例练习嵌套循环应用 案例练习--阶乘的累加和 案例题目 ...

  8. 047 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 09 嵌套while循环应用

    047 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 09 嵌套while循环应用 本文知识点:嵌套while循环应用 什么是循环嵌套? 什么是循环嵌套? ...

  9. 046 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 08 for循环的注意事项

    046 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 08 for循环的注意事项 本文知识点:for循环的注意事项 for循环的注意事项 for循环有3个 ...

随机推荐

  1. Alpha冲刺(4/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  2. Git_解决冲突

    人生不如意之事十之八九,合并分支往往也不是一帆风顺的. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1 Switched to a new ...

  3. org.springframework.orm.hibernate3.LocalSessionFactoryBean

    Spring整合hibernate在配置sessionFactory时, 启动总是报出javax.transaction.TransactionManager找不到. 原因是:缺少jar包,jta-1 ...

  4. Window 8 启用 Telnet 命令工具一览图

    Window 8 启用 Telnet 命令工具一览图 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创 ...

  5. Spring安全权限管理(Spring Security)

    1.spring Security简要介绍 Spring Security以前叫做acegi,是后来才成为Spring的一个子项目,也是目前最为流行的一个安全权限管理框架,它与Spring紧密结合在一 ...

  6. ActiveX控件开发

    VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程 开篇语:最近在弄ocx控件发布到asp.net网站上使用,就是用户在使用过程中,自动下载安装ocx控件.(此文章也是 ...

  7. noise_process.c

    #include <math.h>#include "otdr_const.h"#include "haar.h"#include "ot ...

  8. Java网络编程技术1

    1. Java网络编程常用API 1.1 InetAddress类使用示例 1.1.1根据域名查找IP地址 获取用户通过命令行方式指定的域名,然后通过InetAddress对象来获取该域名对应的IP地 ...

  9. [转]我花了一个五一终于搞懂了OpenLDAP

    轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDAP)是一个开放的,中立的,工业标准的应用协议,通过IP协议提供访问控制和维护分布式信息的 ...

  10. 安全开发 | 如何让Django框架中的CSRF_Token的值每次请求都不一样

    前言 用过Django 进行开发的同学都知道,Django框架天然支持对CSRF攻击的防护,因为其内置了一个名为CsrfViewMiddleware的中间件,其基于Cookie方式的防护原理,相比基于 ...