首頁  >  文章  >  Java  >  Java 程式設計中的循環

Java 程式設計中的循環

WBOY
WBOY原創
2024-08-30 15:24:311137瀏覽

簡單定義中的「編碼」意味著告訴電腦做什麼的方法;然而,這並不像看起來那麼容易,但到目前為止,我們不會專注於後面的部分(意味著簡單或困難)。在本主題中,我們將學習 Java 程式設計中的循環。

廣告 該類別中的熱門課程 程式語言 - 專業化 | 54 課程系列 | 4 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

電腦只能理解「ON」和「OFF」類型的數據,也就是俗稱的「二進位」值。二進位代碼由(0 和 1)組成,全世界的電腦都能理解。但問題是我們不能僅僅為了讓電腦理解我們要求它們計算的內容而寫入數萬億個 0 和 1。這就是程式語言或編碼發揮作用的地方。

這樣,我們就成功地將我們的理解過濾到了編碼層面,現在,既然我們知道了「編碼」的作用以及我們為什麼編碼,我們就必須進一步深入到「循環」層面,這就是討論的標題是?

我們身邊有好幾個PL;其中許多用於 Web 開發,其他用於桌面應用程式開發,有些稱為高級 PL,有些稱為低級 PL。所有這些程式語言都有一些共同點,即「循環」。

更深入地討論,循環幾乎存在於所有程式語言中;讓我們看看他們對開發人員有什麼優勢 –

  • 這些是「可重複使用的」
  • 它們縮小了「編碼」的大小。
  • 他們讓「控制」變得簡單。
  • 它們往往會降低「複雜性」。

JAVA 程式設計中的循環旨在解決程式碼複雜性,可用且可供開發人員根據需求重複使用程式碼。

Java 中的循環類型

java中循環的類型如下:

在JAVA中,迴圈是迭代語句。這些語句幫助開發人員(或使用者)迭代程式碼,或一組程式碼運行多次(根據需要)。

在JAVA中,循環主要有3大類,分別是

  • FOR 循環
  • While 循環
  • DO-While 迴圈

在深入研究這些循環之前,我們希望讀者了解一件事(這對所有三個循環都有價值)。無論是 FOR、WHILE 或 DO WHILE,都有起始、主體和最後的目的地。不用多說,讓我們一一看看它們 –

1. For 迴圈

如果您是開發人員,並且希望在獲得最終結果(或結果)之前執行或執行程式的一部分特定次數,那麼您將使用 FOR 迴圈。請記住,只有當您清楚知道要執行語句的次數時,您才會使用「For 迴圈」。 FOR 迴圈會重複自身,直到其值等於「TRUE」。

讓我們來看看它的流程圖,以便更好更清楚地理解 –

Java 程式設計中的循環

文法

for(initialization condition; testing condition; increment/decrement)
{
statement(s)
}

說明

所有 3 個參數(即初始化、條件和遞增/遞減)都保留在 FOR 迴圈中的一個語句中。初始化意味著提供變數的初始值。條件表示我們要在程式中檢查的值。增量/減量表示您希望在循環中擁有的值;該值將相應增加或減少。迴圈體以大括號開始,以大括號 (}) 結束,並包含將使用迴圈執行的語句。

範例

我們的目的是列印 1 到 100 之間的所有偶數。

class Test
{
public Static Void Main (String ar [])
{
int no; for (no = 2; no <=100; no = no+2)
{
System.out. print ln(no);
}
}
}

程式的輸出將為 2,4,6,8,10,12…………………..96,98,100

2. While 迴圈

只有當需要重複執行一定數量的語句直到條件滿足時,我們才需要?這裡,與 FOR 迴圈不同,在執行語句之前先檢查條件。

讓我們看一下它的流程圖,以便更好更清楚地理解 –

Java 程式設計中的循環

文法

while (boolean condition)
{
loop statements
}

說明

雖然循環以放置在括號內的應用條件語句開始,但它們也將循環語句保留在大括號內。正如我們所說,WHILE 循環將一直運行,直到該值保持為真值為止。

Example 

We want to print all the odd numbers between 1 to 100.

class Test
{
public static void main (String ar[])
{
int no = 1;
while (no<100)
{
System.out.print (no);
No = no +2;
} } }

Output for this program will be 1,3,5,7,9,11………………………………………..97,99

3. Do While

There is not much difference between the WHILE and DO WHILE loops; the difference lies in their statement validation. In DO WHILE, the condition is checked after execution of the block of statements; hence we can say in DO WHILE, the statements are at least executed once.

Let us see the flowchart of it for better and clear understanding –

Java 程式設計中的循環

Syntax 

do
{
statements..
}
while (condition);

Explanation 

There is no condition checking in the DO WHILE loop for the first time; later, the condition is checked for TRUE or FALSE. If it is TRUE, then the next iteration of loops start; otherwise, the loop terminates itself.

Example

class Test
{
public Static Void Main (String ar[])
{
int no = 2;
do
{
System.out.print (no);
No = no + 2;
}
while (no<=100);
}}}

Output for this program will be – 2,4,6,8,10…………….98,100

Comparison Between Different Types of Loops

The comparison between different types of loops are as follows:

1) Declaration

For Loop

for (initialization; condition; iteration){
//body of 'for' loop
}

While Loop

Statements; //body of loop
}

Do While

do
{
Statements
}
while (condition);

2) We use FOR loop if the user knows the time of iteration, whereas the WHILE and DO WHILE loop is preferred when the number of iteration is not known.

3) Regarding the conditional statement in FOR loop, it will operate infinite time, whereas for WHILE and DO WHILE the absence of conditional statement will give ‘Compilation Error’.

Conclusion

The loops concept is crucial for any users who belong to development; if they are freshers and prepare for exams or interviews, they must be well-rehearsed with the Loops concepts. We have covered all the crucial aspects of Loops, and this is how things work in Loops. These 3 Loops are the most crucial ones, and the rest are improvised on them. If you have a good grip over these, then the rest will be quite easier to understand.

以上是Java 程式設計中的循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn