Java基础篇—————从入门到精通(3)

一、流程控制&顺序结构

程序的流程就是代码的执行顺序。分为循环判断两部分。

class Demo 
{
	public static void main(String[] args) 
	{
		int a = 3 ;
		int b = 5 ;
		int sum ;
		sum = a + b;
		System.out.println("sum="+sum);
	}
}

执行时,首先启动JVM,去加载当前Demo的class文件,并且从程序的入口main方法这里开始执行,然后从第一行开始向下执行,直到执行完整段代码,不会跳过任何一行代码。这样的执行流程就是顺序执行结构。

二、判断结构(if)

if是Java里的关键字,JVM就会知道此处需要判断,会根据判断结果处理相应的代码。

1.格式

1.1第一种

if(条件表达式){
       执行语句;
     }

条件表达式的返回值是Boolean类型,如果结果为true,则会执行语句,如果结果为false,则会跳过大括号执行后面的程序。

public class Demo02 {

    public static void main(String[] args) {
      if(2>3){
          System.out.println("hello world");
      }
        System.out.println("到我了");
    }
}

执行结果是到我了

1.2第二种格式

if(条件表达式){
       执行语句1}
    else{
       执行语句2}

这种看条件表达式的返回值,如果是true的话,运行执行语句1,如果是false的话,运行执行语句2.

public class Demo02 {

    public static void main(String[] args) {
      if(2>3){
          System.out.println("hello world");
      }
      else{
          System.out.println("到我了");
      }
       
    }
}

结果可想而知
这种格式的if语句直接可以用三元运算符来做的

1.3第三种格式

if(条件表达式){
     执行语句;
}
else if(条件表达式){
    执行语句;
 }
 ..........
 else{
   执行语句;
}

基本是和以上的两种格式执行方法是一样的
举一个例子,根据用户输入的数字对应的星期来显示星期几。

public class Demo03 {
    public static void main(String[] args) {
        int week =9;

        if(week == 1){
            System.out.println("星期一");
        }
        else if(week ==2){
            System.out.println("星期二");
        }
        else if(week == 3){
            System.out.println("星期三");
        }
        else if(week == 4){
            System.out.println("星期四");
        }
        else if(week == 5){
            System.out.println("星期五");
        }
        else if(week == 6){
            System.out.println("星期六");
        }
        else if(week == 7){
            System.out.println("星期天");
        }
        else {
            System.out.println("请输入1到7的数字");
        }
    }
}

三、多分支结构(switch)

1.switch语句格式

switch(表达式){
    case 取值1:
       执行语句;
       breakcase 取值2:
       执行语句;
       break.......
       default:
       执行语句;
       break}

程序执行时,遇到switch关键字,首先会计算表达式的值,然后根据计算的值和case后面的值做比较,当case后面的值和switch表达式的值相同时,就执行case身后的所有语句,若case身后没有和switch表达式匹配的值,程序就会执行default后面的语句。
演示:

/**
 * 使用switch结构将学生成绩划分为
 * A(90  -- 100),B(80 -- 89),C(70 -- 79),D(60  --  69),E(60分以下)五个等级。
 */
public class Demo04 {
    public static void main(String[] args) {
        int res = 5;
        switch(res/10){
            case 10:
            case 9:
                System.out.println("A");
                break;
            case 8:
                System.out.println("B");
                break;
            case 7:
                System.out.println("C");
                break;
            case 6:
                System.out.println("D");
                break;
            case 5:
            case 4:
            case 3:
            case 2:
            case 1:
            case 0:
                System.out.println("E");
                break;
            default:
                System.out.println("请输入正确的成绩");
                break;
        }
    }
}

2.if和switch的区别

if可以判断数值,也可以判断区间,只要运算结果是Boolean类型,都可以进行判断。
switch用于固定的几个值,进行判断,判断的类型有限,有int,byte,short,char

四、循环结构(while&do while&for)

1.几种类型的比较

  • while:事先不知道循环多少次
  • do while:也是事先不知道循环多少次,但先执行一次,先执行,后判断。
  • for:需要知道循环的次数

2.while循环

while(条件表达式){
    执行语句;
}

当执行时遇到while,JVM首先会运算条件表达式,如果是true,执行大括号里的语句,执行完之后,集训进行条件表达式的判断,如果是true,继续执行大括号里的语句,如果是false,就会跳过大括号里的所有语句,继续执行下去。

2.1 while代码举例

/**
 * 运算1-10的和。
 */
public class Demo05 {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while(i<=10){
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }
}

运算结果55

2.2 while循环注意事项

  • while循环的括号中的条件表达式返回值必须是Boolean类型
  • while循环的括号中不能直接写false常量
  • while循环的括号后面没有分号,循环控制的语句用大括号括起来
  • while循环的条件变量要及时更新,保证循环能够正常的结束

3.do while循环

do{
   执行语句;
   }
   while(条件表达式);

先执行do后面的语句,执行完之后在进行while后面的判断,和while循环的步骤相同
注意:不管条件是否满足,此循环都会被至少执行一次

//获取1-100之间,6的倍数的个数。
public class Demo06 {
    public static void main(String[] args) {
        int i = 1;
        int s = 0;
        while(i<=100){
            if(i<6){
                s=0;

            }
            else if ((i%6)==0){
                s++;

            }
            i++;
        }
        System.out.println(s);
    }
}

结果是16

4.for循环

4.1 for循环格式

for(初始化表达式(1):循环条件表达式(2):循环后的操作表达式(3)){
执行语句(4);
}

执行的顺序就是1>2>4>3>2>4>3>2>4…直到判断的结果是false时,循环结束。

4.2 for循环代码举例

class ForDemo 
{
	public static void main(String[] args) 
	{

		for(int x= 1; x<3; x++)
		{
			System.out.println("x="+x);
		}
    }
}

4.3 for和while的区别

while和for可以进行互换,区别在于for为了循环而定义的变量在for循环结束后就在内存中释放了,但是while循环使用的变量在while循环结束后还可以继续使用。

4.4 for循环练习

//获取1-100之间6的倍数的个数
public class Demo07 {
    public static void main(String[] args) {
        int s =0;
        for(int i = 1;i<101;i++){
            if(i%6==0){
                s++;
            }
        }
        System.out.println(s);
    }
}

结果是16

五、嵌套循环

class ForForDemo 
{
	public static void main(String[] args) 
	{

		for (int x=0; x<3; x++)//行
		{
			for(int y=0; y<4; y++)//每一行的个数。
			{
				System.out.print("y="+y);
			}
		}
	}
}
通过结果发现:在循环嵌套中,外循环执行一次,内循环要从头外尾执行完。

六、流程控制(continue,break)

break:终止该层循环
continue:跳过该层循环

break是终止循环,即在程序中遇到break,那么break所属的循环将结束。
		for (int x=0; x<3 ;x++ )
		{
			if(x==1)
				break;
			System.out.println("x="+x);			
		}
continue是结束本次循环,继续下次循环。循环是不会结束的。
		for (int x=0; x<10 ;x++ )
		{
			if(x%2==0)
				continue;
			System.out.println("x="+x);
			
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值