Java 循环结构 - for, while 及 do...while

顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,,就需要使用循环结构。

Java中有三种主要的循环结构:

  • while 循环
  • do…while 循环
  • for 循环

在Java5中引入了一种主要用于数组的增强型for循环。


while 循环

while是最基本的循环,它的结构为:

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

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

实例

Test.java 文件代码:

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 循环

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

do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

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

注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

实例

Test.java 文件代码:

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循环

虽然所有循环结构都可以用 while 或者 do...while表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。

for循环执行的次数是在执行前就确定的。语法格式如下:

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

关于 for 循环有以下几点说明:

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。

实例

Test.java 文件代码:

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

Java 增强 for 循环

Java5 引入了一种主要用于数组的增强型 for 循环。

Java 增强 for 循环语法格式如下:

for(声明语句 : 表达式) { //代码句子 }

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

实例

Test.java 文件代码:

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 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。

break 跳出最里层的循环,并且继续执行该循环下面的语句。

语法

break 的用法很简单,就是循环结构中的一条语句:

break;

实例

Test.java 文件代码:

public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 等于 30 时跳出循环 if( x == 30 ) { break;
} System.out.print( x );
System.out.print("\n");
} }
}

以上实例编译运行结果如下:

10
20

continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

语法

continue 就是循环体中一条简单的语句:

continue;

实例

Test.java 文件代码:

public class Test {
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...else

一个 if 语句包含一个布尔表达式和一条或多条语句。

语法

if 语句的语法如下:

if(布尔表达式) { //如果布尔表达式为true将执行的语句 }

如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。

Test.java 文件代码:

public class Test {
public static void main(String args[]){
int x = 10;
if( x < 20 ){
  System.out.print("这是 if 语句");
} } }

以上代码编译运行结果如下:

这是 if 语句

if...else语句

if 语句后面可以跟 else 语句,当 if 语句的布尔表达式值为 false 时,else 语句块会被执行。

语法

if…else 的用法如下:

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

实例

Test.java 文件代码:

public class Test {
public static void main(String args[]){ int x = 30;
if( x < 20 ){
  System.out.print("这是 if 语句");
}else{
  System.out.print("这是 else 语句");
} } }

以上代码编译运行结果如下:

这是 else 语句

if...else if...else 语句

if 语句后面可以跟 else if…else 语句,这种语句可以检测到多种可能的情况。

使用 if,else if,else 语句的时候,需要注意下面几点:

  • if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
  • if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
  • 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。

语法

if...else 语法格式如下:

if(布尔表达式 1){ //如果布尔表达式 1的值为true执行代码 }else if(布尔表达式 2){ //如果布尔表达式 2的值为true执行代码 }else if(布尔表达式 3){ //如果布尔表达式 3的值为true执行代码 }else { //如果以上布尔表达式都不为true执行代码 }

实例

Test.java 文件代码:

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("这是 else 语句");
} }
}

以上代码编译运行结果如下:

Value of X is 30

嵌套的 if…else 语句

使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。

语法

嵌套的 if…else 语法格式如下:

if(布尔表达式 1){ ////如果布尔表达式 1的值为true执行代码 if(布尔表达式 2){ ////如果布尔表达式 2的值为true执行代码 } }

你可以像 if 语句一样嵌套 else if...else。

实例

Test.java 文件代码:

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

Java switch case 语句

switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

语法

switch case 语句语法格式如下:

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

switch case 语句有如下规则:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

实例

Test.java 文件代码:

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("优秀");
break;
case 'B' :
case 'C' : System.out.println("良好");
break;
case 'D' : System.out.println("及格");
case 'F' : System.out.println("你需要再努力努力");
break;
default : System.out.println("未知等级");
}
System.out.println("你的等级是 " + grade);
} }

以上代码编译运行结果如下:

良好
你的等级是 C

如果 case 语句块中没有 break 语句时,JVM 并不会顺序输出每一个 case 对应的返回值,而是继续匹配,匹配不成功则返回默认 case。

Test.java 文件代码:

public class Test {
public static void main(String args[]){
int i = 5;
switch(i){ case 0: System.out.println("0");
case 1: System.out.println("1");
case 2: System.out.println("2");
default: System.out.println("default");
} }
}

以上代码编译运行结果如下:

default

如果 case 语句块中没有 break 语句时,匹配成功后,从当前 case 开始,后续所有 case 的值都会输出。

Test.java 文件代码:

public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0: System.out.println("0");
case 1: System.out.println("1");
case 2: System.out.println("2");
default: System.out.println("default");
} } }

以上代码编译运行结果如下:

1
2
default

如果当前匹配成功的 case 语句块没有 break 语句,则从当前 case 开始,后续所有 case 的值都会输出,如果后续的 case 语句块有 break 语句则会跳出判断。

Test.java 文件代码:

public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0: System.out.println("0");
case 1: System.out.println("1");
case 2: System.out.println("2");
case 3: System.out.println("3");
break;
default: System.out.println("default");
} }
}

以上代码编译运行结果如下:

1
2
3
 

Java基础之循环语句、条件语句、switch case 语句的更多相关文章

  1. Java基础语法学习(1)switch...case

    switch...case的标准语法 switch(待选择的变量) { case 值1:语句1; break; case 值2:语句2: break; ....... case 值n:语句n; bre ...

  2. JavaSE基础(七)--Java流程控制语句之switch case 语句

    Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句语法格式如下: switch(exp ...

  3. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  4. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  5. JavaScript基础知识(if、if else、else if、while、switch...case语句)

    13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句     "{ }"将多条语句包裹 u ...

  6. Python | 基础系列 · Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  7. Java switch case 语句

    switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch(expression){ case value : //语句 break; //可选 ca ...

  8. Java switch case语句

    switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. switch case 语句语法格式如下: switch(expression){ case value : ...

  9. while, do-while ,switch···case语句的学习与运用

    1.while语句:当···的时候 格式:初始条件           while(循环条件)         {          循环体;          状态改变;         } 相当于 ...

随机推荐

  1. powershell获取windows子文件夹的大小

    $startFolder = "E:\Migration\" $colItems = (Get-ChildItem $startFolder | Where-Object {$_. ...

  2. MySQL安装及后续配置

    rpm -qa | grep mysql  检查已安装的mysql版本 rpm -e --nodeps mysql-libs-5.1.71 卸载 tar -zxvf MySQL.tar.gz 解压 安 ...

  3. 达里奥:典型的去杠杆化过程是怎么进行的zz

    猛人RayDalio的“三部曲”之三:关于去杠杆化的深入理解 作者系统地阐述了去杆杠化过程并深入探讨去杆杠化的运作机理,对我们理解当前全球乃至中国.即将或者已经面临的去杠杆化过程,应当能够带来一些帮助 ...

  4. python黑帽子-黑客与渗透测试编程之道(源代码)

    链接: https://pan.baidu.com/s/1i5BnB5V   密码: ak9t

  5. 《pyhton语言程序设计》_第一章笔记

    章1.62 (1).python区分大小写. (2).python忽略在符号#之后的同行的内容 (3).python和matlab很相似(个人感觉) (4).章节1.91: >>>i ...

  6. git 中Pull/Request 的初步

    1. 目的: pull/request (简称PR) 是 项目管理者(管理者)和项目开发者(开发者)之间提交和确认工作成果的机制. 2. 流程: 开发者: 在本地创建特性分支. > git ch ...

  7. Lerning Entity Framework 6 ------ Working with in-memory data

    Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...

  8. rabbitMq 初步

    RabbitMQ的工作原理 它的基本结构 组成部分说明如下: Broker:消息队列服务进程,此进程包括两个部分:Exchange和Queue. Exchange:消息队列交换机,按一定的规则将消息路 ...

  9. cad2013卸载/安装失败/如何彻底卸载清除干净cad2013注册表和文件的方法

    cad2013提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2013失败提示cad2013安装未完成,某些产品无法安装,也有时候想重新安装cad2013 ...

  10. apache环境之困扰,Rewrite导致无法加载多个不同的.html文件

    又是一个项目,为访问多个纯静态html页面h5游戏页,能够做一些简单分享和跳转即可.原本是一个简单得不能的项目,但是却多生了事端. 我按照apache的惯例,将文件上传到服务器的DocumentRoo ...