本文共 4861 字,大约阅读时间需要 16 分钟。
更多免费内容,点击了解:
目录
控制流程系列教材 (一)- Java的If 条件语句
条件判断
示例 1 : if
如果表达式1的值是true,
就执行表达式2
public class HelloWorld { public static void main(String[] args) { boolean b = true ; //如果成立就打印yes if (b){ System.out.println( "yes" ); } } } |
示例 2 : 多表达式与一个表达式
public class HelloWorld { public static void main(String[] args) { boolean b = false ; //如果有多个表达式,必须用大括弧包括起来 if (b){ System.out.println( "yes1" ); System.out.println( "yes2" ); System.out.println( "yes3" ); } //否则表达式2 3 无论b是否为true都会执行 if (b) System.out.println( "yes1" ); System.out.println( "yes2" ); System.out.println( "yes3" ); //如果只有一个表达式可以不用写括弧,看上去会简约一些 if (b){ System.out.println( "yes1" ); } if (b) System.out.println( "yes1" ); } } |
示例 3 : if 使用过程中可能遇到的坑
在第6行,if后面有一个分号; 而
如果b为true,会执行这个分号,然后打印yes
如果b为false,不会执行这个分号,然后打印yes
这样,看上去无论如何都会打印yes
public class HelloWorld { public static void main(String[] args) { boolean b = false ; if (b); System.out.println( "yes" ); } } |
示例 4 : if else
else 代表不成立的情况
public class HelloWorld { public static void main(String[] args) { boolean b = false ; if (b) System.out.println( "yes" ); else System.out.println( "no" ); } } |
示例 5 : else if
else if 是多条件判断
public class HelloWorld { public static void main(String[] args) { //如果只使用 if,会执行4次判断 int i = 2 ; if (i== 1 ) System.out.println( 1 ); if (i== 2 ) System.out.println( 2 ); if (i== 3 ) System.out.println( 3 ); if (i== 4 ) System.out.println( 4 ); //如果使用else if, 一旦在18行,判断成立, 20行和22行的判断就不会执行了,节约了运算资源 if (i== 1 ) System.out.println( 1 ); else if (i== 2 ) System.out.println( 2 ); else if (i== 3 ) System.out.println( 3 ); else if (i== 4 ) System.out.println( 4 ); } } |
控制流程系列教材 (二)- java的switch语句
switch 语句相当于 if else的另一种表达方式
示例 1 : switch
switch可以使用byte,short,int,char,String,enum
注: 每个表达式结束,都应该有一个break;
注: String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数
注: enum是枚举类型,在章节有详细讲解
public class HelloWorld { public static void main(String[] args) { //如果使用if else int day = 5 ; if (day== 1 ) System.out.println( "星期一" ); else if (day== 2 ) System.out.println( "星期二" ); else if (day== 3 ) System.out.println( "星期三" ); else if (day== 4 ) System.out.println( "星期四" ); else if (day== 5 ) System.out.println( "星期五" ); else if (day== 6 ) System.out.println( "星期六" ); else if (day== 7 ) System.out.println( "星期天" ); else System.out.println( "这个是什么鬼?" ); //如果使用switch switch (day){ case 1 : System.out.println( "星期一" ); break ; case 2 : System.out.println( "星期二" ); break ; case 3 : System.out.println( "星期三" ); break ; case 4 : System.out.println( "星期四" ); break ; case 5 : System.out.println( "星期五" ); break ; case 6 : System.out.println( "星期六" ); break ; case 7 : System.out.println( "星期天" ); break ; default : System.out.println( "这个是什么鬼?" ); } } } |
控制流程系列教材 (三)- java的while语句
while和do-while循环语句
示例 1 : 条件为true时 重复执行
只要while中的表达式成立,就会不断地循环执行
public class HelloWorld { public static void main(String[] args) { //打印0到4 int i = 0 ; while (i< 5 ){ System.out.println(i); i++; } } } |
示例 2 : 条件为true时 重复执行,至少会执行一次
与while的区别是,无论是否成立,先执行一次,再进行判断
public class HelloWorld { public static void main(String[] args) { //打印0到4 //与while的区别是,无论是否成立,先执行一次,再进行判断 int i = 0 ; do { System.out.println(i); i++; } while (i< 5 ); } } |
控制流程系列教材 (四)- java的for循环
for循环,和while一样,只是表达方式不一样
示例 1 : for
比较for和while
public class HelloWorld { public static void main(String[] args) { //使用while打印0到4 int i = 0 ; while (i< 5 ){ System.out.println( "while循环输出的" +i); i++; } //使用for打印0到4 for ( int j = 0 ; j < 5 ; j++) { System.out.println( "for 循环输出的" +j); } } } |
控制流程系列教材 (五)- java 的 continue语句
continue
继续下一次循环
示例 1 : continue
如果是双数,后面的代码不执行,直接进行下一次循环
public class HelloWorld { public static void main(String[] args) { //打印单数 for ( int j = 0 ; j < 10 ; j++) { if ( 0 ==j% 2 ) continue ; //如果是双数,后面的代码不执行,直接进行下一次循环 System.out.println(j); } } } |
控制流程系列教材 (六)- java的break语句
结束循环
示例 1 : break;
直接结束当前for循环
public class HelloWorld { public static void main(String[] args) { //打印单数 for ( int j = 0 ; j < 10 ; j++) { if ( 0 ==j% 2 ) break ; //如果是双数,直接结束循环 System.out.println(j); } } } |
控制流程系列教材 (七)- java 如何结束外部for循环
break是结束当前循环
示例 1 : 结束当前循环
break; 只能结束当前循环
public class HelloWorld { public static void main(String[] args) { //打印单数 for ( int i = 0 ; i < 10 ; i++) { for ( int j = 0 ; j < 10 ; j++) { System.out.println(i+ ":" +j); if ( 0 ==j% 2 ) break ; //如果是双数,结束当前循环 } } } } |
示例 2 : 使用boolean变量结束外部循环
借助boolean变量结束外部循环
需要在内部循环中修改这个变量值
每次内部循环结束后,都要在外部循环中判断,这个变量的值
public class HelloWorld { public static void main(String[] args) { boolean breakout = false ; //是否终止外部循环的标记 for ( int i = 0 ; i < 10 ; i++) { for ( int j = 0 ; j < 10 ; j++) { System.out.println(i + ":" + j); if ( 0 == j % 2 ) { breakout = true ; //终止外部循环的标记设置为true break ; } } if (breakout) //判断是否终止外部循环 break ; } } } |
示例 3 : 使用标签结束外部循环
在外部循环的前一行,加上标签
在break的时候使用该标签
即能达到结束外部循环的效果
public class HelloWorld { public static void main(String[] args) { //打印单数 outloop: //outloop这个标示是可以自定义的比如outloop1,ol2,out5 for ( int i = 0 ; i < 10 ; i++) { for ( int j = 0 ; j < 10 ; j++) { System.out.println(i+ ":" +j); if ( 0 ==j% 2 ) break outloop; //如果是双数,结束外部循环 } } } } |
转载地址:https://dechen.blog.csdn.net/article/details/105429901 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!