1. 程序逻辑控制
程序的执行一共有三类形式:顺序结构、分支结构、循环结构。
顺序结构指的是所有的代码按照定义顺序一行行执行,如主方法中所编写的代码一样。
1.1 分支结构
分支结构指的是进行逻辑判断,当满足于某些条件的时候才会执行某些语句。分支结构有两类:if 分支结构和switch分支结构。
- if分支节后基本语法如下:
- if (布尔表达式){
条件满足时的执行代码;
} - if (布尔表达式){
条件满足时的执行代码;
}else{
条件不满足时的执行代码;
} - if (布尔表达式){
条件满足时的执行代码;
}else if (布尔表达式){
条件满足时的执行代码;
}else if
···
else{
所有条件都不满足时的执行代码;`
}
- if (布尔表达式){
范例:使用 if 语句
代码
public class Test{
public static void main(String args[]){
int age = 18;
if ( age >= 18 ){
System.out.println("成年!") ; // 输出结果:成年!
}
}
}
范例:使用 if - else语句
代码
public class Test{
public static void main(String args[]){
int age = 16;
if ( age >= 18 ){
System.out.println("成年!") ;
}else {
System.out.println("未成年!") ; // 输出结果:未成年!
}
}
}
范例:使用 if - else - if语句
代码
public class Test{
public static void main(String args[]){
int age = 26;
if ( age <= 18 ){
System.out.println("未成年") ;
}else if( age > 18 && age < 40) {
System.out.println("青年") ; // 输出结果:青年
}else {
System.out.println("中老年") ;
}
}
}
使用if - else 可以实现条件的判断,但是如果想要进行多数值内容的判断,那么就可以使用switch完成,但是要注意,switch随着JDK版本的变化支持的数据类型也在变化:
①最初的数据类型支持:int、char;
②从JDK 1.4开始支持了枚举(enu);
③从JDK1.7开始支持了String。
- switch分支节后基本语法如下:
- switch( 数字 | 枚举 | 字符 | 字符串 ){
case 内容1 : {
内容满足时执行的语句;
[break;]
}
case 内容2 : {
内容满足时执行的语句;
[break;]
}
case 内容n : {
内容满足时执行的语句;
[break;]
}
[default : {
都不满足时执行的语句;
[break;]
}]
}
- switch( 数字 | 枚举 | 字符 | 字符串 ){
switch这种开关语句有一个重要特点:
如果在编写case时未加break,则会在满足的case语句之后,一直执行到遇见break或全部结束。
范例:观察switch
代码
public class Test{
public static void main(String args[]){
int ch = 1;
switch (ch) {
default : {
System.out.println("没有条件被满足") ;
}
case 1 : {
System.out.println("内容为1") ;
}
case 2 : {
System.out.println("内容为2") ;
}
}
}
} /* 输出结果:
内容为1
内容为2
*/
代码
public class Test{
public static void main(String args[]){
int ch = 1;
switch (ch) {
default : {
System.out.println("没有条件被满足") ;
break;
}
case 1 : {
System.out.println("内容为1") ;
break;
}
case 2 : {
System.out.println("内容为2") ;
break;
}
}
}
} // 输出结果:内容为1
String:
代码
public class Test{
public static void main(String args[]){
String info = "hello";
switch (info) {
case "hello" : {
System.out.println("你好") ;
break;
}
case "world" : {
System.out.println("世界") ;
break;
}
default : {
System.out.println("没有条件被满足") ;
break;
}
}
}
} // 输出结果:你好
1.2 循环结构
循环结构指的是某几行代码被一直重复执行的操作形式。循环一般会有两类循环:while循环、for循环。
- while循环
- 循环的初始化内容
while (循环的结束条件判断){
循环语句;
修改循环结束条件;
} - 循环的初始化内容
do{
循环语句;
修改循环结束条件;
}while (循环的结束条件判断);
- 循环的初始化内容
范例:使用while实现1~100的累加
代码
public class Test{
public static void main(String args[]){
int i = 1;
int sum = 0;
while ( i <= 100 ){ // 表示是一个循环的结束条件判断
sum = sum + i;
i++; // 循环条件变更
}
System.out.println("sum = " + sum) ;
}
} //输出结果: sum = 5050
使用while循环的最大特点:
如果判断条件不满足就一次都不执行,而使用do - while的特点是即使判断条件不满足也会执行一次。
范例:使用do - while实现1~100的累加
代码
public class Test{
public static void main(String args[]){
int i = 101; // 判断条件不满足
int sum = 0;
do{
sum = sum + i;
i++; // 循环条件变更
} while ( i <= 100 ); // 表示是一个循环的结束条件判断
System.out.println("sum = " + sum) ;
}
}
// 输出结果:sum = 101
以后开发之中do - while 基本不使用。
- for循环
- for(循环初始化条件 ; 循环结束判断 ; 循环修改条件) {
循环体代码;
}
- for(循环初始化条件 ; 循环结束判断 ; 循环修改条件) {
范例:使用for循环实现1~100的累加
代码
public class Test{
public static void main(String args[]){
int sum = 0;
// 1. 循环初始化:int i = 0;
// 2. 判断循环条件: i <= 100;
// 4. 循环条件变更: i++
// 5. 判断循环条件,在2,3,4,5之间循环
for ( int i = 1 ; i <= 100 ; i++) {
sum = sum + i; // 3. 循环体操作
}
System.out.println("sum = " + sum) ;
}
} // 输出结果:sum = 5050
千万记住,对于以下的for循环,别写!
代码
int sum = 0;
int i = 1:
for ( ; i <= 100 ;) {
sum = sum + i;
i++;
}
循环使用原则:
- 对于不知道循环次数,但是知道循环结束条件的,使用while循环;
- 如果已经明确知道循环次数,则使用for循环。
1.3 循环控制
在循环处理的时候有两个关键字:continue、break。一般这样的语句都会结合 if 判断一起使用。
1.continue:执行到词此语句时,将跳过循环体的剩余部分,而返回到循环的判断出进行处理。
范例:continue
代码
public class Test{
public static void main(String args[]){
for ( int i = 0 ; i < 6 ; i++) {
if (i ==3){
continue; // 执行此语句后循环体的后面代码不执行,有时是空出一次循环
}
System.out.println("i = " + i ) ;
}
}
} /*输出结果:
i = 0
i = 1
i = 2
i = 4
i = 5
*/
2.break:退出整个循环。
范例:break
代码
public class Test{
public static void main(String args[]){
for ( int i = 0 ; i < 6 ; i++) {
if (i ==3){
break; // 执行此语句后循环结束
}
System.out.println("i = " + i ) ;
}
}
} /* 输出结果:
i = 0
i = 1
i = 2
*/
其他的语言有一种goto的功能,这个功能不会出现在Java里,也没有这样的关键字,不过可以利用continue实现与之一样的功能。
范例:
代码
public class Test{
public static void main(String args[]){
mp: for (int x = 0 ; x < 5 ; x++){
for ( int y = 0 ; y < 3 ; y++) {
if (x > 2){
continue mp;
}
System.out.println("x = " + x + ", y = " + y ) ;
}
}
}
} //输出结果:
// x = 0, y = 0
// x = 0, y = 1
// x = 0, y = 2
// x = 1, y = 0
// x = 1, y = 1
// x = 1, y = 2
// x = 2, y = 0
// x = 2, y = 1
// x = 2, y = 2
正常情况很少这样写,也很少使用break、continue语句。
1.4 循环嵌套(理解)
循环语句本身是可以进行嵌套使用的,但从现在的开发来讲,这种嵌套的操作已经少了。
范例:打印乘法口诀表
代码
public class Test{
public static void main(String args[]){
for (int x = 1 ; x <=9 ; x++){
for ( int y = 1 ; y <= x ; y++) {
System.out.print( x + "*" + y + "=" + (x*y) + "\t" ) ;
}
System.out.println( ) ;
}
}
} /* 输出结果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */
范例:打印*三角形
代码
public class Test{
public static void main(String args[]){
int line = 5 ; // 打印5行
for (int x = 0 ; x < line ; x++){ // 控制行
for ( int y = 0 ; y < line - x ; y++) { // 空格
System.out.print( " ") ;
}
for ( int y = 0 ; y < x ; y++) { // *
System.out.print( "* ") ;
}
System.out.println() ;
}
}
} /* 输出结果:
*
* *
* * *
* * * *
*/