Introduction to flow control and control statements

"Process control": It means that we want to arrange how our program runs under some normal conditions or under some abnormal conditions. This requires some judgment, and based on this judgment, do some "other things", which is "process control
."

Judgment statement _if statement:

if statement_format 1:
if (Boolean expression) {
// If the result of "Boolean expression" is true, then execute this
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if (age >= 23 && age <= 25) {
System.out.println("年龄合适,那么你的身高呢?");
double h = sc.nextDouble();
if (h > 1.6) {
System.out.println("身高也很合适,那么你的月收入呢?");
int s = sc.nextInt();
if (s > 20000) {
System.out.println("年龄、身高、月薪都很合适...我们约会吧!!");
} } }
System.out.println("程序结束!");
}

if statement _ format two:
if (Boolean expression) {
// If the Boolean expression returns true then execute here
} else {
// If the Boolean expression returns false then execute here
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你考试的分数:");
int s = sc.nextInt();
if (s >= 60) {
System.out.println("及格!");
}else{
System.out.println("不及格!");
}
System.out.println("程序结束!");
}

if statement_format 3:
if (Boolean expression 1) {
expression 1 is true: execute here
} else if (Boolean expression 2) {// expression 1 is false: execute here
expression 2 is true: execute here
} else if (Boolean expression 3) {// Expression 2 is false: execute here
Expression 3 is true: execute here
} else {// Expression 3 is fals e: execute here
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的分数:");
int s = sc.nextInt();
if (s >= 0 && s < 60) {
System.out.println("凉凉!!");
} else if (s >= 60 && s < 80) {
System.out.println("及格!");
} else if (s >= 80 && s < 90) {
System.out.println("良好!");
} else if (s >= 90 && s <= 100) {
System.out.println("优秀!");
}else{
System.out.println("你的分数不正确!");
} } 

Select statement _switch statement:

1). The switch statement has the same function as the if statement, which is used for "judgment". But when judging the equivalence of multiple values, it is clearer to use the switch statement structure.
2). For example: the user enters the value of 1-7, the program must judge, and print: Monday, Tuesday ...

public class Demo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入1--7的星期值:");
int w = sc.nextInt();
//使用switch语句--switch(byte,short,int,char,String,枚举 变量/表达式)
switch (w){
case 1://case 后面必须是"常量",不能是”变量"/"表达式" System.out.println("星期一");
break;//表示:跳出switch语句
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://相当于:else{...}
System.out.println("你的输入有误!");
break; } } }

Note:
The values ​​of multiple cases cannot be the same;
the break in each case means that it is not necessary to jump out of the switch statement. But during execution, if the code in the case is executed, it will continue to execute the code in the subsequent case unconditionally until it encounters a break; or the switch statement ends.
**

Loop statement _for statement:

**
"Cycle": Do one thing repeatedly.
For example: the program wants to print HelloWorld 10 times

public static void main(String[] args) {
//打印10次HelloWorld
for(int i = 0 ; i < 10 ; i++){
System.out.println("HelloWorld!");
} }

Loop statement _while loop statement:

Format: while (Boolean expression) {
// If "Boolean expression" is true, then execute the loop body
// after the execution is completed, it will go up and continue to execute "Boolean expression"
}

public class Demo08 {
public static void main(String[] args) {
//1.打印10次HelloWorld
/*int i = 0;
while (i < 10) {
System.out.println("HelloWorld");
i++;
}*/

Loop statement _do_while loop statement:

Format: do {
// loop body
} while (Boolean expression);
Example:

public class Demo {
public static void main(String[] args) {
//1.打印10次HelloWorld
/* int i = 0;
do{
System.out.println("HelloWorld");
i++;
}while(i < 10);*/

The scope of inserting small knowledge variables:

The scope of the variable is within the curly braces that define it. If the curly braces are out, the variable cannot be accessed.

public static void main(String[] args){
int a = 10;
if(...){
int b = 20;
System.out.println(a);//OK的
System.out.println(b);//OK的if(...){
int c = 30;
System.out.println(c);//OK的
System.out.println(a);//OK的
System.out.println(b);//OK的
}
System.out.println(c);//错误
}
System.out.println(a);//OK的
System.out.println(b);//错误
}

Loop statement _ endless loop:

public static void main(String[] args) {
/*for(;; ) {
System.out.println("呵呵");
}*/
/*while (true) {
System.out.println("嘻嘻");
}
*/
do{
System.out.println("嘻嘻");
}while(true);
}

Loop statement _ nested loop:

1). Nested loop: refers to a loop that contains another loop;
for example:

public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5 ; j++) {
System.out.println("HelloWorld");
}
}
}

Loop statement _continue and break statement:

1) .continue: End this cycle and continue to the next cycle;

//1.打印1--100所有的偶数
for(int i= 1 ;i <= 100 ;i++){
if(i % 2 == 0){
System.out.println(i);
} }

for(int i = 1 ;i <= 100; i++){
if(i % 2 != 0){
continue;//结束本次循环,继续下一次循环
}
System.out.println(i);
}

2) .break: jump out of the loop statement (end the loop);

//1.从1开始,找出10个能同时被2,3,5整除的数
int i = 1;//存储数
int c = 0;//存储找到的个数
while(true){
if(i % 2 == 0 && i % 3 == 0 && i % 5 == 0){
c++;
System.out.println(i);
}
i++;
if(c == 10){
break;//跳出while循环
} }
Published 8 original articles · Likes0 · Visits 38

Guess you like

Origin blog.csdn.net/FearSun/article/details/105381750