JAVA基础知识之控制流程

一、控制流程概念

           1、控制流程就是控制程序的走向

           2、实现手段为:条件判断和循环

二、条件判断

            1、主要通过判定条件成立与否执行不同代码,达到控制走向的目的

            2、主要方式有:if 语句, switch case 语句

           3、 if语句:主要有三类,if语句、 if else语句 、if else if......else语句

if 语句:若成立则执行代码

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        int a = 3;

        if (a > 0)
        {
            System.out.println("你好");
        }
        
    }
}

  控制台输出如下:


 if  else 语句:若成立则执行代码,若不成立,则执行else语句内的代码

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        int a = 3;

        if (a >5)
        {
            System.out.println("你好");
        }else {
            
            System.out.println("我好");
        }
        
    }
}

控制台输出如下:



if  elseif else 语句:用于多个条件判断,若全部不满足,在执行else内代码,若其中某个条件执行,则剩余条件全部跳过

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        int a = 3;

        if (a > 5)
        {
            System.out.println("你好");
        }
        else if (a > 4)
        {

            System.out.println("我好");

        }
        else
        {

            System.out.println("他好");
        }

    }
}

控制台输出:



特别注意点:

     1、当多个条件时请选择使用if else if else 语句而不是多个if语句---因为多个if语句程序会全部测试,而if elseif else 语句某个满足后,其余的都会跳过不执行,减少了程序的执行时间

     2、当使用if else if else 语句时,请考虑全部条件,避免出现漏洞(如上图举例是存在漏洞的)


switch case 语句:常用于对某个值进行多种匹配而后不同处理,其主要形式如下所示:

switch (key)
        {
        case value:
            
            break;

        default:
            break;
        }

   1、key不能是布尔型变量,否则会编译出错

   2、若case中的value等于key,那么程序将直接执行case后面的代码,而后break跳出switch,执行程序剩余代码

   3、default指当所有的case都不能匹配key时的执行代码

   4、如下面代码所示,最终控制台将会输出"值是5",因为前面4个case都不满足,第5个满足了,所以会匹配,然后打印内容,请注意,switch是按顺序往下匹配的,所以若我case 5这个代码放在第一位,那么剩余的4个就不会执行,也达到了节省时间的目的

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        int a=5;
        
        switch (a)
        {
        case 1:
            System.out.println("值是1");
            break;
        case 2:
            System.out.println("值是2");
            break;
        case 3:
            System.out.println("值是3");
            break;
        case 4:
            System.out.println("值是4");
            break;
        case 5:
            System.out.println("值是5");
            break;
            
        default:
            break;
        }
        
        System.out.println("switch执行结束");
        
    }
}

三、循环

     1、循环就是指不断的执行同一段代码以达到目的,如计算1-100之和等

     2、JAVA主要有三类循环,while循环、do while循环、for 循环

while 循环:先判定条件,若为真,进入循环代码,执行一轮后。条件再次判定,直到不满足时跳出循环

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        int count=1;
        
        int total=0;
        
      while (count<=100)
    {
       
          total=total+count;
          count++;
    }
        
       
    }
}

do while循环:先执行一次循环代码,而后判定条件,若条件为真,继续循环,若条件为假,跳出循环

package textCode;

public class Test
{

    public static void main(String[] args)
    {
        int count = 1;
        int total = 0;
        do
        {
            total=total+count;
            count++;
        }
        while (count<=100);  
    }
}

for循环:先对变量赋值,而后判定条件,若为真,则执行循环代码,而后变量自增(自减),在判定条件以此循环

package textCode;

public class Test
{
    public static void main(String[] args)
    {
        int total = 0;
        for (int i = 1; i <= 100; i++)
        {
            total = total + i;
        }
    }
}

特别注意点:

       1、for循环是JAVA中最为常用的循环,while和dowhile使用较少

       2、for循环的判断条件基本是int型变量是否大于或者小于某个数,而while和dowhile循环判断条件可以是数字也可以直接布尔型

四、特殊的控制

        在JAVA中,有两个关键字是可以用在循环和判断中,用于进行特殊的控制,分别是break、continue

break:代码执行到该关键字时,则会跳出当前的循环,若是嵌套循环,则只会跳出当前而回到外层循环

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        for (int i = 0; i < 10; i++)
        {
            System.out.println(i);

            if (i == 3)
            {
                System.out.println("跳出循环");
                break;
            }
        }
        
        System.out.println("循环结束");

    }
}

控制台输出:


continue:回到循环的起点,后续代码不执行

package textCode;

public class Test
{

    public static void main(String[] args)
    {

        for (int i = 0; i < 10; i++)
        {
            
            if (i%3==0)
            {
                continue;
            }
            System.out.println(i);
        }
        
        System.out.println("循环结束");

    }
}

控制台输出:当i为3、6、9时直接回到循环起点,不执行后面的打印代码






猜你喜欢

转载自blog.csdn.net/ai_bao_zi/article/details/80814595
今日推荐