目录
前言:
前言:
控制流程(也称为流程控制)是 计算机 运算领域的用语,意指在 程序 运行时,个别的指令(或是陈述、 子程序 )运行或 求值 的顺序。不论是在 声明式编程语言或是函数编程语言中都有类似的概念。 在声明式的编程语言中, 流程控制指令 是指会改变程序运行顺序的指令,可能是运行不同位置的指令,或是在二段(或多段)程序中选择一个运行。 继续运行位在不同位置的一段指令(无条件分支指令)。 若特定条件成立时,运行一段指令,例如C语言的switch指令,是一种有条件分支指令。 运行一段指令若干次,直到特定条件成立为止,例如C语言的for指令,仍然可视为一种有条件分支指令。
一、程序流程控制:分支结构
概述:
1.流程控制语句是用来控制程序中个语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块。
2.流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:
顺序结构:程序从上到下逐行执行,中间没有任何判断和跳转。
分支结构:根据条件选择性地执行某段代码。
有if-else和switch-case两种分支语句。
循环结构:根据循环条件,重复性的执行某段代码。
有while、do...while、for三种循环语句。
注:JDK1.5提供了foreach循环,方便的遍历集合、数组元素。
(1)if-else结构
1.说明:
1.else结构是可选的。
2.针对条件表达式:
如果多个表达式之间是互斥关系,那个判断和执行语句声明在上面都无所谓
如果多个表达式之间是有交集的关系,需要根据实际情况,考虑清楚应该将那个结构声明在上面。
如果多个表达式之间是有包含的关系,通常情况下,需要将范围小的声明在范围大的上面。否则,范围小的就没有机会执行了。
3.if-else是可以相互嵌套的。
注意:
1.if 语句至少要有一个else语句,必须在else if语句之后;
2.一旦一个else if语句为true,其他语句不再执行;
3.语句有若干else if语句,必须在else语句之前;
if语句的三种格式:
格式1:
if (条件表达式){
执行代码块
}
格式2:二选一
if (条件表达式){ 执行代码块1 }else { 执行代码块2 }
格式3:多选一
if (条件表达式){ 执行代码块1 }else if{ 执行代码块2 } ...... else{ 执行代码块n }
代码演示:
public class If{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
scanner.nextLine();
String s = scanner.nextLine();
//equals:判断字符串是否相等
if(s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
例题:成绩判断
public class If{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score==100){
System.out.println("恭喜满分");
}else if(score<100&&score>=90) {
System.out.println("A级");
}else if(score<90&&score>=80) {
System.out.println("B级");
}else if(score<80&&score>=70) {
System.out.println("C级");
}else if(score<70&&score>=60) {
System.out.println("D级");
}else if(score<60) {
System.out.println("不及格");
}else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
Scanner类
1.概念:
Scanner类是引用数据类型的一种,我们可以使用该类来完成用户键盘录入,获取到录入的数据。
2.具体实现步骤:
1.导包:import java.util.Scanner;
2.Scanner的实例化:Scanner scan = new Scanner(System.in) ;
3.调用Scanner类的相关方法,来获取指定类型的变量。
注意:需要根据相应的方法,来输入指定类型的值。如果输入的数据类型与要求的类型不匹配时,会报异常,导致程序终止。
import java.util.Scanner;//导包
public class Test {
//Scanner类
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);//Scanner的实例化
int num = scan.nextInt();//Scanner类的相关方法
System.out.println(num);
}
}
(2)Switch-case结构
1.说明:
1.根据Switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应的case结构中,调用其执行语句。当调用完执行语句后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字或此switch-case结构末尾结束为止。
2.break,可以使用在switch-csae结构中,表示一旦执行到此关键字,就跳出switch-case结构。
3.switch结构中的表达式,只能是如下六中数据类型之一:byte、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)。
4.break关键字在switch-case结构中是可选的。即可以加break,也可以不加break。
5.case只能声明常量,不能声明范围。
6.default:相当于if-else结构中的else;
default结构是可选的;
2.语法格式:
switch (表达式){ case 常量1: 执行语句1; //break case 常量2: 执行语句2; //break .......... case 常量n: 执行语句n; //break default: 执行语句; }
3.代码演示:
public class Switch {
public static void main(String[] args) {
String name = "狂神";
switch (name){
//case 穿透 //匹配一个具体的值
case "张三":
System.out.println("张三");
break;//可选
case "李四":
System.out.println("李四");
break;//可选
case "狂神":
System.out.println("狂神");
break;//可选
default:
System.out.println("未知人物");
}
}
}
例题:
从键盘分别输入年、月、日,判断这一天是当年的第几天 注:判断一年是否是闰年的标准 1.可以被4整除,但不可被100整除;或可以被400整除
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份");
int year = scan.nextInt();
System.out.println("请输入月份");
int month = scan.nextInt();
System.out.println("请输入日份");
int day = scan.nextInt();
//定义一个变量保存总天数
int sumdays= 0;
switch (month){
case 12:
sumdays +=30;
case 11:
sumdays +=31;
case 10:
sumdays +=30;
case 9:
sumdays +=31;
case 8:
sumdays +=31;
case 7:
sumdays +=30;
case 6:
sumdays +=31;
case 5:
sumdays +=30;
case 4:
sumdays +=31;
case 3:
//判断是否是闰年
if (year %4 ==0 && year %100 !=0 || year %400 == 0) {
sumdays +=29;
}else {
sumdays +=28;
}
case 2:
sumdays +=30;
case 1:
sumdays +=day;
}
System.out.println(year+"年"+month+"月"+day+"日是当年的第"+sumdays+"天");
}
总结:
1.凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
2.当我们写分支结构时,当发现既可以使用switch-case(同时,case中的表达式取值情况不太多),又可以使用if-else时,优先使用switch-case。
原因:switch-case执行效率比较高。
二、程序流程控制:循环结构
1.概念:
在某些条件满足的情况下,反复执行特定代码的功能。
2.循环语句分类:
for语句
while语句
do-while语句
3.循环语句的四个组成部分:
初始化部分(init_statement)
循环条件部分(test_exp)
循环体部分(body_statement)
迭代部分(alter_statement)
(3)for循环结构
1.循环结构的4个要素:
初始化条件
循环条件 ---->是boolean类型
循环体
迭代条件
2.for循环的结构
for( 初始化条件;循环条件 ;迭代条件 ){ 循环体; }
3.代码演示
//输出5个hello word
for (int i = 0; i < 5; i++) {
System.out.println("hello word");
}
4.例题1:
下列代码的输出结果:
int num = 1;
for (System.out.println('a');num <=3 ; System.out.println('c') , num++) {
System.out.println('b');
}
输出结果:abcbcbc
例题2:
遍历100以内的偶数。
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
例题3:
编写程序从1循环到150,并在每行都打印一个值, 另外在每个3的倍数行上打印出”foo“, 在每个5的倍数行上打印出”biz“, 在每个7的倍数行上打印出”baz“。
public static void main(String[] args) {
for (int i = 0; i < 150; i++) {
System.out.print(i+" ");
if (i % 3 == 0) {
System.out.print("foo ");
}
if (i % 5 == 0) {
System.out.print("biz ");
}
if (i % 7 == 0) {
System.out.print("baz ");
}
}
}
(4)while循环结构
1.说明:
1.写while循环一定不要丢了迭代条件。一旦丢了,就可能导致死循环。
2.我们写程序要避免死循环。
3.for循环和while循环是可以相互转换的。
区别:for循环和while循环的初始化条件部分的作用范围不同。
2.while循环结构:
初始化条件 while (循环条件){ 循环体; 迭代条件; }
3.代码演示:
public class Test {
public static void main(String[] args) {
//遍历100以内的所有偶数
int i= 1;
while (i<=100){
if (i %2== 0) {
System.out.println(i);
}
}
}
}
4.例题:
public class Test {
public static void main(String[] args) {
//遍历100以内的所有偶数
int i= 1;
while (i<=100){
if (i %2== 0) {
System.out.println(i);
}
i++;
}
System.out.println(i);
}
}
(5)do-while循环结构
1.说明:
1.do-while循环至少会执行一次循环体;
2.开发中,使用for和while更多一些;较少使用do-while;
2.do-while循环结构:
初始化条件; do{ 循环体; 迭代条件; }while (循环条件);
3.代码演示:
public class Test {
public static void main(String[] args) {
//遍历100以内的偶数,并计算所有偶数的和及偶数的个数
int num =1;
int sum =0;//记录总和
int count =0;//记录个数
do{
if (num % 2 ==0) {
System.out.println(num);
sum +=num;
count++;
}
num++;
}while (num<=100);
System.out.println("总和为:"+sum);
System.out.println("个数为:"+count);
}
}
(6)while(true)结构的使用
(6)while(true)结构的使用
1.说明:
1.最简单的"无限"循环格式:while(true),for( ; ; ),无限循环存在的原因是并不知道循环多少次,需要根据循环体内部某些条件,来控制循环的结束。
2.结束循环有几种方式?
方式一:循环条件部分返回false;
方式二:在循环体中,执行break。
2.例题:
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
public class Test {
public static void main(String[] args) {
//从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
Scanner scan = new Scanner(System.in);
int positive =0;//记录整正数的个数;
int negative = 0;//记录负数的个数;
while (true){
int number = scan.nextInt();
//判断number的正负情况。
if (number >0) {
positive++;
} else if (number<0) {
negative++;
}else {
//执行break,则跳出循环。
break;
}
}
}
}
(7)嵌套循环的使用
1.概念:我们将一个循环结构A声名在另一个循环结构B的循环体中,就构成了嵌套循环。
外层循环:循环结构B
内层循环:循环结构A
2.说明:
1.内层循环结构遍历一遍,只相当于外层循环循环体执行一次;
2.假设外层循环需要执行m次,内层循环需要执行n次。
此时内层循环的循环体执行了m×n次。
3.技巧:
外层循环控制行数,内层循环控制列数
3.代码演示:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 6; j++) {
System.out.println('*');
}
System.out.println();
}
}
}
4.
例题1:打印九九乘法表
public class Test {
//打印九九乘法表
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < i; j++) {
System.out.println(i + "*" + j + "=" + (i * j));
}
System.out.println();
}
}
}
例题2:打印1000以内的所有质数。
//1000以内的所有质数.
//质数:素数,只能被1和它本身整除的自然数 --》从2开始,到这个数-1结束为止,都不能被这个数本身整除。
//最小的质数:2
public static void main(String[] args) {
boolean isFlag = true;//标识i是否被j除尽,一旦除尽,修改其值
int count=0;//记录质数的个数
//获取当前时间距离1970-01-01 00:00:00 的毫秒数
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {//遍历1000以内的自然数
for (int j = 2; j < Math.sqrt(i); j++) {//j:被i去除
if (i % j== 0){//i被j除尽
isFlag = false;
break;//只对本身非质数的自然数是有效的。
}
}
if (isFlag==true){
System.out.println(i);
count++;
}
//重置isFlag
isFlag = true;
}
//获取当前时间距离1970-01-01 00:00:00 的毫秒数
long end = System.currentTimeMillis();
System.out.println("质数的个数为:"+count);
System.out.println("所花费的时间为:"+(end-start)/1000 +"秒");//1000毫秒=1秒
}
(8)特殊关键字的使用:break、continue
1.说明:
break
使用范围:switch-case、循环结构中
使用的作用:结束当前循环
关键字后面不能有执行语句
默认跳过包裹此关键字最近的一层循环
continue
使用范围:循环结构中
使用的作用:结束当次循环
关键字后面不能有执行语句
2.代码演示:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if(i % 4 ==0){
//break;//结果为123
continue;//结果为1235679
}
System.out.println(i);
}
}
}
3.带标签的break、continue使用
public class Test {
//
public static void main(String[] args) {
label:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
if (i % 4 == 0) {
//break;//默认跳过包裹此关键字最近的一层循环
//continue;
// break label;//结束指定标识的一层循环结构
continue;//结束指定标识的一层循环结构当次循环
}
System.out.println(i);
}
}
}
}