首 页最新软件下载排行文章资讯投稿发布下载专题
维维下载站
您的位置:首页编程开发网络编程编程其它 → Java中循环控制语句及条件判断语句使用例子详解

Java中循环控制语句及条件判断语句使用例子详解

来源:本站整理 发布时间:2016-2-15 10:14:19 人气:

本文主要为编程爱好者详细介绍Java中的循环控制语句与条件判断语句的使用心得,主要是循环和判断是Java编程中流程控制的基础,有兴趣的快点收藏此文吧。
 

循环控制
有时候会存在一种情况,那就是当我们需要执行的代码块数次,通常被称为一个循环。
Java有很灵活的三循环机制。可以使用以下三种循环之一:

  • while 循环
  • do...while 循环
  • for 循环

截至Java5,对增强的for循环进行了介绍。这主要用于数组。

while 循环
while循环是一个控制结构,可重复的特定任务次数。

语法

while循环的语法是:

while(Boolean_expression)
{
  //Statements
}

在执行的时候,要是布尔表达式的结果为真,则循环中的动作将被执行。只要该表达式的结果为真,执行将继续下去。

在这里,while循环的关键点是循环可能不会永远运行。当表达式进行测试,结果为假,循环体将被跳过,在while循环以后的第一个语句将被执行。

示例:

public class Test {

  public static void main(String args[]) {
   int x = 10;

   while( x < 20 ) {
     System.out.print("value of x : " + x );
     x++;
     System.out.print("\n");
   }
  }
}
这样的话就会产生以下结果:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

do...while 循环
do ... while循环类似while循环,不同的是一个do ... while循环是保证至少执行一次。

语法

do...while循环的语法是:

do
{
  //Statements
} while (Boolean_expression);

请注意,布尔表达式出现在循环的结尾,因此在循环中的语句执行前一次布尔测试。

要是布尔表达式为真,控制流跳回,而且在循环中的语句再次执行。这一过程反复进行,直到布尔表达式为假。

示例:

public class Test {

  public static void main(String args[]){
   int x = 10;

   do{
     System.out.print("value of x : " + x );
     x++;
     System.out.print("\n");
   }while( x < 20 );
  }
}

这样便会产生以下结果:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

for 循环
for循环是一个循环控制结构,能够有效地编写需要执行的特定次数的循环。

知道一个任务要重复多少次时,for循环是有好处的。

语法

for循环的语法是:

for(initialization; Boolean_expression; update)
{
  //Statements
}

下面是一个for循环的控制流程:

初始化步骤首先被执行,而且仅一次。这个步骤可以声明和初始化任何循环控制变量。不用将一个声明放在这里,只需一个分号出现。

接下来,布尔表达式求值。要是是 true,则执行循环体。要是是false,则循环体不执行,而且流程控制的跳转到经过for循环的下一个语句。

接着循环体在for循环执行时,控制流程跳转备份到更新语句。该语句允许更新任何循环控制变量。这个语句可以留空,只要一个分号出现在布尔表达式以后。

布尔表达式现在再次评估计算。假如是true,循环执行,并且重复这个过程(循环体,接着更新的步骤,之后布尔表达式)。然后,布尔表达式为 false,则循环终止。
示例

public class Test {

  public static void main(String args[]) {

   for(int x = 10; x < 20; x = x+1) {
     System.out.print("value of x : " + x );
     System.out.print("\n");
   }
  }
}

这样就会产生以下结果:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

for 循环在 Java 中新特性
截至Java5,对增强的for循环进行了介绍。这主要用于数组。

语法

增强的for循环的语法是:

for(declaration : expression)
{
  //Statements
}

声明: 新声明块变量,这是一种和你所正在访问数组中的元素兼容的变量。该变量在for块内可以被利用并且它的值作为当前的数组元素将是相同的。

表达: 这个计算结果完成需要循环数组。表达式可以是一个数组变量或返回一个数组的方法调用。
示例:

public class Test {

  public static void main(String args[]){
   int [] numbers = {10, 20, 30, 40, 50};

   for(int x : numbers ){
     System.out.print( x );
     System.out.print(",");
   }
   System.out.print("\n");
   String [] names ={"James", "Larry", "Tom", "Lacy"};
   for( String name : names ) {
     System.out.print( name );
     System.out.print(",");
   }
  }
}

这样就会产生以下结果:
10,20,30,40,50,
James,Larry,Tom,Lacy,

break 关键字
关键字break是用来停止整个循环的。 break关键字必须使用于任何循环中或者一个switch语句中。

关键字break把停止最内层循环的执行,并且开始执行在块以后的下一行代码。

语法

break语法是任何循环中一个单独的语句:

代码如下:
break

示例
:public class Test {

  public static void main(String args[]) {
   int [] numbers = {10, 20, 30, 40, 50};

   for(int x : numbers ) {
     if( x == 30 ) {
     break;
     }
     System.out.print( x );
     System.out.print("\n");
   }
  }
}

这样的话会产生以下结果:
10
20

continue 关键字
continue关键字可以在任一环的控制结构使用。它使循环马上跳转到循环的下一次迭代.

在for循环中,continue关键字会导致控制流马上跳转到更新语句。
在一个while循环或do/while循环,控制流马上跳转到布尔表达式。
语法

continue 语法是任何循环中一个单独的语句:

代码如下:
continue

示例:

  public static void main(String args[]) {
   int [] numbers = {10, 20, 30, 40, 50};

   for(int x : numbers ) {
     if( x == 30 ) {
     continue;
     }
     System.out.print( x );
     System.out.print("\n");
   }
  }
}

这样就会产生以下结果:

10
20
40
50

条件判断
在 Java 中有两种类型的条件判断语句,它们分别为:

  • if 语句
  • switch 语句

if 语句:

if 语句由一个布尔表达式以后跟一个或者多个语句组成。

语法

if 语句的语法是:

if(Boolean_expression)
{
  //Statements will execute if the Boolean expression is true
}

要是布尔表达式的值为 true,那么代码里面的块 if 语句将被执行。假如不是 true,在 if 语句(大括号后)结束以后的第一套代码将被执行。

示例:

public class Test {

  public static void main(String args[]){
   int x = 10;

   if( x < 20 ){
     System.out.print("This is if statement");
   }
  }
}

这样就会产生以下结果:
This is if statement

if...else 语句
任何 if 语句后面可以跟一个可选的 else 语句,当布尔表达式为 false,语句被执行。

语法

if...else 的语法是:

if(Boolean_expression){
  //Executes when the Boolean expression is true
}else{
  //Executes when the Boolean expression is false
}

示例:

public class Test {

  public static void main(String args[]){
   int x = 30;

   if( x < 20 ){
     System.out.print("This is if statement");
   }else{
     System.out.print("This is else statement");
   }
  }
}

这样就会产生以下结果:
This is else statement

if...else if...else 语句
if 后面可以跟一个可选的 else if...else 语句,在测试不同条件下单一的 if 语句和 else if 语句是非常有用的。

当使用 if , else if , else 语句时有几点要牢记。

  • 一个 if 语句可以有0个或一个 else 语句 且它必须在 else if 语句的之后。
  • 一个 if 语句 可以有0个或多个 else if 语句且它们必须在 else 语句之前。
  • 一旦 else if 语句成功, 余下 else if 语句或 else 语句都不会被测试执行。

语法

if...else 的语法是:

if(Boolean_expression 1){
  //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
  //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
  //Executes when the Boolean expression 3 is true
}else {
  //Executes when the none of the above condition is true.
}

示例:

public class Test {

  public static void main(String args[]){
   int x = 30;

   if( x == 10 ){
     System.out.print("Value of X is 10");
   }else if( x == 20 ){
     System.out.print("Value of X is 20");
   }else if( x == 30 ){
     System.out.print("Value of X is 30");
   }else{
     System.out.print("This is else statement");
   }
  }
}

这样就会产生以下结果:
Value of X is 30

嵌套 if...else 语句
它始终是合法的嵌套 if-else 语句,这意味着你可以在另外一个 if 或 else if 语句中使用一个 if 或 else if 语句。

语法

嵌套 if...else 的语法如下:

if(Boolean_expression 1){
  //Executes when the Boolean expression 1 is true
  if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
  }
}

因为我们有嵌套的 if 语句,所以可以用类似的方式嵌套 else if...else。

示例:

public class Test {

  public static void main(String args[]){
   int x = 30;
   int y = 10;

   if( x == 30 ){
     if( y == 10 ){
       System.out.print("X = 30 and Y = 10");
     }
    }
  }
}

这样便会产生以下结果:
X = 30 and Y = 10

switch 语句
switch 语句允许一个变量来对一系列值得相等性进行测试。每一个值被称为一 case,而且被启动的变量会为每一个 case 检查。

语法

增强的 for 循环的语法是:

switch(expression){
  case value :
    //Statements
    break; //optional
  case value :
    //Statements
    break; //optional
  //You can have any number of case statements.
  default : //Optional
    //Statements
}

以下规则适用于 switch 语句:

  • 在 switch 语句中使用的变量只能是一个字节,short,int 或 char。
  • 在一个 switch 语句中可以有任何数量的 case 语句。每个 case 后跟着即将被比较的值和一个冒号。
  • 对于 case 的值必须是相同的数据类型作为开关变量,它必须是一个常量或文字。
  • 当被启动了的变量与 case 是相等的,那 case 后的语句将执行,一直到 break 为止。
  • 当达到一个 break 语句,switch 终止,并且控制流跳转到跟着 switch 语句的下一行。
  • 不是每一个 case 需要包含一个 break。如果没有出现 break,控制流将贯穿到后面的 case 直到 break 为止。
  • switch 语句可以有一个可选默认 case ,它必须出现在 switch 的结束处。在执行一项任务时没有任何 case 是真,那默认 case 可被使用。在默认 case 中不需要 break。

示例:

public class Test {

  public static void main(String args[]){
   //char grade = args[0].charAt(0);
   char grade = 'C';

   switch(grade)
   {
     case 'A' :
      System.out.println("Excellent!"); 
      break;
     case 'B' :
     case 'C' :
      System.out.println("Well done");
      break;
     case 'D' :
      System.out.println("You passed");
     case 'F' :
      System.out.println("Better try again");
      break;
     default :
      System.out.println("Invalid grade");
   }
   System.out.println("Your grade is " + grade);
  }
}
编译并且运行上面使用各种命令行参数的程序。这样就会产生以下结果:
$ java Test
Well done
Your grade is a C
相关下载
栏目导航
本类热门阅览