1.if结构

if单选结构

语法:

if(布尔表达式){
  //如果布尔表达式的值为true
}else{
  //如果布尔表达式的值为false
}

案列:考试分数大于60分是及格,小于60分就是不及格

  Scanner in=new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        double score=in.nextDouble();
        if (score>=60){
            System.out.println("恭喜你!及格了");
        }else{
            System.out.println("非常遗憾,没有及格。");
        }
        in.close();

if多选结构

if(布尔表达式){
  //如果布尔表达式1的值为true
}else if{
  //如果布尔表达式2的值为true
}else if{
  //如果布尔表达式3的值为true
}else {
  //如果没有布尔表达式的值为true

案例:不同成绩有不同的等级。

        Scanner in=new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        double score=in.nextDouble();
        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&&score>=0){
            System.out.println("不及格!");
        }else {
            System.out.println("成绩不合法。");
        }
        in.close();

2.switch多选结构

switch 语句中的变量类型可以是:

1.byte short int char

2从Java SE 7 开始,switch 支持字符串String类型

3.case标签必须为字符串常量或者字面量

语法:

switch(expression){
	case value :
	//语句
	break;//可选
	case value:
	break;
	//任意数量的case语句
	default://kexuan
	//语句
}

案例:

 char grade ='C';
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;//case具有穿透效果,所以一般要加break
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("不及格");
                break;
            default:
                System.out.println("未知等级");
        }

3.循环结构

while是最基本的循环

语法:

while(布尔表达式){
//循环内容
}

1.只要布尔表达式为true,循环就会一直执行下去

2.大多数情况是会让循环停止下来,我们需要一个让表达式失效的方式来结束循环

3.少部分情况需要循环一直执行,比如服务器的请求响应监听等等

4.循环条件一直为true就会造成无限循环【死循环】,正常业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死奔溃

案例:1+2+3+....+100=?

        int i=0;
        int sum=0;
		while (i<=100){
             sum+=i;
             i++;
        }
        System.out.println(sum);

4.do-while循环

1.对于while语句而言,如果不满足条件,则不能进入循环,有时候我们需要即使不满足条件也至少要执行一次

2.do-while循环和while循环相似,不同的是,do-while循环至少会执行一次

语法:

do{
//代码语句
}while(布尔表达式);

3.do-while和while区别:

​ while先判断后执行,do-while是先执行后判断

​ do-while总是会至少执行一次

区别:

 int a=0;
        while(a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("==============");
        do{
            System.out.println(a);
            a++;
        }while(a<0);

5.for循环

特点:

1.for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构

2.for循环执行的次数是在执行前就确定的

语法:

for(初始化;布尔表达式;更新){
//代码语句
}

案例1:计算0到100之间的奇数和偶数和

int oddSum=0;
        int evenSum=0;
        for (int i=1;i<=100;i++){
            if (i%2==0){//判断是否为偶数
                evenSum+=i;
            }else{
                oddSum+=i;
            }
        }
        System.out.println("奇数和为:"+oddSum+","+"偶数和为:"+evenSum);

案列2:用while或则for循环输出1—1000之间能被5整除的数,并且每行输出3个

for (int i=0;i<1000;i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(3*5)==0){
                System.out.println("\n");
            }
        }
        //print 输出不会换行
        //println 输出会自动换

案例3:打印九九乘法表

 //1.先打印第一列
        //2.把i固定,再用一个j循环包起来
        //3.去掉重复项,j<=i
        //4.调整样式
        for (int i=1;i<=9;i++){
            for (int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }

增强for循环

 int [] numbers={10,20,30,40,50,};//定义一个数组
        for(int x:numbers){
            System.out.println(x);
        }//遍历数组

break and continue

break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。

int i=3;
        while(i<100){
            i++;
            System.out.println(i);
            if (i==3){
                break;
            }
        }

continue语句在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

int i=0;
        while(i<100){
            i++;
            if(i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i+"\t");
        }

总结案例:打印一个五行的三角形

//五行的三角形
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >=i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <=i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }