zoukankan      html  css  js  c++  java
  • 流程控制语句

    一、条件判断语句

         1、if-else语句        

              if (条件表达式)
                      if语句体;
           如果条件表达式为true,执行if语句体, 否则不执行。 当if只有一条语句体时,可以省略{},但是,强烈建议, 不管if有几条语句体,都不要省略{}。
             

    /*
    	if-else
    	if (条件表达式) {
    		if语句体;
    	} else {
    		else语句体;
    	}
    	当条件表达式为true时,执行if语句体。
    	否则,执行else语句体。
    */
    
    public class IfElse {
    	public static void main(String[] args) {
    		int x = -1;
    		if (x > 0) {
    			System.out.println("注册成功");
    		} else {
    			System.out.println("注册失败");
    		}
    	}
    }
    

             

    /*
    	if (条件表达式1) {
    		语句1;
    	} else if(条件表达式2) {
    		语句2;
    	} else if (条件表达式3) {
    		语句3;
    	} ……
    
    	} else if (条件表达式n) {
    		语句n;
    	}
    	从上到下依次判断各个条件表达式,哪一个条件表达式为true,
    	就执行对应的分支。至多只会执行一个分支。
    */
    
    public class IfElseif {
    	public static void main(String[] args) {
    		//当执行多选择分支时,如果多个条件存在包含关系,
    		//则需要将范围最小的放在最前面,范围大的放在后面。
    		/*
    		错误
    		int score = 90;
    		if (score >= 60) {
    			System.out.println("及格");
    		} else if (score >= 80) {
    			System.out.println("良好");
    		} else if (score >= 90) {
    			System.out.println("优秀");
    		}
    		*/
    		int score = 90;
    		if (score >= 90) {
    			System.out.println("优秀");
    		} else if (score >= 80) {
    			System.out.println("良好");
    		} else if (score >= 60) {
    			System.out.println("及格");
    		}
    		
    		/*
    			当各个条件没有关联,但频率不同的时候,
    			我们应该将频率高的放在上面,频率低
    			的放在下面,以减少判断次数。
    
    			40 * 1ms + 30 + 2ms + 20 * 3ms + 10 * 4ms
    			10 * 1ms + 20 * 2ms + 30 * 3ms + 40 * 4ms
    		*/
    		
    
    		int direction = 1;
    		if (direction == 1) {
    			System.out.println("向东");
    		} else if (direction == 2) {
    			System.out.println("向南");
    		} else if (direction == 3) {
    			System.out.println("向西");
    		} else if (direction == 4) {
    			System.out.println("向北");
    		}
    	}
    }
    

      

    /*
    	if (条件表达式1) {
    		语句1;
    	} else if(条件表达式2) {
    		语句2;
    	} else if (条件表达式3) {
    		语句3;
    	} ……
    
    	} else if (条件表达式n) {
    		语句n;
    	} else {
    		else语句;
    	}
    	从上到下依次判断各个条件表达式,哪一个条件表达式为true,
    	就执行对应的分支。如果所有的条件表达式都为false,则执行
    	else分支。
    */
    
    public class IfElseifElse {
    	public static void main(String[] args) {
    		int score = 90;
    		if (score >= 90) {
    			System.out.println("优秀");
    		} else if (score >= 80) {
    			System.out.println("良好");
    		} else if (score >= 60) {
    			System.out.println("及格");
    		} else {
    			System.out.println("不及格");
    		}
    	}
    }
    

       2、switch case 

               

    /*
    	switch (表达式) {
    		case 值1: 分支1; [break;]
    		case 值2: 分支2; [break;]
    		……
    		case 值n: 分支n; [break;]
    		default: 默认分支; [break;]
    	}
    	使用表达式的值,依次与每个case进行比较,
    	哪个case匹配,就执行对应的分支。
    	如果所有的case都不匹配,则执行default分支。
    	default分支是可选的。
    
    	switch表达式的类型可以是byte,short,int,char,以及
    	对应的包装类型,String类型与枚举类型。
    	不可以是boolean,float,double,long。
    	每个case的值要求是常量值(常量表达式)。
    	各个case的值不能重复。
    */
    
    public class Switch {
    	public static void main(String[] args) {
    		int x = 10;
    		switch (x) {
    			case 1: System.out.println("1");break;
    			case 2: System.out.println("2");break;
    			case 3: System.out.println("3");break;
    			default: System.out.println("4");break;
    		}
    
    		switch (x) {
    			case 1: 
    			case 2: System.out.println("1或2");break;
    			case 3: System.out.println("3");break;
    			default: System.out.println("4");break;
    		}
    		//switch对String的支持
    		String s = "abc";
    		switch (s) {
    			case "a": System.out.println("1");break;
    			case "b": System.out.println("2");break;
    			case "abc":System.out.println("3");break;
    		}
    	}
    }
    

      3、while

    /*
    	循环:循环就是重复性执行一个相同(相似)的操作。
    	while (条件表达式) {
    		循环体;
    	}
    	1判断条件表达式是否为true,如果为true,执行循环体。
    	2重复执行步骤1,直到条件表达式为false,退出循环。
    */
    public class While {
    	public static void main(String[] args) {
    		/*System.out.println(1);
    		System.out.println(1);
    		System.out.println(1);
    		System.out.println(1);
    		System.out.println(1);*/
    		int i = 1;
    		while (i <= 5) {
    			//System.out.println(i);
    			//i = i + 1;
    			//i += 1;
    			//i++;
    			System.out.println(i++);
    		}
    	}
    }
    

      4、dowhile

    /*
    	do {
    		循环语句;
    	} while (条件表达式);
    
    	1 执行循环语句,然后判断条件表达式。
    	2 如果条件表达式为true,重复步骤1。
    	如果条件表达式为false,退出循环。
    
    	while与do-while区别:
    	while循环是先判断条件表达式,然后执行循环体,
    	do-while是先执行循环体,然后判断条件表达式。
    	如果条件表达式开始就为false,则while循环一次
    	也不会执行,而do-while循环会执行一次。
    
    	while循环可能一次也不会执行,do-while循环至少
    	会执行一次。
    */
    
    public class DoWhile {
    	public static void main(String[] args) {
    		int i = 1;
    		do {
    			System.out.println(i);
    			i++;
    		} while (i <= 5);
    
    		while (i > 10) {
    			System.out.println("while循环执行");
    		}
    		do {
    			System.out.println("do-while循环执行");
    		} while (i > 10);
    	}
    }
    

      5、for 循环

    /*
    	for (初始化语句; 条件表达式; 循环控制语句) {
    		循环语句;
    	}
    	1 执行初始化语句,该语句只执行一次。(不管for循环执行
    	多少次)
    	2 判断条件表达式是否为true,如果为true,则执行循环
    	语句,再执行循环控制语句。
    	3 重复步骤2,直到条件表达式为false,退出循环。
    
    	for循环的初始化语句,条件表达式与循环控制语句都
    	是可选的,但是;;不能省略。此时,如果条件表达式缺失,
    	则认为条件表达式永远为true。
    	for (; ;) {
    
    	}
    	
    	while循环可以看成是缺少了初始化语句与
    	循环控制语句的for循环。
    	for (;条件表达式;) {
    
    	}
    */
    
    public class For {
    	public static void main(String[] args) {
    		/*
    		int i = 1;
    		while (i <= 5) {
    			System.out.println(i);
    			i++;
    		}
    		*/
    		for (int i = 1; i <= 5; i++) {
    			System.out.println(i);
    		}
    		for (int i = 1; i <=5; i++) {
    			//其他操作
    		}
    		/*等价于while循环。
    		for (;i <= 5;) {
    
    		}
    		*/
    	}
    }
    

      6、循环嵌套

    /*
    	循环嵌套
    	循环中还有循环。
    	当循环嵌套时,外循环每执行一次,内循环都会
    	完整的执行一遍。
    */
    
    public class NestLoop {
    	public static void main(String[] args) {
    		for (int i = 1; i <= 5; i++) {
    			//语句
    			for (int j = 1; j <=3; j++) {
    				System.out.println("i=" + i + ",j=" + j);
    			}
    		}
    	}
    }
    

      7、break

    /*
    	break
    	break可以提前结束循环。(不管循环还剩多少次没有执行)
    
    	break可以在两处使用:
    	1 break可以用在switch-case中。
    	2 break可以用在循环中。
    */
    
    public class Break {
    	public static void main(String[] args) {
    		for (int i = 1; i < 10; i++) {
    			if (i % 5 == 0) {
    				break;
    			}
    			System.out.println(i);
    		}
    	}
    }
    

      8、循环标签

    /*
    	循环标签
    	使用循环标签可以用来标记一个循环(给循环起一个名字)。
    
    
    */
    
    public class LoopLable {
    	public static void main(String[] args) {
    		/*
    			continue outer与break inner的区别:
    			continue outer会结束本次整个外层循环,进行
    			外层循环的下一次循环。如果外层循环还有其他
    			语句,则语句不会得到执行。
    			break inner会跳出整个内层循环,如果外层循环
    			还有其他语句,则语句会得到执行。
    		*/
    
    		outer:
    		for (int i = 1; i <= 3; i++) {
    			inner:
    			for (int j = 1; j <= 3; j++) {
    				if (i == 2 && j == 2) {
    					//跳出外层循环
    					break outer;
    					//continue outer;
    					//continue inner;
    					//break inner;
    				}
    			}
    			/*
    			外层循环的其他循环语句
    			for (int k = 0; k < 2; k++) {
    				//操作
    			}
    			*/
    		}
    	}
    }
    

      9、continue

    /*
    	continue
    	continue用于结束本次循环,对后续的循环没有影响。
    
    	continue只能用在循环当中。
    */
    
    public class Continue {
    	public static void main(String[] args) {
    		for (int i = 1; i < 5; i++) {
    			if (i == 3) {
    				continue;
    			}
    			System.out.println(i);
    		}
    
    		for (int i = 1; i < 5; i++) {
    			if (i != 3) {
    				System.out.println(i);
    			}
    		}
    		/*
    		循环控制语句不能简单的认为是循环体
    		的最后一条语句。
    
    		continue可以结束循环体语句,但是
    		循环控制语句还是可以正常执行。
    
    		
    		for (int i = 1; i <= 5; i++) {
    			System.out.println(i);
    			if (i == 3) {
    				continue;
    			}
    		}
    
    		for (int i = 1; i <= 5; ) {
    			System.out.println(i);
    			if (i == 3) {
    				continue;
    			}
    			i++;
    		}
    		*/
    	}
    }
    

      

  • 相关阅读:
    mac下webstorm自动编译typescript配置
    [转]Golang 中使用 JSON 的小技巧
    Element-UI 框架 el-scrollbar 组件
    npm读取config配置的优先级(yarn同理)
    win, mac, linux 默认系统缓存目录
    yum离线安装rpm包
    常见网络摄像机(摄像头)的端口及RTSP地址
    sed命令在mac和linux下的区别
    canvas笔记备忘
    shell脚本:批量修改文件名(添加/删除文件名中字符)
  • 原文地址:https://www.cnblogs.com/liuwei6/p/6561268.html
Copyright © 2011-2022 走看看