---
title: "if-else 等 控制执行流程"
author: "zsl131"
date: "2010-03-24T15:02:59Z"
tags: []
categories: ["编程语言"]
slug: "if-else"
draft: false
lastmod: "2010-03-25T01:12:11Z"
id: 623664
url: "https://zsl131.iteye.com/blog/623664"
---

**控制执行流程**

 

**就像有知觉的生物一样，程序必须在执行过程中控制它的世界，并做出选择。在Java中，你要使用执行控制语句来做出选择。**

 

Java中使用的流程控制语句和其它计算机高级语言的流程控制语句基本相同。Java中涉及到的流程语句的关键字大致包括：if-else、while、do-while、for、return、break、continue以及选择语句switch，下面用一些小例子来介绍一下这些关键字。

 

 

**·If-else**

 

**public** **class** IfTest {

 

    **public** **static** **void** main(String \[] args) {

      

       **int** a = 0;

       **int** b = 1;

      

       //作为条件的值是一个boolean的值

       //boolean 为true或false

       **if**(a&lt;b) {

           System.*out*.println("a&lt;b");

       } **else** **if**(a==b) {

           System.*out*.println("a==b");

       } **else** {

           System.*out*.println("a&gt;b");

       }

    }

}

 

**·while**

**public** **class** WhileTest {

 

    **public** **static** **void** main(String\[] args) {

      

       **int** num = 0;

       //作为while的条件也是一个boolean类型的值

       **while**(num&lt;10) {

           System.*out*.println("当前值:"+num);

           //这里是控制退出while循环的条件

           //num++相当于num=num+1;这也叫做先赋值型，另一种叫后赋值型：++num;

           num++;

       }

    }

 

}

 

**·do-while**

**public** **class** DoWhileTest {

 

    **public** **static** **void** main(String\[] args) {

 

       **int** num = 0;

       //不管满不满足条件都会先执行一次do

       **do**{

           System.*out*.println("当前值:"+num);

           num++;

       } **while** (num&lt;10); {

           System.*out*.println("退出while");

       }

    }

 

}

 

 

**·for**

**public** **class** ForTest {

 

    **public** **static** **void** main(String\[] args) {

 

       //第一种传统的for循环

       **for**(**int** i=0;i&lt;10;i++) {

           System.*out*.print("当前值："+i+"  ");

       }

       //这句只是为了换行用，下同

       System.*out*.println();

       //另一种新型的for循环

       //先定义一个String类型的数组

       String \[] strs = **new** String\[]{"abc","bcd","cde","def","efg"};

       //for循环里的第一个参数是说明的这个数组容器里元素的类型为String

       //第二个参数是定义的一个变量str

       //第三个参数是要循环的容器对象strs

       **for**(String str:strs) {

           System.*out*.print(str+",");

       }

       System.*out*.println();

       //该for循环相当于：

       **for**(**int** i=0;i&lt;strs.length;i++) {

           String str = strs\[i];

           System.*out*.print(str+",");

       }

    }

 

}

 

**·return**

**public** **class** ReturnTest {

 

    **public** **static** **void** main(String\[] args) {

   

       //static静态方法可以通过类名直接调用

       System.*out*.println(ReturnTest.*getStr*());

    }

    /\**

     * 这里提前说到函数，也就是java中常说的方法

     * 这里定义了一个static静态的方法

     * 方法名为：getStr

     * 返回值的类型为：String

     * 参数列表为空

     * **@return** 返回一个String类型的值

     \*/

    **public** **static** String getStr() {

       **return** "返回的数据";

    }

 

}

 

 

**·break**

**public** **class** BreakTest {

 

    **public** **static** **void** main(String\[] args) {

 

       **int** temp = 5;

       **for**(**int** i=0;i&lt;10;i++) {

           System.*out*.println("当前值："+i);

           //break是退出最近的一个循环

           //这里当temp的值等于i的值时就退出for循环，所以只会到5的时候就不会再执行了

           **if**(temp==i) {

              **break**;

           }

       }

    }

 

}

 

**·continue**

**还有一部份没有上传完整，如有需要请下载下面的附件查看，源代码在该附件中。**