周全控制Java中的輪回掌握語句與前提斷定語句的應用。本站提示廣大學習愛好者:(周全控制Java中的輪回掌握語句與前提斷定語句的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是周全控制Java中的輪回掌握語句與前提斷定語句的應用正文
輪回掌握
能夠存在一種情形,當我們須要履行的代碼塊數次,平日被稱為一個輪回。
Java有異常靈巧的三輪回機制。可使用以下三種輪回之一:
截至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 語句:
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...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 語句:
示例
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