Java: flow control statements

Here Insert Picture Description

Flow control statements

During a program execution, the order of execution of the statement of the program is the result of a direct impact. So we want to achieve our functions to be performed by the execution order control statements.

Sequence Structure

Is the most simple basic program flow control statements, no specific grammatical structure, in the order code, followed by the implementation of the program in most of the code is executed so.
Overall: EDITORIAL first execution after written on the back of execution

System.out.println("第一步执行");
System.out.println("第二步执行");
System.out.println("第三步执行");
/*
运行结果:
第一步执行
第二步执行
第三步执行
*/
//在顺序结构语句中,会根据代码的顺序来运行。
Select structure

Selection structure is also known as a branched structure. Select specific structure of syntax rules, a specific code to execute the logic operations are determined, there are two logical operation result (true and to false), it generates a selection, executing different code according to different options.

The if statement

Several formats:

//1
if(比较表达式或boolean类型的值)
{
	语句体;
}
/*
执行流程:
先计算比较表达式的值,看其返回值是true还是false。
如果是true,就执行语句体;
如果是false,就不执行语句体;
*/
//示例
if(10>2)
{
	System.out.println("执行");
}
/*
首先会对if里的表达式进行判断,10>2为ture,所以执行{}中的代码。
运行结果:
执行
*/
//2
if(比较表达式) 
{
	语句体1;
}
else 
{
	语句体2;
}
/*
执行流程:
首先计算比较表达式的值,看其返回值是true还是false。
如果是true,就执行语句体1;
如果是false,就执行语句体2;
*/
//示例:
if(10==2)
{
	System.out.println("执行if{}");
}
else
{
	System.out.println("执行else{}");
}
/*
首先对if括号中的表达式进行判断,10==2为false,所以跳过if{}中的语句,执行else{}中的代码。
运行结果:
执行else{}
*/
//3
if(比较表达式1)
{
	语句体1;
}
else if(比较表达式2) 
{
	语句体2;
}
else if(比较表达式3) 
{
	语句体3;
}
...
else 
{
	语句体n+1;
}
/*
执行流程:
首先计算比较表达式1看其返回值是true还是false,
如果是true,就执行语句体1,if语句结束。
如果是false,接着计算比较表达式2看其返回值是true还是false,
如果是true,就执行语句体2,else if语句结束。
如果是false,接着计算比较表达式3看其返回值是true还是false,
	...
	
如果都是false,就执行else大括号中的语句体n+1。
*/
//示例
if(10==2)
{
	System.out.println("执行if{}");
}
else if(10>2)
{
	System.out.ptintln("执行else if{1}");
}
else if(10<2)
{
	System.out.println("执行else if{2}");
}
else
{
	System.out.println("执行else{}");
}
/*
首先判断if后括号里的表达式,10==2为false,所以跳过if{}中的语句。
再判断else if后括号里的表达式,10>2为true,所以执行else if{}中的语句。
然后跳出选择语句的结构体,不再判断下面的10<2,也不执行下面的else if{}和else{}中的语句
运行结果:
执行else if{1}
*/

Notes:
1. Comparison of expression, whether simple or complex, the result must be a boolean type.
2.if control statement if the statement is a statement the body, {} may be omitted. We recommend caution.

//例题
boolean b = true;
if(b=false)  
	System.out.println("a");//if只控制这一条语句,{}可以省略。
else if(b)
	System.out.println("b");
else if(!b)
	System.out.println("c");
else
	System.out.println("d");
/*
分析:
首先定义一个布尔类型的变量b,值为true
按照顺序先判断if()中的b=false。但这个表达式不是boolean类型的结果,是个赋值语句。
!!所以不执行if{}中的语句,但是把false赋给了b。
然后进行下面的语句,第二个else if()中!b为true,所以输出c,结束整个选择语句结构体。
运行结果:
c
*/

and if conversion issues ternary operator
Note: ternary operator can be realized, can be implemented with an if statement, but not vice versa.
Such as: when the control if the statement is a statement output, can not be converted into ternary operator.
Because the ternary operator is an operator, the operator should have a complete operation result, instead of one output.

if nested

//示例
/*
先判断userName(用户名)是否是"123"
如果是再判断passwor(密码)是否是"321"
如果是,输出"正确"
*/
if(userName=="123")
{
	if(password=="321")
	{
		System.out.println("输入正确");
	}
}
//在使用if嵌套时,无论if控制的语句有多少句,都将其用{}括起来
//这样可以提高代码的可读性
switch statement

format:

switch(表达式)
{
	case1:
		语句体1;
		break;
	case2:
		语句体2;
		break;
	case3:
		语句体3;
		break;
	....
	default
		语句体n+1;
		break;
}
/*
格式解释:
1.switch表示这是switch语句
  表达式的取值:byte,short,int,char
			  JDK5以后可以是枚举(什么是枚举,以后再讲) 
			  JDK7以后可以是String
2.case后跟的是要和表达式进行比较的值
3.语句体部分可以是一条或多条语句
4.break表示中断,结束的意思,结束switch语句
5.default语句表示所有情况都不匹配时,就执行该处的内容,和if语句中的else相似
*/
/*
执行流程:
依次判断表达式的值与哪一个case后面的值相同。
如果有相同值,执行该case后的语句,最后break;跳出选择语句结构体。
如果没有相同值,执行default中的语句,然后break;跳出选择语句结构体。
*/

Interview questions:
1.byte switch can be used as an expression of it? Can
2.long switch can be used as an expression of it? Not
3.String switch can be used as an expression of it? After JDK1.7 can
Note:
behind 1.case can only be constant, not a variable, but more behind the case can not appear the same value.
2.default can be omitted, but not recommended, if the latter case there is no match, he can give tips on this case
3.break can be omitted, but the case will appear penetration phenomenon can sometimes take advantage of this situation
4.default can placed anywhere in the switch structure, but it is recommended on the final
5.switch encounters a break or to the end of execution is over

//注意以下两个程序的区别
//1
int x = 2;
int y = 3;
switch(x)
{
	default:
		y++;
		break;
	case 3:
		y++;
	case 4:
		y++;
}
System.out.println("y="+y);
/*
分析:
没有与x相匹配的case值,此时虽然在switch的末尾,但是还有default没有执行
执行default,y=4,break结束结构体
运行结果:
y=4
*/
//2
int x = 2;
int y = 3;
switch(x){
	default:
		y++;
		
	case 3:
		y++;
	case 4:
		y++;
}
System.out.println("y="+y); 
/*
分析:
没有与x相匹配的case值,执行default,y=4
注意:default中无break,此时并不在switch结构的末尾,要继续往下执行
但不再判断是否与case后面的值相匹配,发生case穿透
执行两次y++,最后y=6
运行结果:
y=6
*/

case of application penetration phenomenon

//根据月份,输出对应的季节
int month=6;
switch(month)
{
	case 3:
	case 4:
	case 5:
		System.out.println("春季");
		break;
	case 6:
	case 7:
	case 8:
		System.out.println("夏季");
		break;
	case 9:
	case 10:
	case 11:
		System.out.println("秋季");
		break;
	case 12:
	case 1:
	case 2:
		System.out.println("冬季");
		break;
	default
		System.out.println("month的值非法");
		break;
}
/*
如果month的值为3,因为case穿透现象,会直接执行case 5:后面的输出语句,然后break;退出循环体
这样做可以简化代码
*/
Loop structure

When the loop may be a case where the loop condition repeatedly execute a section of code, the code is repeated is referred to as loop statement, when this loop is repeatedly performed, it is necessary at the right time cycle is determined to modify the conditions false, thus ending the cycle, otherwise the loop will always execute it, forming an endless loop
for loop
format:

//			1			   2			3	
for(初始化表达式语句;判断条件语句;控制条件语句)
{//		4
	循环体语句;
}
/*
执行流程:
1.执行初始化表达式语句
2.执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
3.执行循环体语句;
4.执行控制条件语句
5.回到第二步继续。
注意:初始化表达语句只执行一次
执行顺序为1–>2–>4–>3–>2–>3–>4...	直到2中的条件判断为false为止	
*/
//示例:输出1~10之间的数字之和
int sum=0;//存储1~10之间的和
for(int i=1;i<=10;i++)
{
	sum=sum+i;
}
System.out.println(sum);
/*
执行流程:
1.先定义一个变量i,并将其初始化,初始化值为1
2.判断i<=10,结果为true
3.执行循环体中的语句,将sum+i赋值给sum
4.执行i++,i=2
5.再继续判断i<=10
……
直到i<=10为false
运行结果:
55
*/

Count idea for loop

/*
统计水仙花数有多少个
水仙花数:个位数字的三次方+十位数字的三次方+百位数字的三次方=该数本身
*/
/*
问题分析:
水仙花数都是三位数,出现水仙花数的范围是100~999
范围过大,我们需要遍历这个范围,就要用到循环
初始化表达语句:int i=100
判断条件语句:i<=999
控制条件语句:i++
在循环体中,我们需要判断一个数各位数字的立方和是否等于该数本身
问题在于如何求出一个三位数的个位数字、十位数字和百位数字
再判断出一个数是水仙花数的时候,我们还要统计水仙花数的数量
*/
int count=0;
for(int i=100;i<=999;i++)
{
	int g=i%10;		//个位数字
	int s=i/10%10;	//十位数字
	int b=i/100%10;	//百位数字
	if(g*g*g+s*s*s+b*b*b==i)
		count++;
}
System.out.println(count);
/*
在这个问题中,我们定义了一个变量count来统计水仙花数的个数
只要该数字满足条件,就让count自增
在统计的问题中,我们可以定义这样一个记录个数的变量,一旦符合条件,使其自增1
对于取出各位上的数字,可以用上述方法,对于四位数、五位数等也同样适用
*/

nested for loop

/*
请输出一个4行5列的星星(*)图案(每次只能输出一个*)。
如图:
	*****
	*****
	*****
	*****
问题分析,如果要输出5颗星要循环5次
现在要在输出4次5颗星。在5次循环的基础上再循环4次
*/
for(int i=0;i<4;i++)
{
	for(int j=0;j<5;j++)
	{
		System.out.print(*);//注意print(不换行)和println的区别
	}
	System.out.println();//每输出5个*进行换行
}
//外循环控制行数,内循环控制列数
while loop

format:

初始化条件语句;
while(判断条件语句)
 {
	循环体语句;
	控制条件语句;
}
/*
执行流程:
1.执行初始化条件语句;
2.执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
3.执行循环体语句;
4.执行控制条件语句
5.回到第二步继续。
*/
//示例:输出1~10之间的数字之和
int sum=0;
int i=1;	//要有初始化条件语句
while(i<=10)
{
	sum=sum+i;
	i++;	//如果没有控制条件语句,会陷入死循环
}
System.out.println(sum);
/*
运行结果:
55
*/
do ... while loop

format:

初始化条件语句;
do {
	循环体语句;
	控制条件语句;
}while(判断条件语句);
/*
执行流程:
1.执行初始化条件语句;
2.执行循环体语句;
3.执行控制条件语句;
4.执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
5.回到b继续。
注意:无论判断条件是否成立,循环都会执行1次,即先执行再判断
*/
//示例:输出1~10之间的数字之和
int sum=0;
int i=1;
do{
	sum=sum+i;
	i++
}while(i<=10)
/*
运行结果:
55
*/

Note: Be sure to pay attention to the variable control of a conditional statement will not be lost, otherwise it will cause an infinite loop!
Distinguish three cycles:
1.do ... the while loop the loop at least once, and for, while loops must first determine whether the conditions established, and then decide whether to execute the loop body statement
2. If you want to at the end of the cycle, continue to use the control variables (the above-described i) conditions, while loop, a for loop otherwise.
for loop control condition variables can disappear early from memory, improve memory usage efficiency
3. If specifically used for loop cycles, the number of cycles is not clear if the while loop
4 loop for priority, then the while loop, Finally, do ... while loop

Keyboard entry

Advantages:
1. To make the data more in line with the program of development of data
2. Let the program a little more flexible
steps:

1.导包
import java.util.Scanner;		//位置:在class上面
2.创建键盘录入对象
Scanner input=new Scanner(System.in);
3.通过对象获取数据
int x=input.nextInt();		//要接收的数据类型是什么,next后就跟什么类型,如:nextDouble()
//示例:由键盘输入两个数,求两数之和
import java.util.Scanner;
class Demo
{
	public static void main(String[] args)
	{
		Scanner input=new Scanner(System.in);
		System.out.println(“请输入第一个数字”)int num1=input.nextInt();
		System.out.println(“请输入第二个数字”)int num2=input.nextInt();
		int sum=num1+num2;
		System.out.ptintln(sum);
	}
}

Control of jump statements break, continue

For example, we would like to end when a loop is executed to a certain step, and now can not do this thing.
To compensate for this defect, Java provides a break, continue, and return (talk back) to achieve the jump control statements and interrupt.

break (interrupt)

Usage scenarios:
1. Select the structure of the switch statement
2. loop statement
Note: leaving the scene there is no sense to use the
effect:
1. Exit single-cycle
2. Exit multilayer cycle (to achieve this effect, to be used with label statement)
3. exit switch structure

tag1:for( ; ; )
{
	循环体语句
	tag2:for( ; ; )
	{
		循环体语句
		break tag1;//退出外循环
		break tag2;//退出内循环
		break;	//退出当前循环,即内循环
	}
	break;		//退出当前循环,即外循环
}
//退出多层循环不常用
continue (continue)

Usage scenarios: In the loop statement
Note: leaving the scene there is no sense to use the
effect: once out of the loop, the next step

//练习题
for(int x=1;x<10;x++)
{
	if(x%3==0)
	{
		//在此处填写代码
	}
	System.out.println(“Java”);
}
/*
需求:
1.在控制台输出2次“Java”	
2.在控制台输出7次“Java”
3.在控制台输出13次“Java”
在提示处填写代码
1.break;
2.continue;
3.System.out.println(“Java”);	
*/
Published 26 original articles · won praise 1 · views 377

Guess you like

Origin blog.csdn.net/weixin_45919908/article/details/103342526